Skip to content

Commit fd3e52e

Browse files
committed
CargoService
1 parent ecc00c0 commit fd3e52e

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

Examples/E1/CargoService.cs

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Examples.E1
8+
{
9+
public class CargoService
10+
{
11+
private CargoFactory CargoFactory { get; set; }
12+
private string Origin =string.Empty;
13+
private string Destination = string.Empty;
14+
public ICargo BookProduct(string type)
15+
{
16+
17+
return CargoFactory.CreateCargoFactory(type,Origin,Destination);
18+
19+
}
20+
}
21+
22+
internal abstract class CargoFactory
23+
{
24+
public ICargo CreateCargoFactory(string type,string origin,string destination)
25+
{
26+
CargoFactory factory =null ;
27+
if (type == "Air")
28+
{
29+
factory= new AirFactory();
30+
}
31+
if (type == "Ship")
32+
{
33+
factory = new ShipFactory(origin,destination);
34+
35+
}
36+
if (type == "Train")
37+
{
38+
factory = new TrainFactory();
39+
40+
}
41+
if (factory is null)
42+
throw new Exception("type is wrong");
43+
44+
return factory.CreateCargo();
45+
}
46+
47+
public abstract ICargo CreateCargo();
48+
49+
}
50+
51+
internal class TrainFactory : CargoFactory
52+
{
53+
public override ICargo CreateCargo()
54+
{
55+
var cargo = new Train();
56+
TrainMethod();
57+
return cargo;
58+
}
59+
60+
private void TrainMethod()
61+
{
62+
throw new NotImplementedException();
63+
}
64+
}
65+
66+
internal class ShipFactory : CargoFactory
67+
{
68+
public ShipFactory(string origin, string destination)
69+
{
70+
Origin = origin;
71+
Destination = destination;
72+
}
73+
74+
public string Origin { get; set; }
75+
public string Destination { get; set; }
76+
public override ICargo CreateCargo()
77+
{
78+
return new Ship(Origin, Destination);
79+
}
80+
}
81+
82+
internal class AirFactory : CargoFactory
83+
{
84+
private static Air Air { get; set; }
85+
public override ICargo CreateCargo()
86+
{
87+
if (Air is null)
88+
{
89+
Air = new Air();
90+
91+
}
92+
return Air;
93+
}
94+
}
95+
96+
internal class Train : ICargo
97+
{
98+
99+
}
100+
101+
internal class Ship : ICargo
102+
{
103+
public Ship(string origin, string destination)
104+
{
105+
Origin = origin;
106+
Destination = destination;
107+
}
108+
109+
public string Origin { get; }
110+
public string Destination { get; }
111+
}
112+
113+
internal class Air : ICargo
114+
{
115+
116+
}
117+
118+
public interface ICargo
119+
{
120+
121+
}
122+
}

0 commit comments

Comments
 (0)