|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using System.Text; |
5 | | -using System.Threading.Tasks; |
| 1 | +namespace Examples.E1; |
6 | 2 |
|
7 | | -namespace Examples.E1 |
| 3 | +public class CargoService |
8 | 4 | { |
9 | | - public class CargoService |
| 5 | + public ICargo BookProduct(byte type, string? origin = null, string? destination = null) |
10 | 6 | { |
11 | | - private CargoFactory CargoFactory { get; set; } |
12 | | - private string Origin =string.Empty; |
13 | | - private string Destination = string.Empty; |
14 | | - public ICargo BookProduct(string type) |
| 7 | + CargoFactory? factory = null; |
| 8 | + if (type == (byte)CargoType.Air) |
15 | 9 | { |
16 | | - |
17 | | - return CargoFactory.CreateCargoFactory(type,Origin,Destination); |
18 | | - |
| 10 | + factory = new AirFactory(); |
19 | 11 | } |
20 | | - } |
21 | | - |
22 | | - internal abstract class CargoFactory |
23 | | - { |
24 | | - public ICargo CreateCargoFactory(string type,string origin,string destination) |
| 12 | + if (type == (byte)CargoType.Ship) |
25 | 13 | { |
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 | | - } |
| 14 | + if (string.IsNullOrWhiteSpace(origin)) |
| 15 | + throw new ArgumentException("Origin should not be null or empty.", nameof(origin)); |
46 | 16 |
|
47 | | - public abstract ICargo CreateCargo(); |
48 | | - |
49 | | - } |
| 17 | + if (string.IsNullOrWhiteSpace(destination)) |
| 18 | + throw new ArgumentException("Destination should not be null or empty.", nameof(destination)); |
| 19 | + factory = new ShipFactory(origin, destination); |
50 | 20 |
|
51 | | - internal class TrainFactory : CargoFactory |
52 | | - { |
53 | | - public override ICargo CreateCargo() |
54 | | - { |
55 | | - var cargo = new Train(); |
56 | | - TrainMethod(); |
57 | | - return cargo; |
58 | 21 | } |
59 | | - |
60 | | - private void TrainMethod() |
| 22 | + if (type == (byte)CargoType.Air) |
61 | 23 | { |
62 | | - throw new NotImplementedException(); |
| 24 | + factory = new TrainFactory(); |
| 25 | + |
63 | 26 | } |
| 27 | + if (factory is null) |
| 28 | + throw new Exception("cargotype is wrong please enter a new type"); |
| 29 | + return factory.Create(origin, destination); |
64 | 30 | } |
| 31 | +} |
65 | 32 |
|
66 | | - internal class ShipFactory : CargoFactory |
67 | | - { |
68 | | - public ShipFactory(string origin, string destination) |
69 | | - { |
70 | | - Origin = origin; |
71 | | - Destination = destination; |
72 | | - } |
| 33 | +public abstract class CargoFactory |
| 34 | +{ |
| 35 | + public abstract ICargo Create(string? origin = null, string? destination = null); |
| 36 | +} |
73 | 37 |
|
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 | | - } |
| 38 | +public class TrainFactory : CargoFactory |
| 39 | +{ |
| 40 | + public override ICargo Create(string? origin = null, string? destination = null) |
| 41 | + { |
| 42 | + var cargo = new Train(); |
| 43 | + TrainMethod(); |
| 44 | + return cargo; |
80 | 45 | } |
81 | 46 |
|
82 | | - internal class AirFactory : CargoFactory |
| 47 | + private void TrainMethod() => Console.WriteLine("train is new"); |
| 48 | +} |
| 49 | + |
| 50 | +public class ShipFactory : CargoFactory |
| 51 | +{ |
| 52 | + public ShipFactory(string origin, string destination) |
83 | 53 | { |
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 | | - } |
| 54 | + Origin = origin; |
| 55 | + Destination = destination; |
94 | 56 | } |
95 | 57 |
|
96 | | - internal class Train : ICargo |
| 58 | + public string Origin { get; set; } |
| 59 | + public string Destination { get; set; } |
| 60 | + public override ICargo Create(string? origin, string? destination) |
97 | 61 | { |
98 | | - |
| 62 | + return new Ship(origin, destination); |
99 | 63 | } |
| 64 | +} |
100 | 65 |
|
101 | | - internal class Ship : ICargo |
102 | | - { |
103 | | - public Ship(string origin, string destination) |
104 | | - { |
105 | | - Origin = origin; |
106 | | - Destination = destination; |
107 | | - } |
| 66 | +public class AirFactory : CargoFactory |
| 67 | +{ |
| 68 | + private static readonly Lazy<Air> _air = new Lazy<Air>(() => new Air()); |
108 | 69 |
|
109 | | - public string Origin { get; } |
110 | | - public string Destination { get; } |
| 70 | + public override ICargo Create(string? origin, string? destination) |
| 71 | + { |
| 72 | + return _air.Value; |
111 | 73 | } |
| 74 | +} |
112 | 75 |
|
113 | | - internal class Air : ICargo |
| 76 | +public class Train : ICargo |
| 77 | +{ |
| 78 | + public decimal SetPayment() |
114 | 79 | { |
| 80 | + return 500; |
| 81 | + } |
| 82 | +} |
115 | 83 |
|
| 84 | +public class Ship : ICargo |
| 85 | +{ |
| 86 | + public Ship(string origin, string destination) |
| 87 | + { |
| 88 | + Origin = origin; |
| 89 | + Destination = destination; |
116 | 90 | } |
117 | 91 |
|
118 | | - public interface ICargo |
| 92 | + public string Origin { get; set; } |
| 93 | + public string Destination { get; set; } |
| 94 | + public decimal SetPayment() |
119 | 95 | { |
| 96 | + return 2000; |
| 97 | + } |
| 98 | +} |
120 | 99 |
|
| 100 | +public class Air : ICargo |
| 101 | +{ |
| 102 | + public decimal SetPayment() |
| 103 | + { |
| 104 | + return 1000; |
121 | 105 | } |
122 | 106 | } |
| 107 | + |
| 108 | +public interface ICargo |
| 109 | +{ |
| 110 | + public decimal SetPayment(); |
| 111 | +} |
| 112 | +public enum CargoType |
| 113 | +{ |
| 114 | + NotSet = 0, |
| 115 | + Air = 1, |
| 116 | + Ship = 2, |
| 117 | + Train = 3, |
| 118 | +} |
| 119 | + |
0 commit comments