├── .gitignore
├── AbstractFactoryPattern
├── AbstractFactoryPattern.csproj
└── Program.cs
├── AdapterPattern
├── AdapterPattern.csproj
└── Program.cs
├── BridgePattern
├── BridgePattern.csproj
└── Program.cs
├── BuilderPattern
├── BuilderPattern.csproj
└── Program.cs
├── ChainOfResponsibilityPattern
├── ChainOfResponsibilityPattern.csproj
└── Program.cs
├── CommandPattern
├── CommandPattern.csproj
└── Program.cs
├── CompositePattern
├── CompositePattern.csproj
└── Program.cs
├── DecoratorPattern
├── DecoratorPattern.csproj
└── Program.cs
├── DesignPatterns.sln
├── FacadePattern
├── FacadePattern.csproj
└── Program.cs
├── FactoryMethodPattern
├── FactoryMethodPattern.csproj
└── Program.cs
├── FactoryPattern
├── FactoryPattern.csproj
└── Program.cs
├── FlyweightPattern
├── FlyweightPattern.csproj
└── Program.cs
├── IteratorPattern
├── IteratorPattern.csproj
└── Program.cs
├── LICENSE
├── MediatorPattern
├── MediatorPattern.csproj
└── Program.cs
├── Memento
├── MementoPattern.csproj
└── Program.cs
├── ObserverPattern
├── ObserverPattern.csproj
└── Program.cs
├── PrototypePattern
├── Program.cs
└── PrototypePattern.csproj
├── ProxyPattern
├── Program.cs
└── ProxyPattern.csproj
├── README.md
├── SingletonPattern
├── Program.cs
└── SingletonPattern.csproj
├── StatePattern
├── Program.cs
└── StatePattern.csproj
├── StrategyPattern
├── Program.cs
└── StrategyPattern.csproj
├── TemplateMethodPattern
├── Program.cs
└── TemplateMethodPattern.csproj
└── VisitorPattern
├── Program.cs
└── VisitorPattern.csproj
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/AbstractFactoryPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/AbstractFactoryPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AbstractFactoryPattern
4 | {
5 | interface IDoor
6 | {
7 |
8 | void GetDescription();
9 |
10 | }
11 | class WoodenDoor : IDoor
12 | {
13 | public void GetDescription()
14 | {
15 | Console.WriteLine("I am a wooden door");
16 | }
17 | }
18 |
19 | class IronDoor : IDoor
20 | {
21 | public void GetDescription()
22 | {
23 | Console.WriteLine("I am a iron door");
24 | }
25 | }
26 |
27 | interface IDoorFittingExpert
28 | {
29 | void GetDescription();
30 | }
31 |
32 | class Welder : IDoorFittingExpert
33 | {
34 | public void GetDescription()
35 | {
36 | Console.WriteLine("I can only fit iron doors");
37 | }
38 | }
39 |
40 | class Carpenter : IDoorFittingExpert
41 | {
42 | public void GetDescription()
43 | {
44 | Console.WriteLine("I can only fit wooden doors");
45 | }
46 | }
47 |
48 | interface IDoorFactory
49 | {
50 | IDoor MakeDoor();
51 | IDoorFittingExpert MakeFittingExpert();
52 | }
53 |
54 | // Wooden factory to return carpenter and wooden door
55 | class WoodenDoorFactory : IDoorFactory
56 | {
57 | public IDoor MakeDoor()
58 | {
59 | return new WoodenDoor();
60 | }
61 | // Iron door factory to get iron door and the relevant fitting expert
62 | public IDoorFittingExpert MakeFittingExpert()
63 | {
64 | return new Carpenter();
65 | }
66 | }
67 |
68 | class IronDoorFactory : IDoorFactory
69 | {
70 | public IDoor MakeDoor()
71 | {
72 | return new IronDoor();
73 | }
74 |
75 | public IDoorFittingExpert MakeFittingExpert()
76 | {
77 | return new Welder();
78 | }
79 | }
80 |
81 |
82 | class Program
83 | {
84 | static void Main(string[] args)
85 | {
86 | var woodenDoorFactory = new WoodenDoorFactory();
87 | var woodenDoor = woodenDoorFactory.MakeDoor();
88 | var woodenDoorFittingExpert = woodenDoorFactory.MakeFittingExpert();
89 |
90 | woodenDoor.GetDescription(); //Output : I am a wooden door
91 | woodenDoorFittingExpert.GetDescription();//Output : I can only fit woooden doors
92 |
93 | var ironDoorFactory = new IronDoorFactory();
94 | var ironDoor = ironDoorFactory.MakeDoor();
95 | var ironDoorFittingExpert = ironDoorFactory.MakeFittingExpert();
96 |
97 | ironDoor.GetDescription();//Output : I am a iron door
98 | ironDoorFittingExpert.GetDescription();//Output : I can only fit iron doors
99 |
100 | Console.ReadLine();
101 | }
102 | }
103 | }
104 |
--------------------------------------------------------------------------------
/AdapterPattern/AdapterPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/AdapterPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace AdapterPattern
4 | {
5 | interface ILion
6 | {
7 | void Roar();
8 | }
9 |
10 | class AfricanLion : ILion
11 | {
12 | public void Roar()
13 | {
14 |
15 | }
16 | }
17 |
18 | class AsiaLion : ILion
19 | {
20 | public void Roar()
21 | {
22 |
23 | }
24 | }
25 |
26 | // This needs to be added to the game
27 | class WildDog
28 | {
29 | public void bark()
30 | {
31 | }
32 | }
33 |
34 | // Adapter around wild dog to make it compatible with our game
35 | class WildDogAdapter : ILion
36 | {
37 | private WildDog mDog;
38 | public WildDogAdapter(WildDog dog)
39 | {
40 | this.mDog = dog;
41 | }
42 | public void Roar()
43 | {
44 | mDog.bark();
45 | }
46 | }
47 |
48 | class Hunter
49 | {
50 | public void Hunt(ILion lion)
51 | {
52 |
53 | }
54 | }
55 |
56 | class Program
57 | {
58 | static void Main(string[] args)
59 | {
60 | var wildDog = new WildDog();
61 | var wildDogAdapter = new WildDogAdapter(wildDog);
62 |
63 | var hunter = new Hunter();
64 | hunter.Hunt(wildDogAdapter);
65 |
66 | Console.ReadLine();
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/BridgePattern/BridgePattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/BridgePattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace BridgePattern
4 | {
5 | class Program
6 | {
7 | interface IWebPage
8 | {
9 | string GetContent();
10 | }
11 |
12 | class About : IWebPage
13 | {
14 | protected ITheme theme;
15 |
16 | public About(ITheme theme)
17 | {
18 | this.theme = theme;
19 | }
20 |
21 | public string GetContent()
22 | {
23 | return String.Format("About page in {0}", theme.GetColor());
24 | }
25 | }
26 |
27 | class Careers : IWebPage
28 | {
29 | protected ITheme theme;
30 |
31 | public Careers(ITheme theme)
32 | {
33 | this.theme = theme;
34 | }
35 |
36 | public string GetContent()
37 | {
38 | return String.Format("Careers page in {0}", theme.GetColor());
39 | }
40 | }
41 |
42 | interface ITheme
43 | {
44 | string GetColor();
45 | }
46 |
47 | class DarkTheme : ITheme
48 | {
49 | public string GetColor()
50 | {
51 | return "Dark Black";
52 | }
53 | }
54 |
55 | class LightTheme : ITheme
56 | {
57 | public string GetColor()
58 | {
59 | return "Off White";
60 | }
61 | }
62 |
63 | class AquaTheme : ITheme
64 | {
65 | public string GetColor()
66 | {
67 | return "Light blue";
68 | }
69 | }
70 | static void Main(string[] args)
71 | {
72 | var darkTheme = new DarkTheme();
73 |
74 | var about = new About(darkTheme);
75 | var careers = new Careers(darkTheme);
76 |
77 | Console.WriteLine(about.GetContent());
78 | Console.WriteLine(careers.GetContent());
79 | Console.ReadLine();
80 | }
81 | }
82 | }
83 |
--------------------------------------------------------------------------------
/BuilderPattern/BuilderPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/BuilderPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 |
4 | namespace BuilderPattern
5 | {
6 | class Program
7 | {
8 | class Burger
9 | {
10 | private int mSize;
11 | private bool mCheese;
12 | private bool mPepperoni;
13 | private bool mLettuce;
14 | private bool mTomato;
15 |
16 | public Burger(BurgerBuilder builder)
17 | {
18 | this.mSize = builder.Size;
19 | this.mCheese = builder.Cheese;
20 | this.mPepperoni = builder.Pepperoni;
21 | this.mLettuce = builder.Lettuce;
22 | this.mTomato = builder.Tomato;
23 | }
24 |
25 | public string GetDescription()
26 | {
27 | var sb = new StringBuilder();
28 | sb.Append(String.Format("This is {0} inch Burger. ", this.mSize));
29 | return sb.ToString();
30 | }
31 | }
32 |
33 | class BurgerBuilder {
34 | public int Size;
35 | public bool Cheese;
36 | public bool Pepperoni;
37 | public bool Lettuce;
38 | public bool Tomato;
39 |
40 | public BurgerBuilder(int size)
41 | {
42 | this.Size = size;
43 | }
44 |
45 | public BurgerBuilder AddCheese()
46 | {
47 | this.Cheese = true;
48 | return this;
49 | }
50 |
51 | public BurgerBuilder AddPepperoni()
52 | {
53 | this.Pepperoni = true;
54 | return this;
55 | }
56 |
57 | public BurgerBuilder AddLettuce()
58 | {
59 | this.Lettuce = true;
60 | return this;
61 | }
62 |
63 | public BurgerBuilder AddTomato()
64 | {
65 | this.Tomato = true;
66 | return this;
67 | }
68 |
69 | public Burger Build()
70 | {
71 | return new Burger(this);
72 | }
73 | }
74 |
75 | static void Main(string[] args)
76 | {
77 | var burger = new BurgerBuilder(4).AddCheese()
78 | .AddPepperoni()
79 | .AddLettuce()
80 | .AddTomato()
81 | .Build();
82 | Console.WriteLine(burger.GetDescription());
83 | Console.ReadLine();
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/ChainOfResponsibilityPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ChainOfResponsibilityPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ChainOfResponsibilityPattern
4 | {
5 | abstract class Account
6 | {
7 | private Account mSuccessor;
8 | protected decimal mBalance;
9 |
10 | public void SetNext(Account account)
11 | {
12 | mSuccessor = account;
13 | }
14 |
15 | public void Pay(decimal amountTopay)
16 | {
17 | if (CanPay(amountTopay))
18 | {
19 | Console.WriteLine("Paid {0:c} using {1}.", amountTopay, this.GetType().Name);
20 | }
21 | else if (this.mSuccessor != null)
22 | {
23 | Console.WriteLine("Cannot pay using {0}. Proceeding..", this.GetType().Name);
24 | mSuccessor.Pay(amountTopay);
25 | }
26 | else
27 | {
28 | throw new Exception("None of the accounts have enough balance");
29 | }
30 | }
31 | private bool CanPay(decimal amount)
32 | {
33 | return mBalance >= amount ? true : false;
34 | }
35 | }
36 |
37 | class Bank : Account
38 | {
39 | public Bank(decimal balance)
40 | {
41 | this.mBalance = balance;
42 | }
43 | }
44 |
45 | class Paypal : Account
46 | {
47 | public Paypal(decimal balance)
48 | {
49 | this.mBalance = balance;
50 | }
51 | }
52 |
53 | class Bitcoin : Account
54 | {
55 | public Bitcoin(decimal balance)
56 | {
57 | this.mBalance = balance;
58 | }
59 | }
60 | class Program
61 | {
62 | static void Main(string[] args)
63 | {
64 | // Let's prepare a chain like below
65 | // $bank->$paypal->$bitcoin
66 | //
67 | // First priority bank
68 | // If bank can't pay then paypal
69 | // If paypal can't pay then bit coin
70 | var bank = new Bank(100); // Bank with balance 100
71 | var paypal = new Paypal(200); // Paypal with balance 200
72 | var bitcoin = new Bitcoin(300); // Bitcoin with balance 300
73 |
74 | bank.SetNext(paypal);
75 | paypal.SetNext(bitcoin);
76 |
77 | // Let's try to pay using the first priority i.e. bank
78 | bank.Pay(259);
79 | // Output will be
80 | // ==============
81 | // Cannot pay using bank. Proceeding ..
82 | // Cannot pay using paypal. Proceeding ..:
83 | // Paid 259 using Bitcoin!
84 |
85 | Console.ReadLine();
86 | }
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/CommandPattern/CommandPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CommandPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace CommandPattern
4 | {
5 | class Program
6 | {
7 | // Receiver
8 | class Bulb
9 | {
10 | public void TurnOn()
11 | {
12 | Console.WriteLine("Bulb has been lit");
13 | }
14 |
15 | public void TurnOff()
16 | {
17 | Console.WriteLine("Darkness!");
18 | }
19 | }
20 |
21 | interface ICommand
22 | {
23 | void Execute();
24 | void Undo();
25 | void Redo();
26 | }
27 |
28 | // Command
29 | class TurnOn : ICommand
30 | {
31 | private Bulb mBulb;
32 |
33 | public TurnOn(Bulb bulb)
34 | {
35 | mBulb = bulb ?? throw new ArgumentNullException("Bulb", "Bulb cannot be null");
36 | }
37 |
38 | public void Execute()
39 | {
40 | mBulb.TurnOn();
41 | }
42 |
43 | public void Undo()
44 | {
45 | mBulb.TurnOff();
46 | }
47 |
48 | public void Redo()
49 | {
50 | Execute();
51 | }
52 | }
53 |
54 | class TurnOff : ICommand
55 | {
56 | private Bulb mBulb;
57 |
58 | public TurnOff(Bulb bulb)
59 | {
60 | mBulb = bulb ?? throw new ArgumentNullException("Bulb", "Bulb cannot be null");
61 | }
62 |
63 | public void Execute()
64 | {
65 | mBulb.TurnOff();
66 | }
67 |
68 | public void Undo()
69 | {
70 | mBulb.TurnOn();
71 | }
72 |
73 | public void Redo()
74 | {
75 | Execute();
76 | }
77 | }
78 |
79 | // Invoker
80 | class RemoteControl
81 | {
82 | public void Submit(ICommand command)
83 | {
84 | command.Execute();
85 | }
86 | }
87 |
88 | static void Main(string[] args)
89 | {
90 | var bulb = new Bulb();
91 |
92 | var turnOn = new TurnOn(bulb);
93 | var turnOff = new TurnOff(bulb);
94 |
95 | var remote = new RemoteControl();
96 | remote.Submit(turnOn); // Bulb has been lit!
97 | remote.Submit(turnOff); // Darkness!
98 |
99 | Console.ReadLine();
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/CompositePattern/CompositePattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/CompositePattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace CompositePattern
5 | {
6 | interface IEmployee
7 | {
8 | float GetSalary();
9 | string GetRole();
10 | string GetName();
11 | }
12 |
13 |
14 | class Developer : IEmployee
15 | {
16 | private string mName;
17 | private float mSalary;
18 |
19 | public Developer(string name, float salary)
20 | {
21 | this.mName = name;
22 | this.mSalary = salary;
23 | }
24 |
25 | public float GetSalary()
26 | {
27 | return this.mSalary;
28 | }
29 |
30 | public string GetRole()
31 | {
32 | return "Developer";
33 | }
34 |
35 | public string GetName()
36 | {
37 | return this.mName;
38 | }
39 | }
40 |
41 | class Designer : IEmployee
42 | {
43 | private string mName;
44 | private float mSalary;
45 |
46 | public Designer(string name, float salary)
47 | {
48 | this.mName = name;
49 | this.mSalary = salary;
50 | }
51 |
52 | public float GetSalary()
53 | {
54 | return this.mSalary;
55 | }
56 |
57 | public string GetRole()
58 | {
59 | return "Designer";
60 | }
61 |
62 | public string GetName()
63 | {
64 | return this.mName;
65 | }
66 | }
67 | class Organization
68 | {
69 | protected List employees;
70 |
71 | public Organization()
72 | {
73 | employees = new List();
74 | }
75 |
76 | public void AddEmployee(IEmployee employee)
77 | {
78 | employees.Add(employee);
79 | }
80 |
81 | public float GetNetSalaries()
82 | {
83 | float netSalary = 0;
84 |
85 | foreach (var e in employees) {
86 | netSalary += e.GetSalary();
87 | }
88 | return netSalary;
89 | }
90 | }
91 | class Program
92 | {
93 | static void Main(string[] args)
94 | {
95 | var developer = new Developer("John", 5000);
96 | var designer = new Designer("Arya", 5000);
97 |
98 | var organization = new Organization();
99 | organization.AddEmployee(developer);
100 | organization.AddEmployee(designer);
101 | Console.WriteLine("Net Salary of Emmployees in Organization is {0:c}", organization.GetNetSalaries());
102 | Console.ReadLine();
103 | }
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/DecoratorPattern/DecoratorPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/DecoratorPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace DecoratorPattern
4 | {
5 | interface ICoffee
6 | {
7 | int GetCost();
8 | string GetDescription();
9 | }
10 |
11 | class SimpleCoffee : ICoffee
12 | {
13 | public int GetCost()
14 | {
15 | return 5;
16 | }
17 |
18 | public string GetDescription()
19 | {
20 | return "Simple Coffee";
21 | }
22 | }
23 |
24 | class MilkCoffee : ICoffee
25 | {
26 | private readonly ICoffee mCoffee;
27 |
28 | public MilkCoffee(ICoffee coffee)
29 | {
30 | mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
31 | }
32 | public int GetCost()
33 | {
34 | return mCoffee.GetCost() + 1;
35 | }
36 |
37 | public string GetDescription()
38 | {
39 | return String.Concat(mCoffee.GetDescription(), ", milk");
40 | }
41 | }
42 |
43 | class WhipCoffee : ICoffee
44 | {
45 | private readonly ICoffee mCoffee;
46 |
47 | public WhipCoffee(ICoffee coffee)
48 | {
49 | mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
50 | }
51 | public int GetCost()
52 | {
53 | return mCoffee.GetCost() + 1;
54 | }
55 |
56 | public string GetDescription()
57 | {
58 | return String.Concat(mCoffee.GetDescription(), ", whip");
59 | }
60 | }
61 |
62 | class VanillaCoffee : ICoffee
63 | {
64 | private readonly ICoffee mCoffee;
65 |
66 | public VanillaCoffee(ICoffee coffee)
67 | {
68 | mCoffee = coffee ?? throw new ArgumentNullException("coffee", "coffee should not be null");
69 | }
70 | public int GetCost()
71 | {
72 | return mCoffee.GetCost() + 1;
73 | }
74 |
75 | public string GetDescription()
76 | {
77 | return String.Concat(mCoffee.GetDescription(), ", vanilla");
78 | }
79 | }
80 | class Program
81 | {
82 | static void Main(string[] args)
83 | {
84 | var myCoffee = new SimpleCoffee();
85 | Console.WriteLine("{0:c}",myCoffee.GetCost()); // $ 5.00
86 | Console.WriteLine("{0}", myCoffee.GetDescription()); // Simple Coffee
87 |
88 | var milkCoffee = new MilkCoffee(myCoffee);
89 | Console.WriteLine("{0:c}", milkCoffee.GetCost()); // $ 6.00
90 | Console.WriteLine("{0}", milkCoffee.GetDescription()); // Simple Coffee, milk
91 |
92 | var whipCoffee = new WhipCoffee(milkCoffee);
93 | Console.WriteLine("{0:c}", whipCoffee.GetCost()); // $ 7.00
94 | Console.WriteLine("{0}", whipCoffee.GetDescription()); // Simple Coffee, milk, whip
95 |
96 | var vanillaCoffee = new VanillaCoffee(whipCoffee);
97 | Console.WriteLine("{0:c}", vanillaCoffee.GetCost()); // $ 8.00
98 | Console.WriteLine("{0}", vanillaCoffee.GetDescription()); // Simple Coffee, milk, whip
99 | Console.ReadLine();
100 | }
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/DesignPatterns.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 15
4 | VisualStudioVersion = 15.0.27004.2009
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryPattern", "FactoryPattern\FactoryPattern.csproj", "{EC684E1B-EA3F-47FD-8F45-175C56667F13}"
7 | EndProject
8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryMethodPattern", "FactoryMethodPattern\FactoryMethodPattern.csproj", "{493DBC90-47D2-49F5-906D-14F3AFDDF9F8}"
9 | EndProject
10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactoryPattern", "AbstractFactoryPattern\AbstractFactoryPattern.csproj", "{081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}"
11 | EndProject
12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BuilderPattern", "BuilderPattern\BuilderPattern.csproj", "{A2804858-D1F9-4849-8C7E-1AB4DA7F979E}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SingletonPattern", "SingletonPattern\SingletonPattern.csproj", "{A61E958E-B6C9-413F-BACA-EA23D38A7DF2}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AdapterPattern", "AdapterPattern\AdapterPattern.csproj", "{89CDD96A-D34A-41E8-A0A3-7B47C67C56CF}"
17 | EndProject
18 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BridgePattern", "BridgePattern\BridgePattern.csproj", "{8F9283E5-7EE1-4125-9CFB-FB6C380240D5}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompositePattern", "CompositePattern\CompositePattern.csproj", "{3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}"
21 | EndProject
22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FacadePattern", "FacadePattern\FacadePattern.csproj", "{8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DecoratorPattern", "DecoratorPattern\DecoratorPattern.csproj", "{E16867D5-A235-437D-ADBF-8A4418E7038B}"
25 | EndProject
26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FlyweightPattern", "FlyweightPattern\FlyweightPattern.csproj", "{FB1E171E-9C39-4EF1-9377-9786AF3C61DA}"
27 | EndProject
28 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ProxyPattern", "ProxyPattern\ProxyPattern.csproj", "{CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibilityPattern", "ChainOfResponsibilityPattern\ChainOfResponsibilityPattern.csproj", "{E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}"
31 | EndProject
32 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CommandPattern", "CommandPattern\CommandPattern.csproj", "{25E92DA1-1205-4D53-85F4-B77BC92DCF5A}"
33 | EndProject
34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IteratorPattern", "IteratorPattern\IteratorPattern.csproj", "{BB1AE146-315A-44F8-A90B-77E386C65678}"
35 | EndProject
36 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MediatorPattern", "MediatorPattern\MediatorPattern.csproj", "{A6278444-3DDF-4175-996C-361E536668FC}"
37 | EndProject
38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MementoPattern", "Memento\MementoPattern.csproj", "{93F0318A-696F-4C09-800F-7091BAEC2178}"
39 | EndProject
40 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ObserverPattern", "ObserverPattern\ObserverPattern.csproj", "{88754C6C-4DA4-4C4B-8E53-4DDD9425A387}"
41 | EndProject
42 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VisitorPattern", "VisitorPattern\VisitorPattern.csproj", "{C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}"
43 | EndProject
44 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrategyPattern", "StrategyPattern\StrategyPattern.csproj", "{FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}"
45 | EndProject
46 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StatePattern", "StatePattern\StatePattern.csproj", "{606D9229-082C-4A71-8DD3-1918A14B7CF2}"
47 | EndProject
48 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TemplateMethodPattern", "TemplateMethodPattern\TemplateMethodPattern.csproj", "{611F5464-A5AD-4B27-86B1-5BD5EE0B3897}"
49 | EndProject
50 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrototypePattern", "PrototypePattern\PrototypePattern.csproj", "{1E81873C-4A25-4F94-89B0-753C0FE4EB2F}"
51 | EndProject
52 | Global
53 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
54 | Debug|Any CPU = Debug|Any CPU
55 | Release|Any CPU = Release|Any CPU
56 | EndGlobalSection
57 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
58 | {EC684E1B-EA3F-47FD-8F45-175C56667F13}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
59 | {EC684E1B-EA3F-47FD-8F45-175C56667F13}.Debug|Any CPU.Build.0 = Debug|Any CPU
60 | {EC684E1B-EA3F-47FD-8F45-175C56667F13}.Release|Any CPU.ActiveCfg = Release|Any CPU
61 | {EC684E1B-EA3F-47FD-8F45-175C56667F13}.Release|Any CPU.Build.0 = Release|Any CPU
62 | {493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
63 | {493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
64 | {493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
65 | {493DBC90-47D2-49F5-906D-14F3AFDDF9F8}.Release|Any CPU.Build.0 = Release|Any CPU
66 | {081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
67 | {081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Debug|Any CPU.Build.0 = Debug|Any CPU
68 | {081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.ActiveCfg = Release|Any CPU
69 | {081F04C5-CF9E-4997-B04E-5F9FDCCEE90D}.Release|Any CPU.Build.0 = Release|Any CPU
70 | {A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
71 | {A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Debug|Any CPU.Build.0 = Debug|Any CPU
72 | {A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.ActiveCfg = Release|Any CPU
73 | {A2804858-D1F9-4849-8C7E-1AB4DA7F979E}.Release|Any CPU.Build.0 = Release|Any CPU
74 | {A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75 | {A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
76 | {A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
77 | {A61E958E-B6C9-413F-BACA-EA23D38A7DF2}.Release|Any CPU.Build.0 = Release|Any CPU
78 | {89CDD96A-D34A-41E8-A0A3-7B47C67C56CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
79 | {89CDD96A-D34A-41E8-A0A3-7B47C67C56CF}.Debug|Any CPU.Build.0 = Debug|Any CPU
80 | {89CDD96A-D34A-41E8-A0A3-7B47C67C56CF}.Release|Any CPU.ActiveCfg = Release|Any CPU
81 | {89CDD96A-D34A-41E8-A0A3-7B47C67C56CF}.Release|Any CPU.Build.0 = Release|Any CPU
82 | {8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
83 | {8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Debug|Any CPU.Build.0 = Debug|Any CPU
84 | {8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Release|Any CPU.ActiveCfg = Release|Any CPU
85 | {8F9283E5-7EE1-4125-9CFB-FB6C380240D5}.Release|Any CPU.Build.0 = Release|Any CPU
86 | {3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
87 | {3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Debug|Any CPU.Build.0 = Debug|Any CPU
88 | {3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Release|Any CPU.ActiveCfg = Release|Any CPU
89 | {3CFAA453-5386-4A9E-9DDA-20A0DC8F700D}.Release|Any CPU.Build.0 = Release|Any CPU
90 | {8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
91 | {8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Debug|Any CPU.Build.0 = Debug|Any CPU
92 | {8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Release|Any CPU.ActiveCfg = Release|Any CPU
93 | {8E91875D-E79F-4D16-9FFC-49BD5BE7DF03}.Release|Any CPU.Build.0 = Release|Any CPU
94 | {E16867D5-A235-437D-ADBF-8A4418E7038B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
95 | {E16867D5-A235-437D-ADBF-8A4418E7038B}.Debug|Any CPU.Build.0 = Debug|Any CPU
96 | {E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.ActiveCfg = Release|Any CPU
97 | {E16867D5-A235-437D-ADBF-8A4418E7038B}.Release|Any CPU.Build.0 = Release|Any CPU
98 | {FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
99 | {FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
100 | {FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
101 | {FB1E171E-9C39-4EF1-9377-9786AF3C61DA}.Release|Any CPU.Build.0 = Release|Any CPU
102 | {CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
103 | {CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
104 | {CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
105 | {CE4E3751-C4D9-440E-9E25-3E7EA61F4A7D}.Release|Any CPU.Build.0 = Release|Any CPU
106 | {E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
107 | {E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
108 | {E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
109 | {E94F9FB8-B495-4A85-BAB4-0B08C69BA2D9}.Release|Any CPU.Build.0 = Release|Any CPU
110 | {25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111 | {25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Debug|Any CPU.Build.0 = Debug|Any CPU
112 | {25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.ActiveCfg = Release|Any CPU
113 | {25E92DA1-1205-4D53-85F4-B77BC92DCF5A}.Release|Any CPU.Build.0 = Release|Any CPU
114 | {BB1AE146-315A-44F8-A90B-77E386C65678}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115 | {BB1AE146-315A-44F8-A90B-77E386C65678}.Debug|Any CPU.Build.0 = Debug|Any CPU
116 | {BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.ActiveCfg = Release|Any CPU
117 | {BB1AE146-315A-44F8-A90B-77E386C65678}.Release|Any CPU.Build.0 = Release|Any CPU
118 | {A6278444-3DDF-4175-996C-361E536668FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119 | {A6278444-3DDF-4175-996C-361E536668FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
120 | {A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
121 | {A6278444-3DDF-4175-996C-361E536668FC}.Release|Any CPU.Build.0 = Release|Any CPU
122 | {93F0318A-696F-4C09-800F-7091BAEC2178}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123 | {93F0318A-696F-4C09-800F-7091BAEC2178}.Debug|Any CPU.Build.0 = Debug|Any CPU
124 | {93F0318A-696F-4C09-800F-7091BAEC2178}.Release|Any CPU.ActiveCfg = Release|Any CPU
125 | {93F0318A-696F-4C09-800F-7091BAEC2178}.Release|Any CPU.Build.0 = Release|Any CPU
126 | {88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127 | {88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Debug|Any CPU.Build.0 = Debug|Any CPU
128 | {88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Release|Any CPU.ActiveCfg = Release|Any CPU
129 | {88754C6C-4DA4-4C4B-8E53-4DDD9425A387}.Release|Any CPU.Build.0 = Release|Any CPU
130 | {C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131 | {C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
132 | {C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
133 | {C33B3A5D-E92B-455B-A9CD-4DFB6DF985A6}.Release|Any CPU.Build.0 = Release|Any CPU
134 | {FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135 | {FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Debug|Any CPU.Build.0 = Debug|Any CPU
136 | {FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.ActiveCfg = Release|Any CPU
137 | {FCF7B59D-63F0-4BE3-AE39-3EA7D562A67E}.Release|Any CPU.Build.0 = Release|Any CPU
138 | {606D9229-082C-4A71-8DD3-1918A14B7CF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
139 | {606D9229-082C-4A71-8DD3-1918A14B7CF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
140 | {606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
141 | {606D9229-082C-4A71-8DD3-1918A14B7CF2}.Release|Any CPU.Build.0 = Release|Any CPU
142 | {611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
143 | {611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Debug|Any CPU.Build.0 = Debug|Any CPU
144 | {611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Release|Any CPU.ActiveCfg = Release|Any CPU
145 | {611F5464-A5AD-4B27-86B1-5BD5EE0B3897}.Release|Any CPU.Build.0 = Release|Any CPU
146 | {1E81873C-4A25-4F94-89B0-753C0FE4EB2F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
147 | {1E81873C-4A25-4F94-89B0-753C0FE4EB2F}.Debug|Any CPU.Build.0 = Debug|Any CPU
148 | {1E81873C-4A25-4F94-89B0-753C0FE4EB2F}.Release|Any CPU.ActiveCfg = Release|Any CPU
149 | {1E81873C-4A25-4F94-89B0-753C0FE4EB2F}.Release|Any CPU.Build.0 = Release|Any CPU
150 | EndGlobalSection
151 | GlobalSection(SolutionProperties) = preSolution
152 | HideSolutionNode = FALSE
153 | EndGlobalSection
154 | GlobalSection(ExtensibilityGlobals) = postSolution
155 | SolutionGuid = {1F186729-EAD1-4045-8DAF-5C914B4D4824}
156 | EndGlobalSection
157 | EndGlobal
158 |
--------------------------------------------------------------------------------
/FacadePattern/FacadePattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/FacadePattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FacadePattern
4 | {
5 | class Computer
6 | {
7 | public void GetElectricShock()
8 | {
9 | Console.Write("Ouch!");
10 | }
11 |
12 | public void MakeSound()
13 | {
14 | Console.Write("Beep beep!");
15 | }
16 |
17 | public void ShowLoadingScreen()
18 | {
19 | Console.Write("Loading..");
20 | }
21 |
22 | public void Bam()
23 | {
24 | Console.Write("Ready to be used!");
25 | }
26 |
27 | public void CloseEverything()
28 | {
29 | Console.Write("Bup bup bup buzzzz!");
30 | }
31 |
32 | public void Sooth()
33 | {
34 | Console.Write("Zzzzz");
35 | }
36 |
37 | public void PullCurrent()
38 | {
39 | Console.Write("Haaah!");
40 | }
41 | }
42 |
43 | class ComputerFacade
44 | {
45 | private readonly Computer mComputer;
46 |
47 | public ComputerFacade(Computer computer)
48 | {
49 | this.mComputer = computer ?? throw new ArgumentNullException("computer", "computer cannot be null");
50 | }
51 |
52 | public void TurnOn()
53 | {
54 | mComputer.GetElectricShock();
55 | mComputer.MakeSound();
56 | mComputer.ShowLoadingScreen();
57 | mComputer.Bam();
58 | }
59 |
60 | public void TurnOff()
61 | {
62 | mComputer.CloseEverything();
63 | mComputer.PullCurrent();
64 | mComputer.Sooth();
65 | }
66 | }
67 | class Program
68 | {
69 | static void Main(string[] args)
70 | {
71 | var computer = new ComputerFacade(new Computer());
72 | computer.TurnOn(); // Ouch! Beep beep! Loading.. Ready to be used!
73 | Console.WriteLine();
74 | computer.TurnOff(); // Bup bup buzzz! Haah! Zzzzz
75 | Console.ReadLine();
76 | }
77 | }
78 | }
79 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/FactoryMethodPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/FactoryMethodPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FactoryMethodPattern
4 | {
5 | interface IInterviewer
6 | {
7 | void AskQuestions();
8 | }
9 |
10 | class Developer : IInterviewer
11 | {
12 | public void AskQuestions()
13 | {
14 | Console.WriteLine("Asking about design patterns!");
15 | }
16 | }
17 |
18 | class CommunityExecutive : IInterviewer
19 | {
20 | public void AskQuestions()
21 | {
22 | Console.WriteLine("Asking about community building!");
23 | }
24 | }
25 |
26 | abstract class HiringManager
27 | {
28 | // Factory method
29 | abstract protected IInterviewer MakeInterviewer();
30 | public void TakeInterview()
31 | {
32 | var interviewer = this.MakeInterviewer();
33 | interviewer.AskQuestions();
34 | }
35 | }
36 |
37 | class DevelopmentManager : HiringManager
38 | {
39 | protected override IInterviewer MakeInterviewer()
40 | {
41 | return new Developer();
42 | }
43 | }
44 |
45 | class MarketingManager : HiringManager
46 | {
47 | protected override IInterviewer MakeInterviewer()
48 | {
49 | return new CommunityExecutive();
50 | }
51 | }
52 |
53 | class Program
54 | {
55 | static void Main(string[] args)
56 | {
57 | var devManager = new DevelopmentManager();
58 | devManager.TakeInterview(); //Output : Asking about design patterns!
59 |
60 | var marketingManager = new MarketingManager();
61 | marketingManager.TakeInterview();//Output : Asking about community building!
62 |
63 | Console.ReadLine();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/FactoryPattern/FactoryPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/FactoryPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace FactoryPattern
4 | {
5 | public interface IDoor
6 | {
7 | int GetHeight();
8 | int GetWidth();
9 | }
10 |
11 | public class WoodenDoor : IDoor
12 | {
13 | private int Height { get; set; }
14 | private int Width { get; set; }
15 |
16 | public WoodenDoor(int height, int width)
17 | {
18 | this.Height = height;
19 | this.Width = width;
20 | }
21 |
22 | public int GetHeight()
23 | {
24 | return this.Height;
25 | }
26 | public int GetWidth()
27 | {
28 | return this.Width;
29 | }
30 | }
31 |
32 | public static class DoorFactory
33 | {
34 | public static IDoor MakeDoor(int height, int width)
35 | {
36 | return new WoodenDoor(height, width);
37 | }
38 | }
39 | class Program
40 | {
41 | static void Main(string[] args)
42 | {
43 | var door = DoorFactory.MakeDoor(80, 30);
44 | Console.WriteLine("Height of Door : {0}", door.GetHeight());
45 | Console.WriteLine("Width of Door : {0}", door.GetWidth());
46 | Console.ReadLine();
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/FlyweightPattern/FlyweightPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/FlyweightPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace FlyweightPattern
5 | {
6 | class Program
7 | {
8 | // Anything that will be cached is flyweight.
9 | // Types of tea here will be flyweights.
10 | class KarakTea
11 | {
12 | }
13 |
14 | // Acts as a factory and saves the tea
15 | class TeaMaker
16 | {
17 | private Dictionary mAvailableTea = new Dictionary();
18 |
19 | public KarakTea Make(string preference)
20 | {
21 | if (!mAvailableTea.ContainsKey(preference))
22 | {
23 | mAvailableTea[preference] = new KarakTea();
24 | }
25 |
26 | return mAvailableTea[preference];
27 | }
28 | }
29 |
30 | class TeaShop
31 | {
32 | private Dictionary mOrders = new Dictionary();
33 | private readonly TeaMaker mTeaMaker;
34 |
35 | public TeaShop(TeaMaker teaMaker)
36 | {
37 | mTeaMaker = teaMaker ?? throw new ArgumentNullException("teaMaker", "teaMaker cannot be null");
38 | }
39 |
40 | public void TakeOrder(string teaType, int table)
41 | {
42 | mOrders[table] = mTeaMaker.Make(teaType);
43 | }
44 |
45 | public void Serve()
46 | {
47 | foreach (var table in mOrders.Keys)
48 | {
49 | Console.WriteLine("Serving Tea to table # {0}", table);
50 | }
51 | }
52 | }
53 | static void Main(string[] args)
54 | {
55 | var teaMaker = new TeaMaker();
56 | var teaShop = new TeaShop(teaMaker);
57 |
58 | teaShop.TakeOrder("less sugar", 1);
59 | teaShop.TakeOrder("more milk", 2);
60 | teaShop.TakeOrder("without sugar", 5);
61 |
62 | teaShop.Serve();
63 | Console.ReadLine();
64 | }
65 | }
66 | }
67 |
--------------------------------------------------------------------------------
/IteratorPattern/IteratorPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/IteratorPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 |
6 | namespace IteratorPattern
7 | {
8 | class RadioStation
9 | {
10 | private float mFrequency;
11 |
12 | public RadioStation(float frequency)
13 | {
14 | mFrequency = frequency;
15 | }
16 |
17 | public float GetFrequecy()
18 | {
19 | return mFrequency;
20 | }
21 |
22 | }
23 |
24 | class StationList : IEnumerable
25 | {
26 | List mStations = new List();
27 |
28 | public RadioStation this[int index]
29 | {
30 | get { return mStations[index]; }
31 | set { mStations.Insert(index, value); }
32 | }
33 |
34 | public void Add(RadioStation station)
35 | {
36 | mStations.Add(station);
37 | }
38 |
39 | public void Remove(RadioStation station)
40 | {
41 | mStations.Remove(station);
42 | }
43 |
44 | public IEnumerator GetEnumerator()
45 | {
46 | return this.GetEnumerator();
47 | }
48 |
49 | IEnumerator IEnumerable.GetEnumerator()
50 | {
51 | //Use can switch to this internal collection if you do not want to transform
52 | return mStations.GetEnumerator();
53 |
54 | //use this if you want to transform the object before rendering
55 | //foreach (var x in mStations)
56 | //{
57 | // yield return x;
58 | //}
59 | }
60 | }
61 |
62 | class Program
63 | {
64 | static void Main(string[] args)
65 | {
66 | var stations = new StationList();
67 | var station1 = new RadioStation(89);
68 | stations.Add(station1);
69 |
70 | var station2 = new RadioStation(101);
71 | stations.Add(station2);
72 |
73 | var station3 = new RadioStation(102);
74 | stations.Add(station3);
75 |
76 | foreach (var x in stations)
77 | {
78 | Console.Write(x.GetFrequecy());
79 | }
80 |
81 | var q = stations.Where(x => x.GetFrequecy() == 89).FirstOrDefault();
82 | Console.WriteLine(q.GetFrequecy());
83 |
84 | Console.ReadLine();
85 | }
86 | }
87 | }
88 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Anupavan Muddana
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/MediatorPattern/MediatorPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/MediatorPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MediatorPattern
4 | {
5 | interface IChatRoomMediator
6 | {
7 | void ShowMessage(User user, string message);
8 | }
9 |
10 | //Mediator
11 | class ChatRoom : IChatRoomMediator
12 | {
13 | public void ShowMessage(User user, string message)
14 | {
15 | Console.WriteLine("{0} [{1}]:{2}", DateTime.Now.ToString("MMMM dd, H:mm"), user.GetName(), message);
16 | }
17 | }
18 |
19 | class User
20 | {
21 | private string mName;
22 | private IChatRoomMediator mChatRoom;
23 |
24 | public User(string name, IChatRoomMediator chatroom)
25 | {
26 | mChatRoom = chatroom;
27 | mName = name;
28 | }
29 |
30 | public string GetName()
31 | {
32 | return mName;
33 | }
34 |
35 | public void Send(string message)
36 | {
37 | mChatRoom.ShowMessage(this, message);
38 | }
39 | }
40 |
41 |
42 | class Program
43 | {
44 | static void Main(string[] args)
45 | {
46 | var mediator = new ChatRoom();
47 |
48 | var john = new User("John", mediator);
49 | var jane = new User("Jane", mediator);
50 |
51 | john.Send("Hi there!");
52 | jane.Send("Hey!");
53 |
54 | //April 14, 20:05[John]:Hi there!
55 | //April 14, 20:05[Jane]:Hey!
56 |
57 | Console.ReadLine();
58 | }
59 | }
60 | }
61 |
--------------------------------------------------------------------------------
/Memento/MementoPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/Memento/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace MementoPattern
4 | {
5 | class EditorMemento
6 | {
7 | private string mContent;
8 |
9 | public EditorMemento(string content)
10 | {
11 | mContent = content;
12 | }
13 |
14 | public string Content
15 | {
16 | get
17 | {
18 | return mContent;
19 | }
20 | }
21 | }
22 |
23 | class Editor {
24 |
25 | private string mContent = string.Empty;
26 | private EditorMemento memento;
27 |
28 | public Editor()
29 | {
30 | memento = new EditorMemento(string.Empty);
31 | }
32 |
33 | public void Type(string words)
34 | {
35 | mContent = String.Concat(mContent," ", words);
36 | }
37 |
38 | public string Content
39 | {
40 | get
41 | {
42 | return mContent;
43 | }
44 | }
45 |
46 | public void Save()
47 | {
48 | memento = new EditorMemento(mContent);
49 | }
50 |
51 | public void Restore()
52 | {
53 | mContent = memento.Content;
54 | }
55 | }
56 |
57 |
58 | class Program
59 | {
60 | static void Main(string[] args)
61 | {
62 | var editor = new Editor();
63 |
64 | //Type some stuff
65 | editor.Type("This is the first sentence.");
66 | editor.Type("This is second.");
67 |
68 | // Save the state to restore to : This is the first sentence. This is second.
69 | editor.Save();
70 |
71 | //Type some more
72 | editor.Type("This is thrid.");
73 |
74 | //Output the content
75 | Console.WriteLine(editor.Content); // This is the first sentence. This is second. This is third.
76 |
77 | //Restoring to last saved state
78 | editor.Restore();
79 |
80 | Console.Write(editor.Content); // This is the first sentence. This is second
81 |
82 | Console.ReadLine();
83 | }
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/ObserverPattern/ObserverPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ObserverPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace ObserverPattern
5 | {
6 | class JobPost
7 | {
8 | public string Title { get; private set; }
9 |
10 | public JobPost(string title)
11 | {
12 | Title = title;
13 | }
14 | }
15 | class JobSeeker : IObserver
16 | {
17 | public string Name { get; private set; }
18 |
19 | public JobSeeker(string name)
20 | {
21 | Name = name;
22 | }
23 |
24 | //Method is not being called by JobPostings class currently
25 | public void OnCompleted()
26 | {
27 | //No Implementation
28 | }
29 |
30 | //Method is not being called by JobPostings class currently
31 | public void OnError(Exception error)
32 | {
33 | //No Implementation
34 | }
35 |
36 | public void OnNext(JobPost value)
37 | {
38 | Console.WriteLine("Hi {0} ! New job posted: {1}", Name, value.Title);
39 | }
40 | }
41 |
42 | class JobPostings : IObservable
43 | {
44 | private List> mObservers;
45 | private List mJobPostings;
46 |
47 | public JobPostings()
48 | {
49 | mObservers = new List>();
50 | mJobPostings = new List();
51 | }
52 |
53 | public IDisposable Subscribe(IObserver observer)
54 | {
55 | // Check whether observer is already registered. If not, add it
56 | if (!mObservers.Contains(observer))
57 | {
58 | mObservers.Add(observer);
59 | }
60 | return new Unsubscriber(mObservers, observer);
61 | }
62 |
63 | private void Notify(JobPost jobPost)
64 | {
65 | foreach (var observer in mObservers)
66 | {
67 | observer.OnNext(jobPost);
68 | }
69 | }
70 |
71 | public void AddJob(JobPost jobPost)
72 | {
73 | mJobPostings.Add(jobPost);
74 | Notify(jobPost);
75 | }
76 |
77 | }
78 |
79 | internal class Unsubscriber : IDisposable
80 | {
81 | private List> mObservers;
82 | private IObserver mObserver;
83 |
84 | internal Unsubscriber(List> observers, IObserver observer)
85 | {
86 | this.mObservers = observers;
87 | this.mObserver = observer;
88 | }
89 |
90 | public void Dispose()
91 | {
92 | if (mObservers.Contains(mObserver))
93 | mObservers.Remove(mObserver);
94 | }
95 | }
96 |
97 |
98 | class Program
99 | {
100 | static void Main(string[] args)
101 | {
102 | //Create Subscribers
103 | var johnDoe = new JobSeeker("John Doe");
104 | var janeDoe = new JobSeeker("Jane Doe");
105 |
106 | //Create publisher and attch subscribers
107 | var jobPostings = new JobPostings();
108 | jobPostings.Subscribe(johnDoe);
109 | jobPostings.Subscribe(janeDoe);
110 |
111 | //Add a new job and see if subscribers get notified
112 | jobPostings.AddJob(new JobPost("Software Engineer"));
113 |
114 | //Output
115 | // Hi John Doe! New job posted: Software Engineer
116 | // Hi Jane Doe! New job posted: Software Engineer
117 |
118 | Console.ReadLine();
119 | }
120 | }
121 | }
122 |
--------------------------------------------------------------------------------
/PrototypePattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace PrototypePattern
4 | {
5 | class Sheep
6 | {
7 | public string Name { get; set; }
8 |
9 | public string Category { get; set; }
10 |
11 | public Sheep(string name, string category)
12 | {
13 | Name = name;
14 | Category = category;
15 | }
16 |
17 | public Sheep Clone()
18 | {
19 | return MemberwiseClone() as Sheep;
20 | }
21 | }
22 |
23 | class Program
24 | {
25 | static void Main(string[] args)
26 | {
27 | var original = new Sheep("Jolly", "Mountain Sheep");
28 | Console.WriteLine(original.Name); // Jolly
29 | Console.WriteLine(original.Category); // Mountain Sheep
30 |
31 | var cloned = original.Clone();
32 | cloned.Name = "Dolly";
33 | Console.WriteLine(cloned.Name); // Dolly
34 | Console.WriteLine(cloned.Category); // Mountain Sheep
35 | Console.WriteLine(original.Name); // Dolly
36 |
37 | Console.ReadLine();
38 | }
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/PrototypePattern/PrototypePattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/ProxyPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace ProxyPattern
4 | {
5 | interface IDoor
6 | {
7 | void Open();
8 | void Close();
9 | }
10 |
11 | class LabDoor : IDoor
12 | {
13 | public void Close()
14 | {
15 | Console.WriteLine("Closing lab door");
16 | }
17 |
18 | public void Open()
19 | {
20 | Console.WriteLine("Opening lab door");
21 | }
22 | }
23 |
24 | class SecuredDoor
25 | {
26 | private IDoor mDoor;
27 |
28 | public SecuredDoor(IDoor door)
29 | {
30 | mDoor = door ?? throw new ArgumentNullException("door", "door can not be null");
31 | }
32 |
33 | public void Open(string password)
34 | {
35 | if (Authenticate(password))
36 | {
37 | mDoor.Open();
38 | }
39 | else
40 | {
41 | Console.WriteLine("Big no! It ain't possible.");
42 | }
43 | }
44 |
45 | private bool Authenticate(string password)
46 | {
47 | return password == "$ecr@t" ? true : false;
48 | }
49 |
50 | public void Close()
51 | {
52 | mDoor.Close();
53 | }
54 | }
55 | class Program
56 | {
57 | static void Main(string[] args)
58 | {
59 | var door = new SecuredDoor(new LabDoor());
60 | door.Open("invalid"); // Big no! It ain't possible.
61 |
62 | door.Open("$ecr@t"); // Opening lab door
63 | door.Close(); // Closing lab door
64 |
65 | Console.ReadLine();
66 | }
67 | }
68 | }
69 |
--------------------------------------------------------------------------------
/ProxyPattern/ProxyPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # csharp-design-patterns-for-humans-examples
2 | Complete C# Examples Refereed in csharp-design-patterns-for-humans
3 |
--------------------------------------------------------------------------------
/SingletonPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SingletonPattern
4 | {
5 | public class President
6 | {
7 | static President instance;
8 | // Private constructor
9 | private President()
10 | {
11 | //Hiding the Constructor
12 | }
13 |
14 | // Public constructor
15 | public static President get_instance()
16 | {
17 | if (instance == null) {
18 | instance = new President();
19 | }
20 | return instance;
21 | }
22 | }
23 | class Program
24 | {
25 | static void Main(string[] args)
26 | {
27 | President a = President.get_instance();
28 | President b = President.get_instance();
29 |
30 | Console.WriteLine((a==b).ToString());
31 | Console.ReadLine();
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/SingletonPattern/SingletonPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/StatePattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace StatePattern
4 | {
5 | interface IWritingState
6 | {
7 |
8 | void Write(string words);
9 |
10 | }
11 |
12 | class UpperCase : IWritingState
13 | {
14 | public void Write(string words)
15 | {
16 | Console.WriteLine(words.ToUpper());
17 | }
18 | }
19 |
20 | class LowerCase : IWritingState
21 | {
22 | public void Write(string words)
23 | {
24 | Console.WriteLine(words.ToLower());
25 | }
26 | }
27 |
28 | class DefaultText : IWritingState
29 | {
30 | public void Write(string words)
31 | {
32 | Console.WriteLine(words);
33 | }
34 | }
35 |
36 | class TextEditor
37 | {
38 |
39 | private IWritingState mState;
40 |
41 | public TextEditor()
42 | {
43 | mState = new DefaultText();
44 | }
45 |
46 | public void SetState(IWritingState state)
47 | {
48 | mState = state;
49 | }
50 |
51 | public void Type(string words)
52 | {
53 | mState.Write(words);
54 | }
55 |
56 | }
57 |
58 | class Program
59 | {
60 | static void Main(string[] args)
61 | {
62 | var editor = new TextEditor();
63 |
64 | editor.Type("First line");
65 |
66 | editor.SetState(new UpperCase());
67 |
68 | editor.Type("Second Line");
69 | editor.Type("Third Line");
70 |
71 | editor.SetState(new LowerCase());
72 |
73 | editor.Type("Fourth Line");
74 | editor.Type("Fifthe Line");
75 |
76 | // Output:
77 | // First line
78 | // SECOND LINE
79 | // THIRD LINE
80 | // fourth line
81 | // fifth line
82 |
83 | Console.ReadLine();
84 | }
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/StatePattern/StatePattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/StrategyPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace StrategyPattern
5 | {
6 | interface ISortStrategy
7 | {
8 | List Sort(List dataset);
9 | }
10 |
11 | class BubbleSortStrategy : ISortStrategy
12 | {
13 | public List Sort(List dataset)
14 | {
15 | Console.WriteLine("Sorting using Bubble Sort !");
16 | return dataset;
17 | }
18 | }
19 |
20 | class QuickSortStrategy : ISortStrategy
21 | {
22 | public List Sort(List dataset)
23 | {
24 | Console.WriteLine("Sorting using Quick Sort !");
25 | return dataset;
26 | }
27 | }
28 |
29 | class Sorter
30 | {
31 | private readonly ISortStrategy mSorter;
32 |
33 | public Sorter(ISortStrategy sorter)
34 | {
35 | mSorter = sorter;
36 | }
37 |
38 | public List Sort(List unSortedList)
39 | {
40 | return mSorter.Sort(unSortedList);
41 | }
42 | }
43 |
44 | class Program
45 | {
46 | static void Main(string[] args)
47 | {
48 | var unSortedList = new List { 1, 10, 2, 16, 19 };
49 |
50 | var sorter = new Sorter(new QuickSortStrategy());
51 | sorter.Sort(unSortedList); // // Output : Sorting using Bubble Sort !
52 |
53 | sorter = new Sorter(new QuickSortStrategy());
54 | sorter.Sort(unSortedList); // // Output : Sorting using Quick Sort !
55 |
56 | Console.ReadLine();
57 | }
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/StrategyPattern/StrategyPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace TemplateMethodPattern
4 | {
5 | abstract class Builder
6 | {
7 | // Template method
8 | public void Build()
9 | {
10 | Test();
11 | Lint();
12 | Assemble();
13 | Deploy();
14 | }
15 |
16 | abstract public void Test();
17 | abstract public void Lint();
18 | abstract public void Assemble();
19 | abstract public void Deploy();
20 | }
21 |
22 | class AndroidBuilder : Builder
23 | {
24 | public override void Assemble()
25 | {
26 | Console.WriteLine("Assembling the android build");
27 | }
28 |
29 | public override void Deploy()
30 | {
31 | Console.WriteLine("Deploying android build to server");
32 | }
33 |
34 | public override void Lint()
35 | {
36 | Console.WriteLine("Linting the android code");
37 | }
38 |
39 | public override void Test()
40 | {
41 | Console.WriteLine("Running android tests");
42 | }
43 | }
44 |
45 |
46 | class IosBuilder : Builder
47 | {
48 | public override void Assemble()
49 | {
50 | Console.WriteLine("Assembling the ios build");
51 | }
52 |
53 | public override void Deploy()
54 | {
55 | Console.WriteLine("Deploying ios build to server");
56 | }
57 |
58 | public override void Lint()
59 | {
60 | Console.WriteLine("Linting the ios code");
61 | }
62 |
63 | public override void Test()
64 | {
65 | Console.WriteLine("Running ios tests");
66 | }
67 | }
68 |
69 | class Program
70 | {
71 | static void Main(string[] args)
72 | {
73 | var androidBuilder = new AndroidBuilder();
74 | androidBuilder.Build();
75 |
76 | // Output:
77 | // Running android tests
78 | // Linting the android code
79 | // Assembling the android build
80 | // Deploying android build to server
81 |
82 | var iosBuilder = new IosBuilder();
83 | iosBuilder.Build();
84 |
85 | // Output:
86 | // Running ios tests
87 | // Linting the ios code
88 | // Assembling the ios build
89 | // Deploying ios build to server
90 |
91 | Console.ReadLine();
92 | }
93 | }
94 | }
95 |
--------------------------------------------------------------------------------
/TemplateMethodPattern/TemplateMethodPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/VisitorPattern/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace VisitorPattern
4 | {
5 | // Visitee
6 | interface IAnimal
7 | {
8 | void Accept(IAnimalOperation operation);
9 | }
10 |
11 | // Visitor
12 | interface IAnimalOperation
13 | {
14 | void VisitMonkey(Monkey monkey);
15 | void VisitLion(Lion lion);
16 | void VisitDolphin(Dolphin dolphin);
17 | }
18 |
19 | class Monkey : IAnimal
20 | {
21 | public void Shout()
22 | {
23 | Console.WriteLine("Oooh o aa aa!");
24 | }
25 |
26 | public void Accept(IAnimalOperation operation)
27 | {
28 | operation.VisitMonkey(this);
29 | }
30 | }
31 |
32 | class Lion : IAnimal
33 | {
34 | public void Roar()
35 | {
36 | Console.WriteLine("Roaar!");
37 | }
38 |
39 | public void Accept(IAnimalOperation operation)
40 | {
41 | operation.VisitLion(this);
42 | }
43 | }
44 |
45 | class Dolphin : IAnimal
46 | {
47 | public void Speak()
48 | {
49 | Console.WriteLine("Tuut tittu tuutt!");
50 | }
51 |
52 | public void Accept(IAnimalOperation operation)
53 | {
54 | operation.VisitDolphin(this);
55 | }
56 | }
57 |
58 | class Speak : IAnimalOperation
59 | {
60 | public void VisitDolphin(Dolphin dolphin)
61 | {
62 | dolphin.Speak();
63 | }
64 |
65 | public void VisitLion(Lion lion)
66 | {
67 | lion.Roar();
68 | }
69 |
70 | public void VisitMonkey(Monkey monkey)
71 | {
72 | monkey.Shout();
73 | }
74 | }
75 |
76 | class Jump : IAnimalOperation
77 | {
78 | public void VisitDolphin(Dolphin dolphin)
79 | {
80 | Console.WriteLine("Walked on water a little and disappeared!");
81 | }
82 |
83 | public void VisitLion(Lion lion)
84 | {
85 | Console.WriteLine("Jumped 7 feet! Back on the ground!");
86 | }
87 |
88 | public void VisitMonkey(Monkey monkey)
89 | {
90 | Console.WriteLine("Jumped 20 feet high! on to the tree!");
91 | }
92 | }
93 |
94 |
95 | class Program
96 | {
97 | static void Main(string[] args)
98 | {
99 | var monkey = new Monkey();
100 | var lion = new Lion();
101 | var dolphin = new Dolphin();
102 |
103 | var speak = new Speak();
104 |
105 | monkey.Accept(speak); // Ooh oo aa aa!
106 | lion.Accept(speak); // Roaaar!
107 | dolphin.Accept(speak); // Tuut tutt tuutt!
108 |
109 | var jump = new Jump();
110 |
111 | monkey.Accept(speak); // Ooh oo aa aa!
112 | monkey.Accept(jump); // Jumped 20 feet high! on to the tree!
113 |
114 | lion.Accept(speak); // Roaaar!
115 | lion.Accept(jump); // Jumped 7 feet! Back on the ground!
116 |
117 | dolphin.Accept(speak); // Tuut tutt tuutt!
118 | dolphin.Accept(jump); // Walked on water a little and disappeared
119 |
120 | Console.ReadLine();
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/VisitorPattern/VisitorPattern.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp2.0
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------