forked from carmeleve/DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
148 lines (120 loc) · 4.33 KB
/
Program.cs
File metadata and controls
148 lines (120 loc) · 4.33 KB
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
using DesignPatterns.AbstractFactory;
using DesignPatterns.Adapter;
using DesignPatterns.Builder;
using DesignPatterns.Composite;
using DesignPatterns.Decorator;
using DesignPatterns.Dinosaur;
using DesignPatterns.Facade;
using DesignPatterns.FactoryMethod;
using DesignPatterns.Proxy;
using System;
using System.Collections.Generic;
using System.Linq;
namespace DesignPatterns
{
class Program
{
static void Main(string[] args)
{
ProxyExample();
}
public static void ProxyExample()
{
var secureNestProxy = new SecureNestProxy(new RealNest());
secureNestProxy.Access("Stegosaurus");
secureNestProxy.Access("TRex");
}
public static void FacadeExample()
{
bool meteor = false;
var dinoEcosystemFacade = new DinoEcosystemFacade(new Soil(1));
while (!meteor)
{
dinoEcosystemFacade.RunAGeneration();
Console.WriteLine("Has a meteor hit? (Y/N)");
var response = Console.ReadLine();
if(response == "Y")
{
meteor = true;
}
}
Console.WriteLine("A meteor has hit and destroyed the ecosystem!");
}
public static void FacadeExample1()
{
bool meteor = false;
var soil = new Soil(1);
while (!meteor)
{
var plant = new Plant(soil);
var herbivore = new Herbivore(plant);
var carnivore = new Carnivore(herbivore);
carnivore.Die(soil);
Console.WriteLine("Has a meteor hit? (Y/N)");
var response = Console.ReadLine();
if (response == "Y")
{
meteor = true;
}
}
Console.WriteLine("A meteor has hit and destroyed the ecosystem!");
}
public static void AdapterExample()
{
var triceratops = new Triceratops();
var child = ChildCreator.CreateChild(new TriceratopsToMammalAdapter(triceratops));
child.Cry();
}
public static void DecoratorExample()
{
var gigantosaurus = new Gigantosaurus();
Console.WriteLine(gigantosaurus.Roar());
var loudGigantosaurus = new LoudGigantosarusDecorator(gigantosaurus);
var extraLoudGigantosaurus = new ExtraLoudGigantosaurusDecorator(loudGigantosaurus);
Console.WriteLine(extraLoudGigantosaurus.Roar());
}
public static void AbstractFactoryExamples(string dinosaurType)
{
IDinosaurNoiseProducer af1 = new AbstractFactory.DinosaurNoiseProducer(new DinosaurFactory(dinosaurType));
af1.MakeADinosaurNoise();
}
public static void FactoryMethodExamples()
{
IDinosaurNoiseProducer fm1 = new TRexNoiseProducer();
fm1.MakeADinosaurNoise();
IDinosaurNoiseProducer fm2 = new StegosaurusNoiseProducer();
fm2.MakeADinosaurNoise();
}
static void CreateAMealExample()
{
var meal = new Meal();
meal.Starter = "a few green leaves";
meal.MainCourse = "a huge plate of ferns";
meal.SetDessert("fruit and berries");
meal.ServeMeal();
}
public static void BuilderExample()
{
MealBuilder builder = new StegosaurusMealBuilder();
Meal meal = builder.BuildMeal();
meal.ServeMeal();
}
public static void CompositeExample()
{
var plants = new List<IPlant>();
var branch = new Branch(new List<IPlant>() { new Leaf(), new Leaf() });
var anotherBranch = new Branch(new List<IPlant>() { new Leaf(), new Leaf(), new Leaf(), new Leaf() });
plants.Add(new Branch(
new List<IPlant>()
{ branch, anotherBranch }
));
plants.Add(new Leaf());
plants.Add(new Branch(new List<IPlant>() { new Leaf(), new Leaf(), new Leaf(), new Leaf(), new Leaf() }));
plants.Add(new Leaf());
foreach (IPlant plant in plants)
{
plant.Eat();
}
}
}
}