-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
367 lines (250 loc) · 9.06 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#region Creational
//Singleton
AddTitle("Singleton:");
var instance1 = Singleton.GetInstance();
var instance2 = Singleton.GetInstance();
Console.WriteLine($"References Are Equal? {ReferenceEquals(instance1, instance2)}");
LogSeparator();
//Simple Factory
AddTitle("SimpleFactory:");
var desk = SimpleFactory.DeskFactory.MakeDesk(80, 30, 40);
Console.WriteLine(
$"woodenDesk height: {desk.GetHeight()} - width: {desk.GetWidth()} - length: {desk.GetLength()}");
LogSeparator();
//Factory Method
AddTitle("Factory Method:");
var woodenChairSeller = new FactoryMethod.WoodenChairSeller();
woodenChairSeller.SellChair();
var metalChairSeller = new FactoryMethod.MetalChairSeller();
metalChairSeller.SellChair();
LogSeparator();
//Builder
AddTitle("Builder:");
var burger = new Builder.BurgerBuilder(15)
.AddCheese()
.AddLettuce()
.AddPepperoni()
.Build();
Console.WriteLine(burger.GetDescription());
LogSeparator();
//Abstract
AddTitle("Abstract:");
AbstractFactory.IWindowFactory woodenWindowFactory = new AbstractFactory.WoodenWindowFactory();
AbstractFactory.IWindow woodenWindow = woodenWindowFactory.MakeWindow();
AbstractFactory.IWindowFittingExpert woodenWindowFittingExpert = woodenWindowFactory.MakeFittingExpert();
woodenWindow.GetDescription();
woodenWindowFittingExpert.GetDescription();
AbstractFactory.IWindowFactory ironWindowFactory = new AbstractFactory.IronWindowFactory();
AbstractFactory.IWindow ironWindow = ironWindowFactory.MakeWindow();
AbstractFactory.IWindowFittingExpert ironWindowFittingExpert = ironWindowFactory.MakeFittingExpert();
ironWindow.GetDescription();
ironWindowFittingExpert.GetDescription();
LogSeparator();
//Prototype
AddTitle("Prototype");
var originalCircle = new Prototype.Circle { Radius = 5 };
// Clone the prototype
var clonedCircle = (Prototype.Circle)originalCircle.Clone();
originalCircle.Draw();
clonedCircle.Draw();
LogSeparator();
#endregion
#region Structural
//Adapter
AddTitle("Adapter:");
Adapter.EuropeanSocket europeanSocket = new Adapter.EuropeanSocket();
Adapter.IPersianSocket persianSocket = new Adapter.SocketAdapter(europeanSocket);
Console.WriteLine("Client: Connecting Persian device to socket.");
persianSocket.Connect();
LogSeparator();
//Decorator
AddTitle("Decorator:");
Decorator.IPizza plainPizza = new Decorator.PlainPizza();
Decorator.IPizza cheesePizza = new Decorator.CheeseDecorator(plainPizza);
Decorator.IPizza tomatoPizza = new Decorator.TomatoDecorator(cheesePizza);
Console.WriteLine($"Plain Pizza: {plainPizza.GetDescription()}, Cost: {plainPizza.GetCost()}T");
Console.WriteLine($"Cheese Pizza: {cheesePizza.GetDescription()}, Cost: {cheesePizza.GetCost()}T");
Console.WriteLine($"Deluxe Pizza: {tomatoPizza.GetDescription()}, Cost: {tomatoPizza.GetCost()}T");
LogSeparator();
//Proxy
AddTitle("Proxy:");
Proxy.ISensitiveData adminData = new Proxy.SensitiveDataProxy("Admin");
Proxy.ISensitiveData regularUser = new Proxy.SensitiveDataProxy("User");
adminData.AccessData();
regularUser.AccessData();
LogSeparator();
//Facade
AddTitle("Facade:");
var facadeOperator = new Facade.FacadeOperator();
facadeOperator.PerformOperations();
LogSeparator();
//Bridge
AddTitle("Bridge");
Bridge.Shape redCircle = new Bridge.BridgeCircle(10, 10, 5, new Bridge.DrawApi1());
redCircle.Draw();
LogSeparator();
//Composite
AddTitle("Composite");
// Create leaf objects
var circle1 = new Composite.CompositeCircle();
var circle2 = new Composite.CompositeCircle();
// Create composite object
var compositeGraphic = new Composite.CompositeGraphic();
compositeGraphic.Add(circle1);
compositeGraphic.Add(circle2);
// Draw individual objects and the composite object
circle1.Draw();
circle2.Draw();
compositeGraphic.Draw();
LogSeparator();
//Flyweight
AddTitle("Flyweight");
var shapeFactory = new Flyweight.ShapeFactory();
var flyCircle1 = shapeFactory.GetShape("5");
flyCircle1.Draw(1, 2);
var flyCircle2 = shapeFactory.GetShape("10");
flyCircle2.Draw(3, 4);
LogSeparator();
#endregion
#region Behavioral
//Observer
AddTitle("Observer:");
Observer.WeatherStation weatherStation = new Observer.WeatherStation();
Observer.IObserver observer1 = new Observer.ConcreteObserver("Observer 1");
Observer.IObserver observer2 = new Observer.ConcreteObserver("Observer 2");
Observer.IObserver observer3 = new Observer.ConcreteObserver("Observer 3");
weatherStation.Attach(observer1);
weatherStation.Attach(observer2);
weatherStation.Attach(observer3);
weatherStation.SetTemperature(25.5f);
weatherStation.Detach(observer2);
weatherStation.SetTemperature(28.0f);
LogSeparator();
//Strategy
AddTitle("Strategy:");
Strategy.ShoppingCart cart = new Strategy.ShoppingCart();
Strategy.IPaymentStrategy creditCardPayment = new Strategy.CreditCardPaymentStrategy("123456789");
Strategy.IPaymentStrategy paypalPayment = new Strategy.PayPalPaymentStrategy("user@gmail.com");
cart.SetPaymentStrategy(creditCardPayment);
cart.Checkout(50000);
cart.SetPaymentStrategy(paypalPayment);
cart.Checkout(80000);
LogSeparator();
//Chain Of Responsibility
AddTitle("Chain Of Responsibility:");
ChainOfResponsibility.IHelpDeskHandler helpDeskHandler = new ChainOfResponsibility.LevelOneSupport();
var lowSeverityTicket =
new ChainOfResponsibility.HelpDeskTicket(Severity: 1, Description: "Printer issue");
var mediumSeverityTicket =
new ChainOfResponsibility.HelpDeskTicket(Severity: 2, Description: "Software problem");
var highSeverityTicket =
new ChainOfResponsibility.HelpDeskTicket(Severity: 4, Description: "Server down");
helpDeskHandler.HandleTicket(lowSeverityTicket);
helpDeskHandler.HandleTicket(mediumSeverityTicket);
helpDeskHandler.HandleTicket(highSeverityTicket);
LogSeparator();
//Command
AddTitle("Command:");
Command.RemoteControl remote = new Command.RemoteControl();
Command.Light livingRoomLight = new Command.Light();
Command.Tv livingRoomTv = new Command.Tv();
Command.ICommand livingRoomLightOn = new Command.LightOnCommand(livingRoomLight);
Command.ICommand livingRoomLightOff = new Command.LightOffCommand(livingRoomLight);
Command.ICommand livingRoomTvOn = new Command.TvOnCommand(livingRoomTv);
Command.ICommand livingRoomTvOff = new Command.TvOffCommand(livingRoomTv);
remote.SetCommand(livingRoomLightOn, livingRoomLightOff);
remote.SetCommand(livingRoomTvOn, livingRoomTvOff);
remote.PressOnButton(0);
remote.PressOffButton(0);
remote.PressOnButton(1);
remote.PressOffButton(1);
LogSeparator();
//Mediator
AddTitle("Mediator:");
Mediator.IAirTrafficControl atc = new Mediator.AirTrafficControl();
Mediator.IAircraft aircraft1 = new Mediator.Aircraft("Flight123", atc);
Mediator.IAircraft aircraft2 = new Mediator.Aircraft("Flight456", atc);
Mediator.IAircraft aircraft3 = new Mediator.Aircraft("Flight789", atc);
aircraft1.SendWarning("Traffic ahead.");
aircraft2.SendWarning("Turbulence reported.");
aircraft3.SendWarning("Hijack Warning.");
LogSeparator();
//State
AddTitle("State");
State.TrafficLight trafficLight = new State.TrafficLight();
// Simulate traffic light changes
trafficLight.Change(); // Red to Yellow
trafficLight.Change(); // Yellow to Green
trafficLight.Change(); // Green to Red
LogSeparator();
//Template Method
AddTitle("Template Method");
Console.WriteLine("Making tea:");
TemplateMethod.Tea tea = new TemplateMethod.Tea();
tea.PrepareBeverage();
Console.WriteLine("\nMaking coffee:");
TemplateMethod.Coffee coffee = new TemplateMethod.Coffee();
coffee.PrepareBeverage();
LogSeparator();
//Visitor
AddTitle("Visitor");
var shapes = new List<Visitor.IShape>
{
new Visitor.Circle(3),
new Visitor.Square(4),
new Visitor.Circle(5),
new Visitor.Square(2)
};
var areaCalculator = new Visitor.AreaCalculator();
foreach (var shape in shapes)
{
shape.Accept(areaCalculator);
}
LogSeparator();
//Iterator
AddTitle("Iterator");
var aggregate = new Iterator.ConcreteAggregate();
var iterator = aggregate.GetIterator();
while (iterator.HasNext())
{
var item = iterator.Next();
Console.WriteLine(item);
}
LogSeparator();
//Memento
AddTitle("Memento");
var originator = new Memento.Originator();
var caretaker = new Memento.Caretaker();
originator.State = "State1";
Console.WriteLine($"Current State: {originator.State}");
caretaker.Memento = originator.CreateMemento();
originator.State = "State2";
Console.WriteLine($"Updated State: {originator.State}");
originator.RestoreMemento(caretaker.Memento);
Console.WriteLine($"Restored State: {originator.State}");
LogSeparator();
//Interpreter
AddTitle("Interpreter");
Interpreter.IExpression expression = new Interpreter.AddExpression(new Interpreter.NumberExpression(1), new Interpreter.NumberExpression(2));
var context = new Interpreter.Context();
expression.Interpret(context);
Console.WriteLine("Result: " + context.Output); // Result: 3
LogSeparator();
#endregion
#region Utils
Console.ReadLine();
return;
void LogSeparator()
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine("*****************************************************************");
Console.ResetColor();
}
void AddTitle(string title)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(title);
Console.ResetColor();
}
#endregion