├── .gitignore
├── Finished sample
├── AbstractFactory
│ ├── AbstractFactory.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Adapter
│ ├── Adapter.csproj
│ ├── ClassAdapterImplementation.cs
│ ├── ObjectAdapterImplementation.cs
│ ├── Program.cs
│ └── Properties
│ │ └── launchSettings.json
├── Bridge
│ ├── Bridge.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Builder
│ ├── Builder.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── ChainOfResponsibility
│ ├── ChainOfResponsibility.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Command
│ ├── Command.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Composite
│ ├── Composite.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Decorator
│ ├── Decorator.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── DesignPatterns.sln
├── Facade
│ ├── Facade.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── FactoryMethod
│ ├── FactoryMethod.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Flyweight
│ ├── Flyweight.csproj
│ ├── Implementation.cs
│ └── Program.cs
├── Interpreter
│ ├── Implementation.cs
│ ├── Interpreter.csproj
│ └── Program.cs
├── Iterator
│ ├── Implementation.cs
│ ├── Iterator.csproj
│ └── Program.cs
├── Mediator
│ ├── Implementation.cs
│ ├── Mediator.csproj
│ └── Program.cs
├── Memento
│ ├── Implementation.cs
│ ├── Memento.csproj
│ └── Program.cs
├── Observer
│ ├── Implementation.cs
│ ├── Observer.csproj
│ └── Program.cs
├── Prototype
│ ├── Implementation.cs
│ ├── Program.cs
│ └── Prototype.csproj
├── Proxy
│ ├── Implementation.cs
│ ├── Program.cs
│ └── Proxy.csproj
├── Singleton
│ ├── Implementation.cs
│ ├── Program.cs
│ └── Singleton.csproj
├── State
│ ├── Implementation.cs
│ ├── Program.cs
│ └── State.csproj
├── Strategy
│ ├── Implementation.cs
│ ├── Program.cs
│ └── Strategy.csproj
├── TemplateMethod
│ ├── Implementation.cs
│ ├── Program.cs
│ └── TemplateMethod.csproj
└── Visitor
│ ├── Implementation.cs
│ ├── Program.cs
│ └── Visitor.csproj
├── README.md
└── Starter solution
├── AbstractFactory
├── AbstractFactory.csproj
├── Implementation.cs
└── Program.cs
├── Adapter
├── Adapter.csproj
├── Program.cs
└── Properties
│ └── launchSettings.json
├── Bridge
├── Bridge.csproj
├── Implementation.cs
└── Program.cs
├── Builder
├── Builder.csproj
├── Implementation.cs
└── Program.cs
├── ChainOfResponsibility
├── ChainOfResponsibility.csproj
├── Implementation.cs
└── Program.cs
├── Command
├── Command.csproj
├── Implementation.cs
└── Program.cs
├── Composite
├── Composite.csproj
├── Implementation.cs
└── Program.cs
├── Decorator
├── Decorator.csproj
├── Implementation.cs
└── Program.cs
├── DesignPatterns.sln
├── Facade
├── Facade.csproj
├── Implementation.cs
└── Program.cs
├── FactoryMethod
├── FactoryMethod.csproj
├── Implementation.cs
└── Program.cs
├── Flyweight
├── Flyweight.csproj
├── Implementation.cs
└── Program.cs
├── Interpreter
├── Implementation.cs
├── Interpreter.csproj
└── Program.cs
├── Iterator
├── Implementation.cs
├── Iterator.csproj
└── Program.cs
├── Mediator
├── Implementation.cs
├── Mediator.csproj
└── Program.cs
├── Memento
├── Implementation.cs
├── Memento.csproj
└── Program.cs
├── Observer
├── Implementation.cs
├── Observer.csproj
└── Program.cs
├── Prototype
├── Implementation.cs
├── Program.cs
└── Prototype.csproj
├── Proxy
├── Implementation.cs
├── Program.cs
└── Proxy.csproj
├── Singleton
├── Implementation.cs
├── Program.cs
└── Singleton.csproj
├── State
├── Implementation.cs
├── Program.cs
└── State.csproj
├── Strategy
├── Implementation.cs
├── Program.cs
└── Strategy.csproj
├── TemplateMethod
├── Implementation.cs
├── Program.cs
└── TemplateMethod.csproj
└── Visitor
├── Implementation.cs
├── Program.cs
└── Visitor.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 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Aa][Rr][Mm]/
27 | [Aa][Rr][Mm]64/
28 | bld/
29 | [Bb]in/
30 | [Oo]bj/
31 | [Ll]og/
32 | [Ll]ogs/
33 |
34 | # Visual Studio 2015/2017 cache/options directory
35 | .vs/
36 | # Uncomment if you have tasks that create the project's static files in wwwroot
37 | #wwwroot/
38 |
39 | # Visual Studio 2017 auto generated files
40 | Generated\ Files/
41 |
42 | # MSTest test Results
43 | [Tt]est[Rr]esult*/
44 | [Bb]uild[Ll]og.*
45 |
46 | # NUnit
47 | *.VisualState.xml
48 | TestResult.xml
49 | nunit-*.xml
50 |
51 | # Build Results of an ATL Project
52 | [Dd]ebugPS/
53 | [Rr]eleasePS/
54 | dlldata.c
55 |
56 | # Benchmark Results
57 | BenchmarkDotNet.Artifacts/
58 |
59 | # .NET Core
60 | project.lock.json
61 | project.fragment.lock.json
62 | artifacts/
63 |
64 | # StyleCop
65 | StyleCopReport.xml
66 |
67 | # Files built by Visual Studio
68 | *_i.c
69 | *_p.c
70 | *_h.h
71 | *.ilk
72 | *.meta
73 | *.obj
74 | *.iobj
75 | *.pch
76 | *.pdb
77 | *.ipdb
78 | *.pgc
79 | *.pgd
80 | *.rsp
81 | *.sbr
82 | *.tlb
83 | *.tli
84 | *.tlh
85 | *.tmp
86 | *.tmp_proj
87 | *_wpftmp.csproj
88 | *.log
89 | *.vspscc
90 | *.vssscc
91 | .builds
92 | *.pidb
93 | *.svclog
94 | *.scc
95 |
96 | # Chutzpah Test files
97 | _Chutzpah*
98 |
99 | # Visual C++ cache files
100 | ipch/
101 | *.aps
102 | *.ncb
103 | *.opendb
104 | *.opensdf
105 | *.sdf
106 | *.cachefile
107 | *.VC.db
108 | *.VC.VC.opendb
109 |
110 | # Visual Studio profiler
111 | *.psess
112 | *.vsp
113 | *.vspx
114 | *.sap
115 |
116 | # Visual Studio Trace Files
117 | *.e2e
118 |
119 | # TFS 2012 Local Workspace
120 | $tf/
121 |
122 | # Guidance Automation Toolkit
123 | *.gpState
124 |
125 | # ReSharper is a .NET coding add-in
126 | _ReSharper*/
127 | *.[Rr]e[Ss]harper
128 | *.DotSettings.user
129 |
130 | # TeamCity is a build add-in
131 | _TeamCity*
132 |
133 | # DotCover is a Code Coverage Tool
134 | *.dotCover
135 |
136 | # AxoCover is a Code Coverage Tool
137 | .axoCover/*
138 | !.axoCover/settings.json
139 |
140 | # Visual Studio code coverage results
141 | *.coverage
142 | *.coveragexml
143 |
144 | # NCrunch
145 | _NCrunch_*
146 | .*crunch*.local.xml
147 | nCrunchTemp_*
148 |
149 | # MightyMoose
150 | *.mm.*
151 | AutoTest.Net/
152 |
153 | # Web workbench (sass)
154 | .sass-cache/
155 |
156 | # Installshield output folder
157 | [Ee]xpress/
158 |
159 | # DocProject is a documentation generator add-in
160 | DocProject/buildhelp/
161 | DocProject/Help/*.HxT
162 | DocProject/Help/*.HxC
163 | DocProject/Help/*.hhc
164 | DocProject/Help/*.hhk
165 | DocProject/Help/*.hhp
166 | DocProject/Help/Html2
167 | DocProject/Help/html
168 |
169 | # Click-Once directory
170 | publish/
171 |
172 | # Publish Web Output
173 | *.[Pp]ublish.xml
174 | *.azurePubxml
175 | # Note: Comment the next line if you want to checkin your web deploy settings,
176 | # but database connection strings (with potential passwords) will be unencrypted
177 | *.pubxml
178 | *.publishproj
179 |
180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
181 | # checkin your Azure Web App publish settings, but sensitive information contained
182 | # in these scripts will be unencrypted
183 | PublishScripts/
184 |
185 | # NuGet Packages
186 | *.nupkg
187 | # NuGet Symbol Packages
188 | *.snupkg
189 | # The packages folder can be ignored because of Package Restore
190 | **/[Pp]ackages/*
191 | # except build/, which is used as an MSBuild target.
192 | !**/[Pp]ackages/build/
193 | # Uncomment if necessary however generally it will be regenerated when needed
194 | #!**/[Pp]ackages/repositories.config
195 | # NuGet v3's project.json files produces more ignorable files
196 | *.nuget.props
197 | *.nuget.targets
198 |
199 | # Microsoft Azure Build Output
200 | csx/
201 | *.build.csdef
202 |
203 | # Microsoft Azure Emulator
204 | ecf/
205 | rcf/
206 |
207 | # Windows Store app package directories and files
208 | AppPackages/
209 | BundleArtifacts/
210 | Package.StoreAssociation.xml
211 | _pkginfo.txt
212 | *.appx
213 | *.appxbundle
214 | *.appxupload
215 |
216 | # Visual Studio cache files
217 | # files ending in .cache can be ignored
218 | *.[Cc]ache
219 | # but keep track of directories ending in .cache
220 | !?*.[Cc]ache/
221 |
222 | # Others
223 | ClientBin/
224 | ~$*
225 | *~
226 | *.dbmdl
227 | *.dbproj.schemaview
228 | *.jfm
229 | *.pfx
230 | *.publishsettings
231 | orleans.codegen.cs
232 |
233 | # Including strong name files can present a security risk
234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
235 | #*.snk
236 |
237 | # Since there are multiple workflows, uncomment next line to ignore bower_components
238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
239 | #bower_components/
240 |
241 | # RIA/Silverlight projects
242 | Generated_Code/
243 |
244 | # Backup & report files from converting an old project file
245 | # to a newer Visual Studio version. Backup files are not needed,
246 | # because we have git ;-)
247 | _UpgradeReport_Files/
248 | Backup*/
249 | UpgradeLog*.XML
250 | UpgradeLog*.htm
251 | ServiceFabricBackup/
252 | *.rptproj.bak
253 |
254 | # SQL Server files
255 | *.mdf
256 | *.ldf
257 | *.ndf
258 |
259 | # Business Intelligence projects
260 | *.rdl.data
261 | *.bim.layout
262 | *.bim_*.settings
263 | *.rptproj.rsuser
264 | *- [Bb]ackup.rdl
265 | *- [Bb]ackup ([0-9]).rdl
266 | *- [Bb]ackup ([0-9][0-9]).rdl
267 |
268 | # Microsoft Fakes
269 | FakesAssemblies/
270 |
271 | # GhostDoc plugin setting file
272 | *.GhostDoc.xml
273 |
274 | # Node.js Tools for Visual Studio
275 | .ntvs_analysis.dat
276 | node_modules/
277 |
278 | # Visual Studio 6 build log
279 | *.plg
280 |
281 | # Visual Studio 6 workspace options file
282 | *.opt
283 |
284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
285 | *.vbw
286 |
287 | # Visual Studio LightSwitch build output
288 | **/*.HTMLClient/GeneratedArtifacts
289 | **/*.DesktopClient/GeneratedArtifacts
290 | **/*.DesktopClient/ModelManifest.xml
291 | **/*.Server/GeneratedArtifacts
292 | **/*.Server/ModelManifest.xml
293 | _Pvt_Extensions
294 |
295 | # Paket dependency manager
296 | .paket/paket.exe
297 | paket-files/
298 |
299 | # FAKE - F# Make
300 | .fake/
301 |
302 | # CodeRush personal settings
303 | .cr/personal
304 |
305 | # Python Tools for Visual Studio (PTVS)
306 | __pycache__/
307 | *.pyc
308 |
309 | # Cake - Uncomment if you are using it
310 | # tools/**
311 | # !tools/packages.config
312 |
313 | # Tabs Studio
314 | *.tss
315 |
316 | # Telerik's JustMock configuration file
317 | *.jmconfig
318 |
319 | # BizTalk build output
320 | *.btp.cs
321 | *.btm.cs
322 | *.odx.cs
323 | *.xsd.cs
324 |
325 | # OpenCover UI analysis results
326 | OpenCover/
327 |
328 | # Azure Stream Analytics local run output
329 | ASALocalRun/
330 |
331 | # MSBuild Binary and Structured Log
332 | *.binlog
333 |
334 | # NVidia Nsight GPU debugger configuration file
335 | *.nvuser
336 |
337 | # MFractors (Xamarin productivity tool) working folder
338 | .mfractor/
339 |
340 | # Local History for Visual Studio
341 | .localhistory/
342 |
343 | # BeatPulse healthcheck temp database
344 | healthchecksdb
345 |
346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
347 | MigrationBackup/
348 |
349 | # Ionide (cross platform F# VS Code tools) working folder
350 | .ionide/
351 |
--------------------------------------------------------------------------------
/Finished sample/AbstractFactory/AbstractFactory.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/AbstractFactory/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace AbstractFactory
8 | {
9 | ///
10 | /// AbstractFactory
11 | ///
12 | public interface IShoppingCartPurchaseFactory
13 | {
14 | IDiscountService CreateDiscountService();
15 | IShippingCostsService CreateShippingCostsService();
16 | }
17 |
18 | ///
19 | /// AbstractProduct
20 | ///
21 | public interface IDiscountService
22 | {
23 | int DiscountPercentage { get; }
24 | }
25 |
26 | ///
27 | /// ConcreteProduct
28 | ///
29 | public class BelgiumDiscountService : IDiscountService
30 | {
31 | public int DiscountPercentage => 20;
32 | }
33 |
34 | ///
35 | /// ConcreteProduct
36 | ///
37 | public class FranceDiscountService : IDiscountService
38 | {
39 | public int DiscountPercentage => 10;
40 | }
41 |
42 | ///
43 | /// AbstractProduct
44 | ///
45 | public interface IShippingCostsService
46 | {
47 | decimal ShippingCosts { get; }
48 | }
49 |
50 | ///
51 | /// ConcreteProduct
52 | ///
53 | public class BelgiumShippingCostsService : IShippingCostsService
54 | {
55 | public decimal ShippingCosts => 20;
56 | }
57 |
58 | ///
59 | /// ConcreteProduct
60 | ///
61 | public class FranceShippingCostsService : IShippingCostsService
62 | {
63 | public decimal ShippingCosts => 25;
64 | }
65 |
66 |
67 | public class BelgiumShoppingCartPurchaseFactory : IShoppingCartPurchaseFactory
68 | {
69 | public IDiscountService CreateDiscountService()
70 | {
71 | return new BelgiumDiscountService();
72 | }
73 |
74 | public IShippingCostsService CreateShippingCostsService()
75 | {
76 | return new BelgiumShippingCostsService();
77 | }
78 | }
79 |
80 |
81 | public class FranceShoppingCartPurchaseFactory : IShoppingCartPurchaseFactory
82 | {
83 | public IDiscountService CreateDiscountService()
84 | {
85 | return new FranceDiscountService();
86 | }
87 |
88 | public IShippingCostsService CreateShippingCostsService()
89 | {
90 | return new FranceShippingCostsService();
91 | }
92 | }
93 |
94 |
95 | ///
96 | /// Client class
97 | ///
98 | public class ShoppingCart
99 | {
100 | private readonly IDiscountService _discountService;
101 | private readonly IShippingCostsService _shippingCostsService;
102 | private int _orderCosts;
103 |
104 | // Constructor
105 | public ShoppingCart(IShoppingCartPurchaseFactory factory)
106 | {
107 | _discountService = factory.CreateDiscountService();
108 | _shippingCostsService = factory.CreateShippingCostsService();
109 | // assume that the total cost of all the items we ordered = 200 euro
110 | _orderCosts = 200;
111 | }
112 |
113 | public void CalculateCosts()
114 | {
115 | Console.WriteLine($"Total costs = {_orderCosts - (_orderCosts / 100 * _discountService.DiscountPercentage) + _shippingCostsService.ShippingCosts }");
116 | }
117 | }
118 | }
119 |
--------------------------------------------------------------------------------
/Finished sample/AbstractFactory/Program.cs:
--------------------------------------------------------------------------------
1 | using AbstractFactory;
2 |
3 | var belgiumShoppingCartPurchaseFactory = new BelgiumShoppingCartPurchaseFactory();
4 | var shoppingCartForBelgium = new ShoppingCart(belgiumShoppingCartPurchaseFactory);
5 | shoppingCartForBelgium.CalculateCosts();
6 |
7 | var franceShoppingCartPurchaseFactory = new FranceShoppingCartPurchaseFactory();
8 | var shoppingCartForFrance = new ShoppingCart(franceShoppingCartPurchaseFactory);
9 | shoppingCartForFrance.CalculateCosts();
10 |
11 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Adapter/Adapter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Adapter/ClassAdapterImplementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Adapter.ClassAdapter
8 | {
9 | public class CityFromExternalSystem
10 | {
11 | public string Name { get; private set; }
12 | public string NickName { get; private set; }
13 | public int Inhabitants { get; private set; }
14 |
15 | public CityFromExternalSystem(string name, string nickName, int inhabitants)
16 | {
17 | Name = name;
18 | NickName = nickName;
19 | Inhabitants = inhabitants;
20 | }
21 | }
22 |
23 | ///
24 | /// Adaptee
25 | ///
26 | public class ExternalSystem
27 | {
28 | public CityFromExternalSystem GetCity()
29 | {
30 | return new CityFromExternalSystem("Antwerp", "'t Stad", 500000);
31 | }
32 | }
33 |
34 | public class City
35 | {
36 | public string FullName { get; private set; }
37 | public long Inhabitants { get; private set; }
38 |
39 | public City(string fullName, long inhabitants)
40 | {
41 | FullName = fullName;
42 | Inhabitants = inhabitants;
43 | }
44 | }
45 |
46 | ///
47 | /// Target
48 | ///
49 | public interface ICityAdapter
50 | {
51 | City GetCity();
52 | }
53 |
54 | ///
55 | /// Adapter
56 | ///
57 | public class CityAdapter : ExternalSystem, ICityAdapter
58 | {
59 | public City GetCity()
60 | {
61 | // call into the external system
62 | var cityFromExternalSystem = base.GetCity();
63 |
64 | // adapt the CityFromExternalCity to a City
65 | return new City(
66 | $"{cityFromExternalSystem.Name} - {cityFromExternalSystem.NickName}"
67 | , cityFromExternalSystem.Inhabitants);
68 | }
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/Finished sample/Adapter/ObjectAdapterImplementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Adapter.ObjectAdapter
8 | {
9 | public class CityFromExternalSystem
10 | {
11 | public string Name { get; private set; }
12 | public string NickName { get; private set; }
13 | public int Inhabitants { get; private set; }
14 |
15 | public CityFromExternalSystem(
16 | string name,
17 | string nickName,
18 | int inhabitants)
19 | {
20 | Name = name;
21 | NickName = nickName;
22 | Inhabitants = inhabitants;
23 | }
24 | }
25 |
26 | ///
27 | /// Adaptee
28 | ///
29 | public class ExternalSystem
30 | {
31 | public CityFromExternalSystem GetCity()
32 | {
33 | return new CityFromExternalSystem("Antwerp", "'t Stad", 500000);
34 | }
35 | }
36 |
37 | public class City
38 | {
39 | public string FullName { get; private set; }
40 | public long Inhabitants { get; private set; }
41 |
42 | public City(string fullName, long inhabitants)
43 | {
44 | FullName = fullName;
45 | Inhabitants = inhabitants;
46 | }
47 | }
48 |
49 | ///
50 | /// Target
51 | ///
52 | public interface ICityAdapter
53 | {
54 | City GetCity();
55 | }
56 |
57 | ///
58 | /// Adapter
59 | ///
60 | public class CityAdapter : ICityAdapter
61 | {
62 | public ExternalSystem ExternalSystem { get; private set; } = new();
63 |
64 | public City GetCity()
65 | {
66 | // call into the external system
67 | var cityFromExternalSystem = ExternalSystem.GetCity();
68 |
69 | // adapt the CityFromExternalCity to a City
70 | return new City(
71 | $"{cityFromExternalSystem.Name} - {cityFromExternalSystem.NickName}"
72 | , cityFromExternalSystem.Inhabitants);
73 | }
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Finished sample/Adapter/Program.cs:
--------------------------------------------------------------------------------
1 | using Adapter;
2 | using Adapter.ClassAdapter;
3 |
4 | // adapter example (same code for object & class adapter)
5 | ICityAdapter adapter = new CityAdapter();
6 | var city = adapter.GetCity();
7 |
8 | Console.WriteLine($"{city.FullName}, {city.Inhabitants}");
9 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Adapter/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "WSL": {
4 | "commandName": "WSL2",
5 | "distributionName": ""
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/Finished sample/Bridge/Bridge.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Bridge/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Bridge
8 | {
9 | ///
10 | /// Abstraction
11 | ///
12 | public abstract class Menu
13 | {
14 | public readonly ICoupon _coupon;
15 | public abstract int CalculatePrice();
16 |
17 | public Menu(ICoupon coupon)
18 | {
19 | _coupon = coupon;
20 | }
21 | }
22 |
23 |
24 | ///
25 | /// RefinedAbstraction
26 | ///
27 | public class VegetarianMenu : Menu
28 | {
29 | public VegetarianMenu(ICoupon coupon): base(coupon)
30 | {
31 | }
32 | public override int CalculatePrice()
33 | {
34 | return 20 - _coupon.CouponValue;
35 | }
36 | }
37 |
38 | ///
39 | /// RefinedAbstraction
40 | ///
41 | public class MeatBasedMenu : Menu
42 | {
43 | public MeatBasedMenu(ICoupon coupon) : base(coupon)
44 | {
45 | }
46 | public override int CalculatePrice()
47 | {
48 | return 30 - _coupon.CouponValue;
49 | }
50 | }
51 |
52 |
53 | ///
54 | /// Implementor
55 | ///
56 | public interface ICoupon
57 | {
58 | int CouponValue { get; }
59 | }
60 |
61 |
62 | ///
63 | /// ConcreteImplementor
64 | ///
65 | public class NoCoupon : ICoupon
66 | {
67 | public int CouponValue { get => 0; }
68 | }
69 |
70 | ///
71 | /// ConcreteImplementor
72 | ///
73 | public class OneEuroCoupon : ICoupon
74 | {
75 | public int CouponValue { get => 1; }
76 | }
77 |
78 | ///
79 | /// ConcreteImplementor
80 | ///
81 | public class TwoEuroCoupon : ICoupon
82 | {
83 | public int CouponValue { get => 2; }
84 | }
85 | }
--------------------------------------------------------------------------------
/Finished sample/Bridge/Program.cs:
--------------------------------------------------------------------------------
1 | // create a menu implementation
2 |
3 | using Bridge;
4 |
5 | var noCoupon = new NoCoupon();
6 | var oneEuroCoupon = new OneEuroCoupon();
7 |
8 | var meatBasedMenu = new MeatBasedMenu(noCoupon);
9 | Console.WriteLine($"Meat based menu, no coupon: {meatBasedMenu.CalculatePrice()} euro.");
10 |
11 | meatBasedMenu = new MeatBasedMenu(oneEuroCoupon);
12 | Console.WriteLine($"Meat based menu, one euro coupon: {meatBasedMenu.CalculatePrice()} euro.");
13 |
14 | var vegetarianMenu = new VegetarianMenu(noCoupon);
15 | Console.WriteLine($"Vegetarian menu, no coupon: {vegetarianMenu.CalculatePrice()} euro.");
16 |
17 | vegetarianMenu = new VegetarianMenu(oneEuroCoupon);
18 | Console.WriteLine($"Vegetarian menu, one euro coupon: {vegetarianMenu.CalculatePrice()} euro.");
19 |
20 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Builder/Builder.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Builder/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace BuilderPattern
8 | {
9 |
10 |
11 | ///
12 | /// Director
13 | ///
14 | public class Garage
15 | {
16 | private CarBuilder? _builder;
17 |
18 | public Garage()
19 | {
20 | }
21 |
22 | public void Construct(CarBuilder builder)
23 | {
24 | _builder = builder;
25 |
26 | _builder.BuildEngine();
27 | _builder.BuildFrame();
28 | }
29 |
30 | // variation: the show method on the director instead of on the product.
31 | public void Show()
32 | {
33 | Console.WriteLine(_builder?.Car.ToString());
34 | }
35 | }
36 |
37 | ///
38 | /// Builder abstract class
39 | ///
40 | public abstract class CarBuilder
41 | {
42 | public Car Car { get; private set; }
43 |
44 | public CarBuilder(string carType)
45 | {
46 | Car = new Car(carType);
47 | }
48 | public abstract void BuildEngine();
49 | public abstract void BuildFrame();
50 | }
51 |
52 | ///
53 | /// ConcreteBuilder1 class
54 | ///
55 | public class MiniBuilder : CarBuilder
56 | {
57 | public MiniBuilder()
58 | : base("Mini")
59 | {
60 | }
61 |
62 | public override void BuildEngine()
63 | {
64 | Car.AddPart("'not a V8'");
65 | }
66 |
67 | public override void BuildFrame()
68 | {
69 | Car.AddPart("'3-door with stripes'");
70 | }
71 | }
72 |
73 | ///
74 | /// ConcreteBuilder2 class
75 | ///
76 | public class BMWBuilder : CarBuilder
77 | {
78 | // Invoke base class constructor
79 | public BMWBuilder()
80 | : base("BMW")
81 | {
82 | }
83 |
84 | public override void BuildEngine()
85 | {
86 | Car.AddPart("'a fancy V8 engine'");
87 | }
88 |
89 | public override void BuildFrame()
90 | {
91 | Car.AddPart("'5-door with metallic finish'");
92 | }
93 | }
94 |
95 |
96 | ///
97 | /// Product class
98 | ///
99 | public class Car
100 | {
101 | private readonly List _parts = new();
102 | private readonly string _carType;
103 |
104 | public Car(string carType)
105 | {
106 | _carType = carType;
107 | }
108 |
109 | public void AddPart(string part)
110 | {
111 | _parts.Add(part);
112 | }
113 |
114 | public override string ToString()
115 | {
116 | var sb = new StringBuilder();
117 | foreach (string part in _parts)
118 | {
119 | sb.Append($"Car of type {_carType} has part {part}. ");
120 | }
121 |
122 | return sb.ToString();
123 | }
124 | }
125 |
126 |
127 |
128 | ///
129 | /// The 'Director' class
130 | ///
131 | public class Director
132 | {
133 | // Builder uses a complex series of steps
134 | public void Construct(Builder builder)
135 | {
136 | builder.BuildPartA();
137 | builder.BuildPartB();
138 | }
139 | }
140 |
141 | ///
142 | /// The 'Builder' abstract class
143 | ///
144 | public abstract class Builder
145 | {
146 | public abstract void BuildPartA();
147 | public abstract void BuildPartB();
148 | public abstract Product GetResult();
149 | }
150 |
151 | ///
152 | /// The 'ConcreteBuilder1' class
153 | ///
154 | public class ConcreteBuilder1 : Builder
155 | {
156 | private Product product = new Product();
157 |
158 | public override void BuildPartA()
159 | {
160 | product.Add("PartA");
161 | }
162 |
163 | public override void BuildPartB()
164 | {
165 | product.Add("PartB");
166 | }
167 |
168 | public override Product GetResult()
169 | {
170 | return product;
171 | }
172 | }
173 |
174 | ///
175 | /// The 'ConcreteBuilder2' class
176 | ///
177 | public class ConcreteBuilder2 : Builder
178 | {
179 | private Product product = new Product();
180 |
181 | public override void BuildPartA()
182 | {
183 | product.Add("PartX");
184 | }
185 |
186 | public override void BuildPartB()
187 | {
188 | product.Add("PartY");
189 | }
190 |
191 | public override Product GetResult()
192 | {
193 | return product;
194 | }
195 | }
196 |
197 | ///
198 | /// The 'Product' class
199 | ///
200 | public class Product
201 | {
202 | private List parts = new List();
203 |
204 | public void Add(string part)
205 | {
206 | parts.Add(part);
207 | }
208 |
209 | public void Show()
210 | {
211 | Console.WriteLine("\nProduct Parts -------");
212 | foreach (string part in parts)
213 | Console.WriteLine(part);
214 | }
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/Finished sample/Builder/Program.cs:
--------------------------------------------------------------------------------
1 | using BuilderPattern;
2 |
3 |
4 | //Director director = new Director();
5 |
6 | //Builder b1 = new ConcreteBuilder1();
7 | //Builder b2 = new ConcreteBuilder2();
8 |
9 | //// Construct two products
10 | //director.Construct(b1);
11 | //Product p1 = b1.GetResult();
12 | //p1.Show();
13 |
14 | //director.Construct(b2);
15 | //Product p2 = b2.GetResult();
16 | //p2.Show();
17 |
18 |
19 |
20 |
21 | var garage = new Garage();
22 |
23 | var miniBuilder = new MiniBuilder();
24 | var bmwBuilder = new BMWBuilder();
25 |
26 | garage.Construct(miniBuilder);
27 | Console.WriteLine(miniBuilder.Car.ToString());
28 | // or:
29 | garage.Show();
30 |
31 | garage.Construct(bmwBuilder);
32 | Console.WriteLine(bmwBuilder.Car.ToString());
33 | // or:
34 | garage.Show();
35 |
36 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/ChainOfResponsibility/ChainOfResponsibility.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/ChainOfResponsibility/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System.ComponentModel.DataAnnotations;
2 |
3 | namespace ChainOfResponsibility
4 | {
5 | public class Document
6 | {
7 | public string Title { get; set; }
8 | public DateTimeOffset LastModified { get; set; }
9 | public bool ApprovedByLitigation { get; set; }
10 | public bool ApprovedByManagement { get; set; }
11 |
12 | public Document(
13 | string title,
14 | DateTimeOffset lastModified,
15 | bool approvedByLitigation,
16 | bool approvedByManagement)
17 | {
18 | Title = title;
19 | LastModified = lastModified;
20 | ApprovedByLitigation = approvedByLitigation;
21 | ApprovedByManagement = approvedByManagement;
22 | }
23 | }
24 |
25 | ///
26 | /// Handler
27 | ///
28 | public interface IHandler where T : class
29 | {
30 | IHandler SetSuccessor(IHandler successor);
31 | void Handle(T request);
32 | }
33 |
34 | ///
35 | /// ConcreteHandler
36 | ///
37 | public class DocumentTitleHandler : IHandler
38 | {
39 | private IHandler? _successor;
40 |
41 | public void Handle(Document document)
42 | {
43 | if (document.Title == string.Empty)
44 | {
45 | // validation doesn't check out
46 | throw new ValidationException(
47 | new ValidationResult(
48 | "Title must be filled out",
49 | new List() { "Title" }), null, null);
50 | }
51 |
52 | // go to the next handler
53 | _successor?.Handle(document);
54 | }
55 |
56 | public IHandler SetSuccessor(IHandler successor)
57 | {
58 | _successor = successor;
59 | return successor;
60 | }
61 | }
62 |
63 | ///
64 | /// ConcreteHandler
65 | ///
66 | public class DocumentLastModifiedHandler : IHandler
67 | {
68 | private IHandler? _successor;
69 |
70 | public void Handle(Document document)
71 | {
72 | if (document.LastModified < DateTime.UtcNow.AddDays(-30))
73 | {
74 | // validation doesn't check out
75 | throw new ValidationException(
76 | new ValidationResult(
77 | "Document must be modified in the last 30 days",
78 | new List() { "LastModified" }), null, null);
79 | }
80 |
81 | // go to the next handler
82 | _successor?.Handle(document);
83 | }
84 |
85 | public IHandler SetSuccessor(IHandler successor)
86 | {
87 | _successor = successor;
88 | return successor;
89 | }
90 | }
91 |
92 | ///
93 | /// ConcreteHandler
94 | ///
95 | public class DocumentApprovedByLitigationHandler : IHandler
96 | {
97 | private IHandler? _successor;
98 |
99 | public void Handle(Document document)
100 | {
101 | if (!document.ApprovedByLitigation)
102 | {
103 | // validation doesn't check out
104 | throw new ValidationException(
105 | new ValidationResult(
106 | "Document must be approved by litigation",
107 | new List() { "ApprovedByLitigation" }), null, null);
108 | }
109 |
110 | // go to the next handler
111 | _successor?.Handle(document);
112 | }
113 |
114 | public IHandler SetSuccessor(IHandler successor)
115 | {
116 | _successor = successor;
117 | return successor;
118 | }
119 | }
120 |
121 | ///
122 | /// ConcreteHandler
123 | ///
124 | public class DocumentApprovedByManagementHandler : IHandler
125 | {
126 | private IHandler? _successor;
127 |
128 | public void Handle(Document document)
129 | {
130 | if (!document.ApprovedByManagement)
131 | {
132 | // validation doesn't check out
133 | throw new ValidationException(
134 | new ValidationResult(
135 | "Document must be approved by management",
136 | new List() { "ApprovedByManagement" }), null, null);
137 | }
138 |
139 | // go to the next handler
140 | _successor?.Handle(document);
141 | }
142 |
143 | public IHandler SetSuccessor(IHandler successor)
144 | {
145 | _successor = successor;
146 | return successor;
147 | }
148 | }
149 | }
--------------------------------------------------------------------------------
/Finished sample/ChainOfResponsibility/Program.cs:
--------------------------------------------------------------------------------
1 | using ChainOfResponsibility;
2 | using System.ComponentModel.DataAnnotations;
3 |
4 | var validDocument = new Document("How to Avoid Java Development",
5 | DateTimeOffset.UtcNow, true, true);
6 | var invalidDocument = new Document("How to Avoid Java Development",
7 | DateTimeOffset.UtcNow, false, true);
8 |
9 | // chain a set of handlers
10 | var documentHandlerChain = new DocumentTitleHandler();
11 | documentHandlerChain
12 | .SetSuccessor(new DocumentLastModifiedHandler())
13 | .SetSuccessor(new DocumentApprovedByLitigationHandler())
14 | .SetSuccessor(new DocumentApprovedByManagementHandler());
15 |
16 | try
17 | {
18 | documentHandlerChain.Handle(validDocument);
19 | Console.WriteLine("Valid document is valid.");
20 | documentHandlerChain.Handle(invalidDocument);
21 | Console.WriteLine("Invalid document is valid.");
22 | }
23 | catch (ValidationException validationException)
24 | {
25 | Console.WriteLine(validationException.Message);
26 | }
--------------------------------------------------------------------------------
/Finished sample/Command/Command.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Command/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Command
8 | {
9 | ///
10 | /// ICommand
11 | ///
12 | public interface ICommand
13 | {
14 | void Execute();
15 | bool CanExecute();
16 | void Undo();
17 | }
18 |
19 | ///
20 | /// ConcreteCommand
21 | ///
22 | public class AddEmployeeToManagerList : ICommand
23 | {
24 | private readonly IEmployeeManagerRepository _employeeManagerRepository;
25 | private readonly int _managerId;
26 | private readonly Employee? _employee;
27 |
28 | public AddEmployeeToManagerList(
29 | IEmployeeManagerRepository employeeManagerRepository,
30 | int managerId,
31 | Employee? employee)
32 | {
33 | _employeeManagerRepository = employeeManagerRepository;
34 | _managerId = managerId;
35 | _employee = employee;
36 | }
37 |
38 | public bool CanExecute()
39 | {
40 | // potentially check against business/technical rules that might block execution
41 |
42 | // employee shouldn't be null
43 | if (_employee == null)
44 | {
45 | return false;
46 | }
47 |
48 | // employee shouldn't be on the manager's list already
49 | if (_employeeManagerRepository.HasEmployee(_managerId, _employee.Id))
50 | {
51 | return false;
52 | }
53 |
54 | // other potential rule(s): ensure that an employee can only be added to
55 | // one manager's list at the same time, etc.
56 | return true;
57 | }
58 |
59 | public void Execute()
60 | {
61 | if (_employee == null)
62 | {
63 | return;
64 | }
65 |
66 | _employeeManagerRepository.AddEmployee(_managerId, _employee);
67 | }
68 |
69 | public void Undo()
70 | {
71 | if (_employee == null)
72 | {
73 | return;
74 | }
75 |
76 | _employeeManagerRepository.RemoveEmployee(_managerId, _employee);
77 | }
78 | }
79 |
80 | ///
81 | /// Invoker
82 | ///
83 | public class CommandManager
84 | {
85 | private readonly Stack _commands = new Stack();
86 |
87 | public void Invoke(ICommand command)
88 | {
89 | if (command.CanExecute())
90 | {
91 | command.Execute();
92 | _commands.Push(command);
93 | }
94 | }
95 |
96 | public void Undo()
97 | {
98 | if (_commands.Any())
99 | {
100 | _commands.Pop()?.Undo();
101 | }
102 | }
103 |
104 | public void UndoAll()
105 | {
106 | while (_commands.Any())
107 | {
108 | _commands.Pop()?.Undo();
109 | }
110 | }
111 | }
112 |
113 |
114 |
115 | public class Employee
116 | {
117 | public int Id { get; set; }
118 | public string Name { get; set; }
119 |
120 | public Employee(int id, string name)
121 | {
122 | Id = id;
123 | Name = name;
124 | }
125 | }
126 |
127 | public class Manager : Employee
128 | {
129 | public List Employees = new();
130 | public Manager(int id, string name)
131 | : base(id, name)
132 | {
133 | }
134 | }
135 |
136 | ///
137 | /// Receiver (interface)
138 | ///
139 | public interface IEmployeeManagerRepository
140 | {
141 | void AddEmployee(int managerId, Employee employee);
142 | void RemoveEmployee(int managerId, Employee employee);
143 | bool HasEmployee(int managerId, int employeeId);
144 | void WriteDataStore();
145 | }
146 |
147 | ///
148 | /// Receiver (implementation)
149 | ///
150 | public class EmployeeManagerRepository : IEmployeeManagerRepository
151 | {
152 | // for demo purposes, use an in-memory datastore as a fake "manager list"
153 | private List _managers = new List()
154 | { new Manager(1, "Katie"), new Manager(2, "Geoff") };
155 |
156 | public void AddEmployee(int managerId, Employee employee)
157 | {
158 | // in real-life, add additional input & error checks
159 | _managers.First(m => m.Id == managerId).Employees.Add(employee);
160 | }
161 |
162 | public void RemoveEmployee(int managerId, Employee employee)
163 | {
164 | // in real-life, add additional input & error checks
165 | _managers.First(m => m.Id == managerId).Employees.Remove(employee);
166 | }
167 |
168 | public bool HasEmployee(int managerId, int employeeId)
169 | {
170 | // in real-life, add additional input & error checks
171 | return _managers.First(m => m.Id == managerId).Employees.Any(e => e.Id == employeeId);
172 | }
173 |
174 |
175 | ///
176 | /// For demo purposes, write out the data store to the console window
177 | ///
178 | public void WriteDataStore()
179 | {
180 | foreach (var manager in _managers)
181 | {
182 | Console.WriteLine($"Manager {manager.Id}, {manager.Name}");
183 | if (manager.Employees.Any())
184 | {
185 | foreach (var employee in manager.Employees)
186 | {
187 | Console.WriteLine($"\t Employee {employee.Id}, {employee.Name}");
188 | }
189 | }
190 | else
191 | {
192 | Console.WriteLine($"\t No employees.");
193 | }
194 | }
195 | Console.WriteLine();
196 | }
197 | }
198 | }
--------------------------------------------------------------------------------
/Finished sample/Command/Program.cs:
--------------------------------------------------------------------------------
1 | using Command;
2 |
3 | CommandManager commandManager = new ();
4 | IEmployeeManagerRepository repository = new EmployeeManagerRepository();
5 |
6 |
7 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 1, new Employee(111, "Kevin")));
8 | repository.WriteDataStore();
9 |
10 | commandManager.Undo();
11 | repository.WriteDataStore();
12 |
13 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 1, new Employee(222, "Clara")));
14 | repository.WriteDataStore();
15 |
16 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 2, new Employee(333, "Tom")));
17 | repository.WriteDataStore();
18 |
19 | // try adding the same employee again
20 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 2, new Employee(333, "Tom")));
21 | repository.WriteDataStore();
22 |
23 | commandManager.UndoAll();
24 | repository.WriteDataStore();
--------------------------------------------------------------------------------
/Finished sample/Composite/Composite.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Composite/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Composite
8 | {
9 | ///
10 | /// Component
11 | ///
12 | public abstract class FileSystemItem
13 | {
14 | public string Name { get; set; }
15 |
16 | public abstract long GetSize();
17 |
18 | public FileSystemItem(string name)
19 | {
20 | Name = name;
21 | }
22 | }
23 |
24 |
25 | ///
26 | /// Leaf
27 | ///
28 | public class File : FileSystemItem
29 | {
30 | private long _size;
31 | public File(string name, long size) : base(name)
32 | {
33 | _size = size;
34 | }
35 |
36 | public override long GetSize()
37 | {
38 | return _size;
39 | }
40 | }
41 |
42 | ///
43 | /// Composite
44 | ///
45 | public class Directory : FileSystemItem
46 | {
47 | private long _size;
48 | private List _fileSystemItems { get; set; } = new List();
49 |
50 | public Directory(string name, long size) : base(name)
51 | {
52 | _size = size;
53 | }
54 |
55 | public void Add(FileSystemItem itemToAdd)
56 | {
57 | _fileSystemItems.Add(itemToAdd);
58 | }
59 |
60 | public void Remove(FileSystemItem itemToRemove)
61 | {
62 | _fileSystemItems.Remove(itemToRemove);
63 | }
64 |
65 | public override long GetSize()
66 | {
67 | var treeSize = _size;
68 | foreach (var fileSystemItem in _fileSystemItems)
69 | {
70 | treeSize += fileSystemItem.GetSize();
71 | }
72 | return treeSize;
73 | }
74 | }
75 |
76 |
77 | }
78 |
--------------------------------------------------------------------------------
/Finished sample/Composite/Program.cs:
--------------------------------------------------------------------------------
1 | var root = new Composite.Directory("root", 0);
2 | var topLevelFile = new Composite.File("toplevel.txt", 100);
3 | var topLevelDirectory1 = new Composite.Directory("topleveldirectory1", 4);
4 | var topLevelDirectory2 = new Composite.Directory("topleveldirectory2", 4);
5 |
6 | root.Add(topLevelFile);
7 | root.Add(topLevelDirectory1);
8 | root.Add(topLevelDirectory2);
9 |
10 | var subLevelFile1 = new Composite.File("sublevel1.txt", 200);
11 | var subLevelFile2 = new Composite.File("sublevel2.txt", 150);
12 |
13 | topLevelDirectory2.Add(subLevelFile1);
14 | topLevelDirectory2.Add(subLevelFile2);
15 |
16 | Console.WriteLine($"Size of topLevelDirectory1: {topLevelDirectory1.GetSize()}");
17 | Console.WriteLine($"Size of topLevelDirectory2: {topLevelDirectory2.GetSize()}");
18 | Console.WriteLine($"Size of root: {root.GetSize()}");
19 |
20 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Decorator/Decorator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Decorator/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Decorator
8 | {
9 | ///
10 | /// ConcreteComponent1
11 | ///
12 | public class CloudMailService : IMailService
13 | {
14 | public bool SendMail(string message)
15 | {
16 | Console.WriteLine($"Message \"{message}\" sent via {nameof(CloudMailService)}.");
17 | return true;
18 | }
19 | }
20 |
21 | ///
22 | /// ConcreteComponent2
23 | ///
24 | public class OnPremiseMailService : IMailService
25 | {
26 | public bool SendMail(string message)
27 | {
28 | Console.WriteLine($"Message \"{message}\" sent via {nameof(OnPremiseMailService)}.");
29 | return true;
30 | }
31 | }
32 |
33 | ///
34 | /// Component (as interface)
35 | ///
36 | public interface IMailService
37 | {
38 | bool SendMail(string message);
39 | }
40 |
41 |
42 | ///
43 | /// Decorator (as abstract base class)
44 | ///
45 | public abstract class MailServiceDecoratorBase : IMailService
46 | {
47 | private readonly IMailService _mailService;
48 | public MailServiceDecoratorBase(IMailService mailService)
49 | {
50 | _mailService = mailService;
51 | }
52 |
53 | public virtual bool SendMail(string message)
54 | {
55 | return _mailService.SendMail(message);
56 | }
57 | }
58 |
59 | ///
60 | /// ConcreteDecorator1
61 | ///
62 | public class StatisticsDecorator : MailServiceDecoratorBase
63 | {
64 | public StatisticsDecorator(IMailService mailService)
65 | : base(mailService)
66 | {
67 | }
68 |
69 | public override bool SendMail(string message)
70 | {
71 | // Fake collecting statistics
72 | Console.WriteLine($"Collecting statistics in {nameof(StatisticsDecorator)}.");
73 | return base.SendMail(message);
74 | }
75 | }
76 |
77 | ///
78 | /// ConcreteDecorator2
79 | ///
80 | public class MessageDatabaseDecorator : MailServiceDecoratorBase
81 | {
82 | ///
83 | /// A list of sent messages - a "fake" database
84 | ///
85 | public List SentMessages { get; private set; } = new List();
86 |
87 | public MessageDatabaseDecorator(IMailService mailService)
88 | : base(mailService)
89 | {
90 | }
91 |
92 | public override bool SendMail(string message)
93 | {
94 | if (base.SendMail(message))
95 | {
96 | // store sent message
97 | SentMessages.Add(message);
98 | return true;
99 | };
100 |
101 | return false;
102 | }
103 | }
104 | }
105 |
--------------------------------------------------------------------------------
/Finished sample/Decorator/Program.cs:
--------------------------------------------------------------------------------
1 | // instantiate mail services
2 | using Decorator;
3 |
4 | var cloudMailService = new CloudMailService();
5 | cloudMailService.SendMail("Hi there.");
6 |
7 | var onPremiseMailService = new OnPremiseMailService();
8 | onPremiseMailService.SendMail("Hi there.");
9 |
10 | // add behavior
11 | var statisticsDecorator = new StatisticsDecorator(cloudMailService);
12 | statisticsDecorator.SendMail($"Hi there via {nameof(StatisticsDecorator)} wrapper.");
13 |
14 | // add behavior
15 | var messageDatabaseDecorator = new MessageDatabaseDecorator(onPremiseMailService);
16 | messageDatabaseDecorator.SendMail(
17 | $"Hi there via {nameof(MessageDatabaseDecorator)} wrapper, message 1.");
18 | messageDatabaseDecorator.SendMail(
19 | $"Hi there via {nameof(MessageDatabaseDecorator)} wrapper, message 2.");
20 |
21 | foreach (var message in messageDatabaseDecorator.SentMessages)
22 | {
23 | Console.WriteLine($"Stored message: \"{message}\"");
24 | }
25 |
26 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/DesignPatterns.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.0.31903.59
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "A - Creational", "A - Creational", "{B0F237CE-4C3B-467B-AB3F-7FED90583AB9}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "01 - Singleton", "01 - Singleton", "{482F9D07-99EE-4BF6-975F-295E89ECBCF9}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02 - Factory Method", "02 - Factory Method", "{E8783E99-05CD-4F71-B166-0FC1B90FC2C2}"
11 | EndProject
12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03 - Abstract Factory", "03 - Abstract Factory", "{4763D927-A2E7-4322-8134-7D24DDD947F1}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryMethod", "FactoryMethod\FactoryMethod.csproj", "{1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactory", "AbstractFactory\AbstractFactory.csproj", "{51C54352-1897-4749-BD39-660613C2235B}"
17 | EndProject
18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04 -Builder", "04 -Builder", "{21752809-82A5-4212-8444-166F656129FC}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Builder", "Builder\Builder.csproj", "{8552B7D4-35B3-4916-8D5D-D0479802BA35}"
21 | EndProject
22 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "05 - Prototype", "05 - Prototype", "{E0164F8E-AB94-4D3F-879A-6DB5F19FE84B}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prototype", "Prototype\Prototype.csproj", "{1C69FD15-5FD4-49AF-B31F-F2A3407364EC}"
25 | EndProject
26 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "B - Structural", "B - Structural", "{BA4514FF-AA9E-402C-B074-5A16FF91FD6E}"
27 | EndProject
28 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06 - Adapter", "06 - Adapter", "{60637BBF-C1E5-47EF-9994-7870B51913A3}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter.csproj", "{4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}"
31 | EndProject
32 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07 - Bridge", "07 - Bridge", "{7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2}"
33 | EndProject
34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{75E6E304-74F2-4D88-8589-4366B1A7684D}"
35 | EndProject
36 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "08 - Decorator", "08 - Decorator", "{46719157-346D-4BCE-B34D-55D2B5F2E93A}"
37 | EndProject
38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{12768030-04EF-4B1D-A0E9-7C96F6B73DBE}"
39 | EndProject
40 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "09 - Composite", "09 - Composite", "{E262BBA8-94AE-495C-85E6-FA54BA6A90A2}"
41 | EndProject
42 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Composite.csproj", "{898A0BFF-C8CA-451D-BA92-DACABABFF53E}"
43 | EndProject
44 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10 - Facade", "10 - Facade", "{BA606C2E-5A34-4290-AC23-58E5482CD300}"
45 | EndProject
46 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facade", "Facade\Facade.csproj", "{2F4804B4-98BC-4DF6-8362-E96AA344A29A}"
47 | EndProject
48 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "11 - Proxy", "11 - Proxy", "{59DA4BDF-CFF5-4F06-8878-67F7900F37D7}"
49 | EndProject
50 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Proxy", "Proxy\Proxy.csproj", "{3BDFB38E-00D4-434E-8985-5D6661931089}"
51 | EndProject
52 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "12 - Flyweight", "12 - Flyweight", "{449D2089-E1E9-4097-8996-5E6E3D80B24C}"
53 | EndProject
54 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flyweight", "Flyweight\Flyweight.csproj", "{ACCB5262-A3F5-4A77-864F-F27A3A3B5259}"
55 | EndProject
56 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Singleton\Singleton.csproj", "{347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}"
57 | EndProject
58 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "C - Behavioral", "C - Behavioral", "{EE357574-412D-4C9D-A167-5002DE164362}"
59 | EndProject
60 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "15 - Command", "15 - Command", "{7736AF2A-2B94-42FB-A793-5020973687C8}"
61 | EndProject
62 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}"
63 | EndProject
64 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "16 - Memento", "16 - Memento", "{C5BDE3AF-1138-4E87-8810-611687FFA522}"
65 | EndProject
66 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memento", "Memento\Memento.csproj", "{176C219A-5E5A-4710-B360-E09DBC80690D}"
67 | EndProject
68 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "20 - State", "20 - State", "{0CC41725-98B1-4F2E-ACB9-3A0F47F65E97}"
69 | EndProject
70 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "State", "State\State.csproj", "{9D29533E-7C7F-4B72-9331-81EA8A9957D7}"
71 | EndProject
72 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "14 - Strategy", "14 - Strategy", "{62068BDC-394F-4A1E-825B-C7DE137900BB}"
73 | EndProject
74 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strategy", "Strategy\Strategy.csproj", "{6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}"
75 | EndProject
76 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "17 - Mediator", "17 - Mediator", "{F71B79ED-777C-4707-9D38-CF15A476C55B}"
77 | EndProject
78 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mediator", "Mediator\Mediator.csproj", "{913A71BA-D3BC-41F1-BA44-888AB8658630}"
79 | EndProject
80 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "18 - Chain of Responsibility", "18 - Chain of Responsibility", "{71A327E1-BA13-466C-B1A6-FA4551E8A670}"
81 | EndProject
82 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{8768B99D-DC81-4951-BA46-8F1D10631C69}"
83 | EndProject
84 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "19 - Observer", "19 - Observer", "{162BE83E-9D99-494B-8464-BB0BEE0BCC81}"
85 | EndProject
86 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Observer", "Observer\Observer.csproj", "{8FA532ED-DB9D-47E7-AD53-31881953993A}"
87 | EndProject
88 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "21 - Iterator", "21 - Iterator", "{818D8FDD-1C97-48D8-AEEF-A7F193AA9187}"
89 | EndProject
90 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Iterator", "Iterator\Iterator.csproj", "{D4FEF27B-1C98-495E-93EF-1AC36ED810AB}"
91 | EndProject
92 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "22 - Visitor", "22 - Visitor", "{173BC868-E7AE-43AE-A05E-FD9BEEA980C7}"
93 | EndProject
94 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Visitor", "Visitor\Visitor.csproj", "{E3A37190-E73D-4F91-A249-9B5D18CF83AF}"
95 | EndProject
96 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "13 - Template Method", "13 - Template Method", "{1622583F-64EB-4F30-8D38-E897FF8FA140}"
97 | EndProject
98 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemplateMethod", "TemplateMethod\TemplateMethod.csproj", "{97CB88F2-868A-457A-90EB-89F55B9CBBF4}"
99 | EndProject
100 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "23 - Interpreter", "23 - Interpreter", "{FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB}"
101 | EndProject
102 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Interpreter", "Interpreter\Interpreter.csproj", "{39C0A16A-B9F1-444B-9D5D-F46BBB84F746}"
103 | EndProject
104 | Global
105 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
106 | Debug|Any CPU = Debug|Any CPU
107 | Release|Any CPU = Release|Any CPU
108 | EndGlobalSection
109 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
110 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
112 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
113 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Release|Any CPU.Build.0 = Release|Any CPU
114 | {51C54352-1897-4749-BD39-660613C2235B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115 | {51C54352-1897-4749-BD39-660613C2235B}.Debug|Any CPU.Build.0 = Debug|Any CPU
116 | {51C54352-1897-4749-BD39-660613C2235B}.Release|Any CPU.ActiveCfg = Release|Any CPU
117 | {51C54352-1897-4749-BD39-660613C2235B}.Release|Any CPU.Build.0 = Release|Any CPU
118 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Debug|Any CPU.Build.0 = Debug|Any CPU
120 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Release|Any CPU.ActiveCfg = Release|Any CPU
121 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Release|Any CPU.Build.0 = Release|Any CPU
122 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
124 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
125 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Release|Any CPU.Build.0 = Release|Any CPU
126 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
128 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
129 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Release|Any CPU.Build.0 = Release|Any CPU
130 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Debug|Any CPU.Build.0 = Debug|Any CPU
132 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Release|Any CPU.ActiveCfg = Release|Any CPU
133 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Release|Any CPU.Build.0 = Release|Any CPU
134 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
136 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
137 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Release|Any CPU.Build.0 = Release|Any CPU
138 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
139 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Debug|Any CPU.Build.0 = Debug|Any CPU
140 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Release|Any CPU.ActiveCfg = Release|Any CPU
141 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Release|Any CPU.Build.0 = Release|Any CPU
142 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
143 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU
144 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU
145 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Release|Any CPU.Build.0 = Release|Any CPU
146 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
147 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Debug|Any CPU.Build.0 = Debug|Any CPU
148 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Release|Any CPU.ActiveCfg = Release|Any CPU
149 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Release|Any CPU.Build.0 = Release|Any CPU
150 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
151 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Debug|Any CPU.Build.0 = Debug|Any CPU
152 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Release|Any CPU.ActiveCfg = Release|Any CPU
153 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Release|Any CPU.Build.0 = Release|Any CPU
154 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
155 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
156 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
157 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Release|Any CPU.Build.0 = Release|Any CPU
158 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
159 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
160 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
161 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Release|Any CPU.Build.0 = Release|Any CPU
162 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
163 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Debug|Any CPU.Build.0 = Debug|Any CPU
164 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Release|Any CPU.ActiveCfg = Release|Any CPU
165 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Release|Any CPU.Build.0 = Release|Any CPU
166 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
167 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
168 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
169 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Release|Any CPU.Build.0 = Release|Any CPU
170 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
171 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Debug|Any CPU.Build.0 = Debug|Any CPU
172 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Release|Any CPU.ActiveCfg = Release|Any CPU
173 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Release|Any CPU.Build.0 = Release|Any CPU
174 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
175 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Debug|Any CPU.Build.0 = Debug|Any CPU
176 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Release|Any CPU.ActiveCfg = Release|Any CPU
177 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Release|Any CPU.Build.0 = Release|Any CPU
178 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
179 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Debug|Any CPU.Build.0 = Debug|Any CPU
180 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Release|Any CPU.ActiveCfg = Release|Any CPU
181 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Release|Any CPU.Build.0 = Release|Any CPU
182 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Debug|Any CPU.Build.0 = Debug|Any CPU
184 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Release|Any CPU.ActiveCfg = Release|Any CPU
185 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Release|Any CPU.Build.0 = Release|Any CPU
186 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
187 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
188 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
189 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Release|Any CPU.Build.0 = Release|Any CPU
190 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
191 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
192 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
193 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Release|Any CPU.Build.0 = Release|Any CPU
194 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
196 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
197 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Release|Any CPU.Build.0 = Release|Any CPU
198 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
199 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Debug|Any CPU.Build.0 = Debug|Any CPU
200 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Release|Any CPU.ActiveCfg = Release|Any CPU
201 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Release|Any CPU.Build.0 = Release|Any CPU
202 | EndGlobalSection
203 | GlobalSection(SolutionProperties) = preSolution
204 | HideSolutionNode = FALSE
205 | EndGlobalSection
206 | GlobalSection(NestedProjects) = preSolution
207 | {482F9D07-99EE-4BF6-975F-295E89ECBCF9} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
208 | {E8783E99-05CD-4F71-B166-0FC1B90FC2C2} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
209 | {4763D927-A2E7-4322-8134-7D24DDD947F1} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
210 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE} = {E8783E99-05CD-4F71-B166-0FC1B90FC2C2}
211 | {51C54352-1897-4749-BD39-660613C2235B} = {4763D927-A2E7-4322-8134-7D24DDD947F1}
212 | {21752809-82A5-4212-8444-166F656129FC} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
213 | {8552B7D4-35B3-4916-8D5D-D0479802BA35} = {21752809-82A5-4212-8444-166F656129FC}
214 | {E0164F8E-AB94-4D3F-879A-6DB5F19FE84B} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
215 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC} = {E0164F8E-AB94-4D3F-879A-6DB5F19FE84B}
216 | {60637BBF-C1E5-47EF-9994-7870B51913A3} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
217 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5} = {60637BBF-C1E5-47EF-9994-7870B51913A3}
218 | {7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
219 | {75E6E304-74F2-4D88-8589-4366B1A7684D} = {7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2}
220 | {46719157-346D-4BCE-B34D-55D2B5F2E93A} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
221 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE} = {46719157-346D-4BCE-B34D-55D2B5F2E93A}
222 | {E262BBA8-94AE-495C-85E6-FA54BA6A90A2} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
223 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E} = {E262BBA8-94AE-495C-85E6-FA54BA6A90A2}
224 | {BA606C2E-5A34-4290-AC23-58E5482CD300} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
225 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A} = {BA606C2E-5A34-4290-AC23-58E5482CD300}
226 | {59DA4BDF-CFF5-4F06-8878-67F7900F37D7} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
227 | {3BDFB38E-00D4-434E-8985-5D6661931089} = {59DA4BDF-CFF5-4F06-8878-67F7900F37D7}
228 | {449D2089-E1E9-4097-8996-5E6E3D80B24C} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
229 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259} = {449D2089-E1E9-4097-8996-5E6E3D80B24C}
230 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C} = {482F9D07-99EE-4BF6-975F-295E89ECBCF9}
231 | {7736AF2A-2B94-42FB-A793-5020973687C8} = {EE357574-412D-4C9D-A167-5002DE164362}
232 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D} = {7736AF2A-2B94-42FB-A793-5020973687C8}
233 | {C5BDE3AF-1138-4E87-8810-611687FFA522} = {EE357574-412D-4C9D-A167-5002DE164362}
234 | {176C219A-5E5A-4710-B360-E09DBC80690D} = {C5BDE3AF-1138-4E87-8810-611687FFA522}
235 | {0CC41725-98B1-4F2E-ACB9-3A0F47F65E97} = {EE357574-412D-4C9D-A167-5002DE164362}
236 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7} = {0CC41725-98B1-4F2E-ACB9-3A0F47F65E97}
237 | {62068BDC-394F-4A1E-825B-C7DE137900BB} = {EE357574-412D-4C9D-A167-5002DE164362}
238 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812} = {62068BDC-394F-4A1E-825B-C7DE137900BB}
239 | {F71B79ED-777C-4707-9D38-CF15A476C55B} = {EE357574-412D-4C9D-A167-5002DE164362}
240 | {913A71BA-D3BC-41F1-BA44-888AB8658630} = {F71B79ED-777C-4707-9D38-CF15A476C55B}
241 | {71A327E1-BA13-466C-B1A6-FA4551E8A670} = {EE357574-412D-4C9D-A167-5002DE164362}
242 | {8768B99D-DC81-4951-BA46-8F1D10631C69} = {71A327E1-BA13-466C-B1A6-FA4551E8A670}
243 | {162BE83E-9D99-494B-8464-BB0BEE0BCC81} = {EE357574-412D-4C9D-A167-5002DE164362}
244 | {8FA532ED-DB9D-47E7-AD53-31881953993A} = {162BE83E-9D99-494B-8464-BB0BEE0BCC81}
245 | {818D8FDD-1C97-48D8-AEEF-A7F193AA9187} = {EE357574-412D-4C9D-A167-5002DE164362}
246 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB} = {818D8FDD-1C97-48D8-AEEF-A7F193AA9187}
247 | {173BC868-E7AE-43AE-A05E-FD9BEEA980C7} = {EE357574-412D-4C9D-A167-5002DE164362}
248 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF} = {173BC868-E7AE-43AE-A05E-FD9BEEA980C7}
249 | {1622583F-64EB-4F30-8D38-E897FF8FA140} = {EE357574-412D-4C9D-A167-5002DE164362}
250 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4} = {1622583F-64EB-4F30-8D38-E897FF8FA140}
251 | {FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB} = {EE357574-412D-4C9D-A167-5002DE164362}
252 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746} = {FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB}
253 | EndGlobalSection
254 | GlobalSection(ExtensibilityGlobals) = postSolution
255 | SolutionGuid = {F84E1DA5-656F-4C72-9FBA-4A07AD3EE35E}
256 | EndGlobalSection
257 | EndGlobal
258 |
--------------------------------------------------------------------------------
/Finished sample/Facade/Facade.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Facade/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Facade
8 | {
9 | ///
10 | /// Facade
11 | ///
12 | public class DiscountFacade
13 | {
14 | private readonly OrderService _orderService = new ();
15 | private readonly CustomerDiscountBaseService _customerDiscountBaseService = new ();
16 | private readonly DayOfTheWeekFactorService _dayOfTheWeekFactorService = new ();
17 |
18 | public double CalculateDiscountPercentage(int customerId)
19 | {
20 | if (!_orderService.HasEnoughOrders(customerId))
21 | {
22 | return 0;
23 | }
24 |
25 | return _customerDiscountBaseService.CalculateDiscountBase(customerId)
26 | * _dayOfTheWeekFactorService.CalculateDayOfTheWeekFactor();
27 | }
28 | }
29 |
30 | ///
31 | /// Subsystem class
32 | ///
33 | public class OrderService
34 | {
35 | public bool HasEnoughOrders(int customerId)
36 | {
37 | // does the customer have enough orders?
38 | // fake calculation for demo purposes
39 | return (customerId > 5);
40 | }
41 | }
42 |
43 | ///
44 | /// Subsystem class
45 | ///
46 | public class CustomerDiscountBaseService
47 | {
48 | public double CalculateDiscountBase(int customerId)
49 | {
50 | // fake calculation for demo purposes
51 | return (customerId > 8) ? 10 : 20;
52 | }
53 | }
54 |
55 |
56 | ///
57 | /// Subsystem class
58 | ///
59 | public class DayOfTheWeekFactorService
60 | {
61 | public double CalculateDayOfTheWeekFactor()
62 | {
63 | // fake calculation for demo purposes
64 | switch (DateTime.UtcNow.DayOfWeek)
65 | {
66 | case DayOfWeek.Saturday:
67 | case DayOfWeek.Sunday:
68 | return 0.8;
69 | default:
70 | return 1.2;
71 | }
72 | }
73 | }
74 |
75 | }
76 |
--------------------------------------------------------------------------------
/Finished sample/Facade/Program.cs:
--------------------------------------------------------------------------------
1 | using Facade;
2 |
3 | var facade = new DiscountFacade();
4 | Console.WriteLine($"Discount percentage for customer with id 1: {facade.CalculateDiscountPercentage(1)}");
5 | Console.WriteLine($"Discount percentage for customer with id 10: {facade.CalculateDiscountPercentage(10)}");
6 |
7 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/FactoryMethod/FactoryMethod.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/FactoryMethod/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace FactoryMethod
8 | {
9 | ///
10 | /// Product
11 | ///
12 | public abstract class DiscountService
13 | {
14 | public abstract int DiscountPercentage { get; }
15 |
16 | public override string ToString() => GetType().Name;
17 | }
18 |
19 | ///
20 | /// ConcreteProduct
21 | ///
22 | public class CountryDiscountService : DiscountService
23 | {
24 | private readonly string _countryIdentifier;
25 |
26 | public CountryDiscountService(string countryIdentifier)
27 | {
28 | _countryIdentifier = countryIdentifier;
29 | }
30 |
31 | public override int DiscountPercentage
32 | {
33 | get
34 | {
35 | switch (_countryIdentifier)
36 | {
37 | // if you're from Belgium, you get a better discount :)
38 | case "BE":
39 | return 20;
40 | default:
41 | return 10;
42 | }
43 | }
44 | }
45 | }
46 |
47 | ///
48 | /// ConcreteProduct
49 | ///
50 | public class CodeDiscountService : DiscountService
51 | {
52 | private readonly Guid _code;
53 |
54 | public CodeDiscountService(Guid code)
55 | {
56 | _code = code;
57 | }
58 |
59 | public override int DiscountPercentage
60 | {
61 | // each code returns the same fixed percentage, but a code is only
62 | // valid once - include a check to so whether the code's been used before
63 | // ...
64 | get => 15;
65 | }
66 | }
67 |
68 | ///
69 | /// Creator
70 | ///
71 | public abstract class DiscountFactory
72 | {
73 | public abstract DiscountService CreateDiscountService();
74 | }
75 |
76 | ///
77 | /// ConcretCreator
78 | ///
79 | public class CountryDiscountFactory : DiscountFactory
80 | {
81 | private readonly string _countryIdentifier;
82 | public CountryDiscountFactory(string countryIdentifier)
83 | {
84 | _countryIdentifier = countryIdentifier;
85 | }
86 |
87 | public override DiscountService CreateDiscountService()
88 | {
89 | return new CountryDiscountService(_countryIdentifier);
90 | }
91 | }
92 |
93 | ///
94 | /// ConcreteCreator
95 | ///
96 | public class CodeDiscountFactory : DiscountFactory
97 | {
98 | private readonly Guid _code;
99 |
100 | public CodeDiscountFactory(Guid code)
101 | {
102 | _code = code;
103 | }
104 | public override DiscountService CreateDiscountService()
105 | {
106 | return new CodeDiscountService(_code);
107 | }
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/Finished sample/FactoryMethod/Program.cs:
--------------------------------------------------------------------------------
1 | using FactoryMethod;
2 |
3 | var factories = new List {
4 | new CodeDiscountFactory(Guid.NewGuid()),
5 | new CountryDiscountFactory("BE") };
6 |
7 | foreach (var factory in factories)
8 | {
9 | var discountService = factory.CreateDiscountService();
10 | Console.WriteLine($"Percentage {discountService.DiscountPercentage} coming from {discountService}");
11 | }
12 |
13 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Flyweight/Flyweight.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Flyweight/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Flyweight
8 | {
9 | ///
10 | /// Flyweight
11 | ///
12 | public interface ICharacter
13 | {
14 | void Draw(string fontFamily, int fontSize);
15 | }
16 |
17 | ///
18 | /// Concrete Flyweight
19 | ///
20 | public class CharacterA : ICharacter
21 | {
22 | private char _actualCharacter = 'a';
23 | private string _fontFamily = string.Empty;
24 | private int _fontSize;
25 |
26 | public void Draw(string fontFamily, int fontSize)
27 | {
28 | _fontFamily = fontFamily;
29 | _fontSize = fontSize;
30 | Console.WriteLine($"Drawing {_actualCharacter}, {_fontFamily} {_fontSize}");
31 | }
32 | }
33 |
34 | ///
35 | /// Concrete Flyweight
36 | ///
37 | public class CharacterB : ICharacter
38 | {
39 | private char _actualCharacter = 'b';
40 | private string _fontFamily = string.Empty;
41 | private int _fontSize;
42 |
43 | public void Draw(string fontFamily, int fontSize)
44 | {
45 | _fontFamily = fontFamily;
46 | _fontSize = fontSize;
47 | Console.WriteLine($"Drawing {_actualCharacter}, {_fontFamily} {_fontSize}");
48 | }
49 | }
50 |
51 | ///
52 | /// FlyweightFactory
53 | ///
54 | public class CharacterFactory
55 | {
56 | private readonly Dictionary _characters = new ();
57 |
58 | public ICharacter? GetCharacter(char characterIdentifier)
59 | {
60 | // Coes the character dictionary contain the one we need?
61 | if (_characters.ContainsKey(characterIdentifier))
62 | {
63 | Console.WriteLine("Character reuse");
64 | return _characters[characterIdentifier];
65 | }
66 |
67 | // The character isn't in the dictionary.
68 | // Create it, store it, return it.
69 | Console.WriteLine("Character construction");
70 | switch (characterIdentifier)
71 | {
72 | case 'a':
73 | _characters[characterIdentifier] = new CharacterA();
74 | return _characters[characterIdentifier];
75 | case 'b':
76 | _characters[characterIdentifier] = new CharacterB();
77 | return _characters[characterIdentifier];
78 | // and so on...
79 | }
80 |
81 | return null;
82 | }
83 |
84 | public ICharacter CreateParagraph(List characters, int location)
85 | {
86 | return new Paragraph(characters, location);
87 | }
88 | }
89 |
90 | ///
91 | /// Unshared Concrete Flyweight
92 | ///
93 | public class Paragraph : ICharacter
94 | {
95 | private int _location;
96 | private List _characters = new();
97 |
98 | public Paragraph(List characters, int location)
99 | {
100 | _characters = characters;
101 | _location = location;
102 | }
103 |
104 | public void Draw(string fontFamily, int fontSize)
105 | {
106 | Console.WriteLine($"Drawing in paragraph at location {_location}");
107 | foreach (var character in _characters)
108 | {
109 | character.Draw(fontFamily, fontSize);
110 | }
111 | }
112 | }
113 | }
114 |
--------------------------------------------------------------------------------
/Finished sample/Flyweight/Program.cs:
--------------------------------------------------------------------------------
1 | using Flyweight;
2 |
3 | var aBunchOfCharacters = "abba";
4 |
5 | var characterFactory = new CharacterFactory();
6 |
7 | // Get the flyweight(s)
8 | var characterObject = characterFactory.GetCharacter(aBunchOfCharacters[0]);
9 | // Pass through extrinsic state
10 | characterObject?.Draw("Arial", 12);
11 |
12 | characterObject = characterFactory.GetCharacter(aBunchOfCharacters[1]);
13 | characterObject?.Draw("Trebuchet MS", 14);
14 |
15 | characterObject = characterFactory.GetCharacter(aBunchOfCharacters[2]);
16 | characterObject?.Draw("Times New Roman", 16);
17 |
18 | characterObject = characterFactory.GetCharacter(aBunchOfCharacters[3]);
19 | characterObject?.Draw("Comic Sans", 18);
20 |
21 | // create unshared concrete flyweight (paragraph)
22 | var paragraph = characterFactory.CreateParagraph(
23 | new List() { characterObject }, 1);
24 |
25 | // draw the paragraph
26 | paragraph.Draw("Lucinda", 12);
27 |
28 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Interpreter/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Interpreter
8 | {
9 | ///
10 | /// Context
11 | ///
12 | public class RomanContext
13 | {
14 | public int Input { get; set; }
15 | public string Output { get; set; } = string.Empty;
16 | public RomanContext(int input)
17 | {
18 | Input = input;
19 | }
20 | }
21 |
22 | ///
23 | /// AbstractExpression
24 | ///
25 | public abstract class RomanExpression
26 | {
27 | public abstract void Interpret(RomanContext value);
28 | }
29 |
30 | // 9 = IX
31 | // 8 = VIII
32 | // 7 = VII
33 | // 6 = VI
34 | // 5 = V
35 | // 4 = IV
36 | // 3 = III
37 | // 2 = II
38 | // 1 = I
39 |
40 | // simplified - each combination is reachable with substraction and these 4:
41 | // 9 = IX
42 | // 5 = V
43 | // 4 = IV
44 | // 1 = I
45 |
46 | ///
47 | /// TerminalExpression
48 | ///
49 | public class RomanOneExpression : RomanExpression
50 | {
51 | public override void Interpret(RomanContext value)
52 | {
53 | while ((value.Input - 9) >= 0)
54 | {
55 | value.Output += "IX";
56 | value.Input -= 9;
57 | }
58 |
59 | while ((value.Input - 5) >= 0)
60 | {
61 | value.Output += "V";
62 | value.Input -= 5;
63 | }
64 |
65 | while ((value.Input - 4) >= 0)
66 | {
67 | value.Output += "IV";
68 | value.Input -= 4;
69 | }
70 |
71 | while ((value.Input - 1) >= 0)
72 | {
73 | value.Output += "I";
74 | value.Input -= 1;
75 | }
76 | }
77 | }
78 |
79 | // 90 = XC
80 | // 80 = LIII
81 | // 70 = LII
82 | // 60 = LX
83 | // 50 = L
84 | // 40 = XL
85 | // 30 = XXX
86 | // 20 = XX
87 | // 10 = X
88 |
89 | // simplified - each combination is reachable with substraction and these 4:
90 | // 90 = XC
91 | // 50 = L
92 | // 40 = XL
93 | // 10 = X
94 |
95 | ///
96 | /// TerminalExpression
97 | ///
98 | public class RomanTenExpression : RomanExpression
99 | {
100 | public override void Interpret(RomanContext value)
101 | {
102 | while ((value.Input - 90) >= 0)
103 | {
104 | value.Output += "XC";
105 | value.Input -= 90;
106 | }
107 |
108 | while ((value.Input - 50) >= 0)
109 | {
110 | value.Output += "L";
111 | value.Input -= 50;
112 | }
113 |
114 | while ((value.Input - 40) >= 0)
115 | {
116 | value.Output += "XL";
117 | value.Input -= 40;
118 | }
119 |
120 | while ((value.Input - 10) >= 0)
121 | {
122 | value.Output += "X";
123 | value.Input -= 10;
124 | }
125 | }
126 | }
127 |
128 | // 900 = CM
129 | // 800 = DCCC
130 | // 700 = DCC
131 | // 600 = DC
132 | // 500 = D
133 | // 400 = CD
134 | // 300 = CCC
135 | // 200 = CC
136 | // 100 = C
137 |
138 | // simplified - each combination is reachable with substraction and these 4:
139 | // 900 = CM
140 | // 500 = D
141 | // 400 = CD
142 | // 100 = C
143 |
144 | ///
145 | /// TerminalExpression
146 | ///
147 | public class RomanHunderdExpression : RomanExpression
148 | {
149 | public override void Interpret(RomanContext value)
150 | {
151 | while ((value.Input - 900) >= 0)
152 | {
153 | value.Output += "CM";
154 | value.Input -= 900;
155 | }
156 |
157 | while ((value.Input - 500) >= 0)
158 | {
159 | value.Output += "D";
160 | value.Input -= 500;
161 | }
162 |
163 | while ((value.Input - 400) >= 0)
164 | {
165 | value.Output += "CD";
166 | value.Input -= 400;
167 | }
168 |
169 | while ((value.Input - 100) >= 0)
170 | {
171 | value.Output += "C";
172 | value.Input -= 100;
173 | }
174 | }
175 | }
176 | }
177 |
--------------------------------------------------------------------------------
/Finished sample/Interpreter/Interpreter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Interpreter/Program.cs:
--------------------------------------------------------------------------------
1 | using Interpreter;
2 |
3 | var expressions = new List
4 | {
5 | new RomanHunderdExpression(),
6 | new RomanTenExpression(),
7 | new RomanOneExpression(),
8 | };
9 |
10 | var context = new RomanContext(5);
11 | foreach (var expression in expressions)
12 | {
13 | expression.Interpret(context);
14 | }
15 | Console.WriteLine($"Translating Arabic numerals to Roman numerals: 5 = {context.Output}");
16 |
17 | context = new RomanContext(81);
18 | foreach (var expression in expressions)
19 | {
20 | expression.Interpret(context);
21 | }
22 | Console.WriteLine($"Translating Arabic numerals to Roman numerals: 81 = {context.Output}");
23 |
24 | context = new RomanContext(733);
25 | foreach (var expression in expressions)
26 | {
27 | expression.Interpret(context);
28 | }
29 | Console.WriteLine($"Translating Arabic numerals to Roman numerals: 733 = {context.Output}");
30 |
--------------------------------------------------------------------------------
/Finished sample/Iterator/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Iterator
2 | {
3 | public class Person
4 | {
5 | public string Name { get; set; }
6 | public string Country { get; set; }
7 |
8 | public Person(string name, string country)
9 | {
10 | Name = name;
11 | Country = country;
12 | }
13 | }
14 |
15 | ///
16 | /// Iterator
17 | ///
18 | public interface IPeopleIterator
19 | {
20 | Person First();
21 | Person Next();
22 | bool IsDone { get; }
23 | Person CurrentItem { get; }
24 | }
25 |
26 | ///
27 | /// ConcreteIterator
28 | ///
29 | public class PeopleIterator : IPeopleIterator
30 | {
31 | private PeopleCollection _peopleCollection;
32 | private int _current = 0;
33 |
34 | public PeopleIterator(PeopleCollection collection)
35 | {
36 | _peopleCollection = collection;
37 | }
38 |
39 | public Person First()
40 | {
41 | _current = 0;
42 | return _peopleCollection
43 | .OrderBy(p => p.Name).ToList()[_current];
44 | }
45 |
46 | public Person Next()
47 | {
48 | _current++;
49 | if (!IsDone)
50 | {
51 | return _peopleCollection
52 | .OrderBy(p => p.Name).ToList()[_current];
53 | }
54 | else
55 | {
56 | return null;
57 | }
58 | }
59 |
60 | public bool IsDone
61 | {
62 | get { return _current >= _peopleCollection.Count; }
63 | }
64 |
65 | public Person CurrentItem
66 | {
67 | get { return _peopleCollection
68 | .OrderBy(p => p.Name).ToList()[_current]; }
69 | }
70 | }
71 |
72 | ///
73 | /// Aggregate
74 | ///
75 | public interface IPeopleCollection
76 | {
77 | IPeopleIterator CreateIterator();
78 | }
79 |
80 | ///
81 | /// ConcreteAggregate
82 | ///
83 | public class PeopleCollection : List, IPeopleCollection
84 | {
85 | public IPeopleIterator CreateIterator()
86 | {
87 | return new PeopleIterator(this);
88 | }
89 | }
90 | }
91 |
--------------------------------------------------------------------------------
/Finished sample/Iterator/Iterator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Iterator/Program.cs:
--------------------------------------------------------------------------------
1 | using Iterator;
2 |
3 | // create the collection
4 | PeopleCollection people = new PeopleCollection();
5 | people.Add(new Person("Kevin Dockx", "Belgium"));
6 | people.Add(new Person("Gill Cleeren", "Belgium"));
7 | people.Add(new Person("Roland Guijt", "The Netherlands"));
8 | people.Add(new Person("Thomas Claudius Huber", "Germany"));
9 |
10 | // create the interator
11 | var peopleIterator = people.CreateIterator();
12 |
13 | // use the iterator to run through the people
14 | // in the collection; they should come out
15 | // in alphabetical order
16 | for (Person person = peopleIterator.First();
17 | !peopleIterator.IsDone;
18 | person = peopleIterator.Next())
19 | {
20 | Console.WriteLine(person?.Name);
21 | }
22 |
--------------------------------------------------------------------------------
/Finished sample/Mediator/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Mediator
8 | {
9 | ///
10 | /// Mediator
11 | ///
12 | public interface IChatRoom
13 | {
14 | void Register(TeamMember teamMember);
15 | void Send(string from, string message);
16 | void Send(string from, string to, string message);
17 | void SendTo(string from, string message) where T : TeamMember;
18 | }
19 |
20 | ///
21 | /// Colleague
22 | ///
23 | public abstract class TeamMember
24 | {
25 | public string Name { get; set; }
26 |
27 | private IChatRoom? _chatroom;
28 |
29 | public TeamMember(string name)
30 | {
31 | Name = name;
32 | }
33 |
34 | internal void SetChatroom(IChatRoom chatRoom)
35 | {
36 | _chatroom = chatRoom;
37 | }
38 |
39 | public void Send(string message)
40 | {
41 | _chatroom?.Send(Name, message);
42 | }
43 |
44 | public void Send(string to, string message)
45 | {
46 | _chatroom?.Send(Name, to, message);
47 | }
48 |
49 | public void SendTo(string message) where T : TeamMember
50 | {
51 | _chatroom?.SendTo(Name, message);
52 | }
53 |
54 | public virtual void Receive(string from, string message)
55 | {
56 | Console.WriteLine($"message from {from} to {Name}: {message}");
57 | }
58 | }
59 |
60 | ///
61 | /// ConcreteColleague
62 | ///
63 | public class Lawyer : TeamMember
64 | {
65 | private List _teamMembersInChat = new();
66 |
67 | public Lawyer(string name) : base(name)
68 | {
69 | }
70 |
71 | public override void Receive(string from, string message)
72 | {
73 | Console.Write($"{nameof(Lawyer)} {Name} received: ");
74 | base.Receive(from, message);
75 | }
76 | }
77 |
78 |
79 | ///
80 | /// ConcreteColleague
81 | ///
82 | public class AccountManager : TeamMember
83 | {
84 | public AccountManager(string name) : base(name)
85 | {
86 | }
87 |
88 | public override void Receive(string from, string message)
89 | {
90 | Console.Write($"{nameof(AccountManager)} {Name} received: ");
91 | base.Receive(from, message);
92 | }
93 | }
94 |
95 |
96 | ///
97 | /// ConcreteMediator
98 | ///
99 | public class TeamChatRoom : IChatRoom
100 | {
101 | private readonly Dictionary teamMembers = new();
102 |
103 | public void Register(TeamMember teamMember)
104 | {
105 | teamMember.SetChatroom(this);
106 | if (!teamMembers.ContainsKey(teamMember.Name))
107 | {
108 | teamMembers.Add(teamMember.Name, teamMember);
109 | }
110 | }
111 |
112 | public void Send(string from, string message)
113 | {
114 | foreach (var teamMember in teamMembers.Values)
115 | {
116 | teamMember.Receive(from, message);
117 | }
118 | }
119 |
120 | public void Send(string from, string to, string message)
121 | {
122 | var teamMember = teamMembers[to];
123 | teamMember?.Receive(from, message);
124 | }
125 |
126 | public void SendTo(string from, string message) where T : TeamMember
127 | {
128 | foreach (var teamMember in teamMembers.Values.OfType())
129 | {
130 | teamMember.Receive(from, message);
131 | }
132 | }
133 | }
134 | }
135 |
--------------------------------------------------------------------------------
/Finished sample/Mediator/Mediator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Mediator/Program.cs:
--------------------------------------------------------------------------------
1 | using Mediator;
2 |
3 | TeamChatRoom teamChatroom = new();
4 |
5 | var sven = new Lawyer("Sven");
6 | var kenneth = new Lawyer("Kenneth");
7 | var ann = new AccountManager("Ann");
8 | var john = new AccountManager("John");
9 | var kylie = new AccountManager("Kylie");
10 |
11 | teamChatroom.Register(sven);
12 | teamChatroom.Register(kenneth);
13 | teamChatroom.Register(ann);
14 | teamChatroom.Register(john);
15 | teamChatroom.Register(kylie);
16 |
17 | ann.Send("Hi everyone, can someone have a look at file ABC123? I need a compliance check.");
18 | sven.Send("On it!");
19 | sven.Send("Ann", "Could you join me in a Teams call?");
20 | sven.Send("Ann", "All good :)");
21 | ann.SendTo("The file was approved!");
--------------------------------------------------------------------------------
/Finished sample/Memento/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Memento
2 | {
3 | ///
4 | /// ICommand
5 | ///
6 | public interface ICommand
7 | {
8 | void Execute();
9 | bool CanExecute();
10 | void Undo();
11 | }
12 |
13 | ///
14 | /// Memento
15 | ///
16 | public class AddEmployeeToManagerListMemento
17 | {
18 | public int ManagerId { get; private set; }
19 | public Employee? Employee { get; private set; }
20 |
21 | public AddEmployeeToManagerListMemento(int managerId, Employee? employee)
22 | {
23 | ManagerId = managerId;
24 | Employee = employee;
25 | }
26 | }
27 |
28 | ///
29 | /// ConcreteCommand
30 | ///
31 | public class AddEmployeeToManagerList : ICommand
32 | {
33 | private readonly IEmployeeManagerRepository _employeeManagerRepository;
34 | private int _managerId;
35 | private Employee? _employee;
36 |
37 | public AddEmployeeToManagerList(
38 | IEmployeeManagerRepository employeeManagerRepository,
39 | int managerId,
40 | Employee? employee)
41 | {
42 | _employeeManagerRepository = employeeManagerRepository;
43 | _managerId = managerId;
44 | _employee = employee;
45 | }
46 |
47 | public AddEmployeeToManagerListMemento CreateMemento()
48 | {
49 | return new AddEmployeeToManagerListMemento(_managerId, _employee);
50 | }
51 |
52 | public void RestoreMemento(AddEmployeeToManagerListMemento memento)
53 | {
54 | _managerId = memento.ManagerId;
55 | _employee = memento.Employee;
56 | }
57 |
58 | public bool CanExecute()
59 | {
60 | // potentially check against business/technical rules that might block execution
61 |
62 | // employee shouldn't be null
63 | if (_employee == null)
64 | {
65 | return false;
66 | }
67 |
68 | // employee shouldn't be on the manager's list already
69 | if (_employeeManagerRepository.HasEmployee(_managerId, _employee.Id))
70 | {
71 | return false;
72 | }
73 |
74 | // other potential rule(s): ensure that an employee can only be added to
75 | // one manager's list at the same time, etc.
76 | return true;
77 | }
78 |
79 | public void Execute()
80 | {
81 | if (_employee == null)
82 | {
83 | return;
84 | }
85 |
86 | _employeeManagerRepository.AddEmployee(_managerId, _employee);
87 | }
88 |
89 | public void Undo()
90 | {
91 | if (_employee == null)
92 | {
93 | return;
94 | }
95 |
96 | _employeeManagerRepository.RemoveEmployee(_managerId, _employee);
97 | }
98 | }
99 |
100 | ///
101 | /// CareTaker (and Invoker from the Command pattern)
102 | ///
103 | public class CommandManager
104 | {
105 | private readonly Stack _mementos = new ();
106 | private AddEmployeeToManagerList? _command;
107 |
108 | public void Invoke(AddEmployeeToManagerList command)
109 | {
110 | // if the command has not been stored yet, store it - we will
111 | // reuse it instead of storing different instances
112 |
113 | if (_command == null)
114 | {
115 | _command = command;
116 | }
117 |
118 | if (command.CanExecute())
119 | {
120 | command.Execute();
121 | _mementos.Push(command.CreateMemento());
122 | }
123 | }
124 |
125 | public void Undo()
126 | {
127 | if (_mementos.Any())
128 | {
129 | _command?.RestoreMemento(_mementos.Pop());
130 | _command?.Undo();
131 | }
132 | }
133 |
134 | public void UndoAll()
135 | {
136 | while (_mementos.Any())
137 | {
138 | _command?.RestoreMemento(_mementos.Pop());
139 | _command?.Undo();
140 | }
141 | }
142 | }
143 |
144 |
145 |
146 | public class Employee
147 | {
148 | public int Id { get; set; }
149 | public string Name { get; set; }
150 |
151 | public Employee(int id, string name)
152 | {
153 | Id = id;
154 | Name = name;
155 | }
156 | }
157 |
158 | public class Manager : Employee
159 | {
160 | public List Employees = new();
161 | public Manager(int id, string name)
162 | : base(id, name)
163 | {
164 | }
165 | }
166 |
167 | ///
168 | /// Receiver (interface)
169 | ///
170 | public interface IEmployeeManagerRepository
171 | {
172 | void AddEmployee(int managerId, Employee employee);
173 | void RemoveEmployee(int managerId, Employee employee);
174 | bool HasEmployee(int managerId, int employeeId);
175 | void WriteDataStore();
176 | }
177 |
178 | ///
179 | /// Receiver (implementation)
180 | ///
181 | public class EmployeeManagerRepository : IEmployeeManagerRepository
182 | {
183 | // for demo purposes, use an in-memory datastore as a fake "manager list"
184 | private List _managers = new List()
185 | { new Manager(1, "Katie"), new Manager(2, "Geoff") };
186 |
187 | public void AddEmployee(int managerId, Employee employee)
188 | {
189 | // in real-life, add additional input & error checks
190 | _managers.First(m => m.Id == managerId).Employees.Add(employee);
191 | }
192 |
193 | public void RemoveEmployee(int managerId, Employee employee)
194 | {
195 | // in real-life, add additional input & error checks
196 | _managers.First(m => m.Id == managerId).Employees.Remove(employee);
197 | }
198 |
199 | public bool HasEmployee(int managerId, int employeeId)
200 | {
201 | // in real-life, add additional input & error checks
202 | return _managers.First(m => m.Id == managerId).Employees.Any(e => e.Id == employeeId);
203 | }
204 |
205 |
206 | ///
207 | /// For demo purposes, write out the data store to the console window
208 | ///
209 | public void WriteDataStore()
210 | {
211 | foreach (var manager in _managers)
212 | {
213 | Console.WriteLine($"Manager {manager.Id}, {manager.Name}");
214 | if (manager.Employees.Any())
215 | {
216 | foreach (var employee in manager.Employees)
217 | {
218 | Console.WriteLine($"\t Employee {employee.Id}, {employee.Name}");
219 | }
220 | }
221 | else
222 | {
223 | Console.WriteLine($"\t No employees.");
224 | }
225 | }
226 | Console.WriteLine();
227 | }
228 | }
229 | }
--------------------------------------------------------------------------------
/Finished sample/Memento/Memento.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Memento/Program.cs:
--------------------------------------------------------------------------------
1 | using Memento;
2 |
3 | CommandManager commandManager = new();
4 | IEmployeeManagerRepository repository = new EmployeeManagerRepository();
5 |
6 |
7 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 1, new Employee(111, "Kevin")));
8 | repository.WriteDataStore();
9 |
10 | commandManager.Undo();
11 | repository.WriteDataStore();
12 |
13 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 1, new Employee(222, "Clara")));
14 | repository.WriteDataStore();
15 |
16 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 2, new Employee(333, "Tom")));
17 | repository.WriteDataStore();
18 |
19 | // try adding the same employee again
20 | commandManager.Invoke(new AddEmployeeToManagerList(repository, 2, new Employee(333, "Tom")));
21 | repository.WriteDataStore();
22 |
23 | commandManager.UndoAll();
24 | repository.WriteDataStore();
--------------------------------------------------------------------------------
/Finished sample/Observer/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Observer
2 | {
3 | public class TicketChange
4 | {
5 | public int Amount { get; private set; }
6 | public int ArtistId { get; private set; }
7 |
8 | public TicketChange(int artistId, int amount)
9 | {
10 | ArtistId = artistId;
11 | Amount = amount;
12 | }
13 | }
14 |
15 | ///
16 | /// Subject
17 | ///
18 | public abstract class TicketChangeNotifier
19 | {
20 | private List _observers = new();
21 |
22 | public void AddObserver(ITicketChangeListener observer)
23 | {
24 | _observers.Add(observer);
25 | }
26 |
27 | public void RemoveObserver(ITicketChangeListener observer)
28 | {
29 | _observers.Remove(observer);
30 | }
31 |
32 | public void Notify(TicketChange ticketChange)
33 | {
34 | foreach (var observer in _observers)
35 | {
36 | observer.ReceiveTicketChangeNotification(ticketChange);
37 | }
38 | }
39 | }
40 |
41 | ///
42 | /// ConcreteSubject
43 | ///
44 | public class OrderService : TicketChangeNotifier
45 | {
46 | public void CompleteTicketSale(int artistId, int amount)
47 | {
48 | // change local datastore. Datastore omitted in demo implementation.
49 | Console.WriteLine($"{nameof(OrderService)} is changing its state.");
50 | // notify observers
51 | Console.WriteLine($"{nameof(OrderService)} is notifying observers...");
52 | Notify(new TicketChange(artistId, amount));
53 | }
54 | }
55 |
56 | ///
57 | /// Observer
58 | ///
59 | public interface ITicketChangeListener
60 | {
61 | void ReceiveTicketChangeNotification(TicketChange ticketChange);
62 | }
63 |
64 | ///
65 | /// ConcreteObserver
66 | ///
67 | public class TicketResellerService : ITicketChangeListener
68 | {
69 | public void ReceiveTicketChangeNotification(TicketChange ticketChange)
70 | {
71 | // update local datastore (datastore omitted in demo implementation)
72 | Console.WriteLine($"{nameof(TicketResellerService)} notified " +
73 | $"of ticket change: artist {ticketChange.ArtistId}, amount {ticketChange.Amount}");
74 | }
75 | }
76 |
77 | ///
78 | /// ConcreteObserver
79 | ///
80 | public class TicketStockService : ITicketChangeListener
81 | {
82 | public void ReceiveTicketChangeNotification(TicketChange ticketChange)
83 | {
84 | // update local datastore (datastore omitted in demo implementation)
85 | Console.WriteLine($"{nameof(TicketStockService)} notified " +
86 | $"of ticket change: artist {ticketChange.ArtistId}, amount {ticketChange.Amount}");
87 | }
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/Finished sample/Observer/Observer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Observer/Program.cs:
--------------------------------------------------------------------------------
1 | using Observer;
2 |
3 | TicketStockService ticketStockService = new();
4 | TicketResellerService ticketResellerService = new();
5 | OrderService orderService = new();
6 |
7 | // add two observers
8 | orderService.AddObserver(ticketStockService);
9 | orderService.AddObserver(ticketResellerService);
10 |
11 | // notify
12 | orderService.CompleteTicketSale(1, 2);
13 |
14 | // remove one observer
15 | orderService.RemoveObserver(ticketResellerService);
16 |
17 | // notify
18 | orderService.CompleteTicketSale(2, 4);
19 |
--------------------------------------------------------------------------------
/Finished sample/Prototype/Implementation.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 | using System.Runtime.Serialization;
6 | using System.Runtime.Serialization.Formatters.Binary;
7 | using System.Threading.Tasks;
8 |
9 | namespace Prototype
10 | {
11 | ///
12 | /// Prototype abstract class
13 | ///
14 | public abstract class Person
15 | {
16 | public abstract string Name { get; set; }
17 |
18 | public abstract Person Clone(bool deepClone);
19 |
20 | }
21 |
22 | ///
23 | /// ConcretePrototype1
24 | ///
25 | public class Employee : Person
26 | {
27 | public Manager Manager { get; set; }
28 | public override string Name { get; set; }
29 |
30 | public Employee(string name, Manager manager)
31 | {
32 | Name = name;
33 | Manager = manager;
34 | }
35 |
36 | public override Person Clone(bool deepClone = false)
37 | {
38 | if (deepClone)
39 | {
40 | var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
41 | var objectAsJson = JsonConvert.SerializeObject(this, typeof(Employee), settings);
42 | return JsonConvert.DeserializeObject(objectAsJson, settings);
43 | }
44 |
45 |
46 | return (Person)MemberwiseClone();
47 | }
48 | }
49 |
50 | ///
51 | /// ConcretePrototype2
52 | ///
53 | public class Manager : Person
54 | {
55 | public override string Name { get; set; }
56 |
57 | public Manager(string name)
58 | {
59 | Name = name;
60 | }
61 |
62 | public override Person Clone(bool deepClone = false)
63 | {
64 | if (deepClone)
65 | {
66 | var settings = new JsonSerializerSettings { TypeNameHandling = TypeNameHandling.All };
67 | var objectAsJson = JsonConvert.SerializeObject(this, typeof(Manager), settings);
68 | return JsonConvert.DeserializeObject(objectAsJson, settings);
69 | }
70 |
71 | return (Person)MemberwiseClone();
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/Finished sample/Prototype/Program.cs:
--------------------------------------------------------------------------------
1 | using Prototype;
2 |
3 | var manager = new Manager("Cindy");
4 | var managerClone = (Manager)manager.Clone();
5 | Console.WriteLine($"Manager was cloned: {managerClone.Name}");
6 |
7 | var employee = new Employee("Kevin", manager);
8 | var employeeClone = (Employee)employee.Clone(true);
9 | Console.WriteLine($"Employee was cloned: {employeeClone.Name}, with manager {employeeClone.Manager.Name}");
10 |
11 | // change the manager name
12 | manager.Name = "Karen";
13 | Console.WriteLine($"Employee was cloned: {employeeClone.Name}, with manager {employeeClone.Manager.Name}");
14 |
15 | Console.ReadKey();
--------------------------------------------------------------------------------
/Finished sample/Prototype/Prototype.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/Finished sample/Proxy/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Proxy
8 | {
9 | ///
10 | /// RealSubject
11 | ///
12 | public class Document : IDocument
13 | {
14 | public string? Title { get; private set; }
15 | public string? Content { get; private set; }
16 | public int AuthorId { get; private set; }
17 | public DateTimeOffset LastAccessed { get; private set; }
18 | private string _fileName;
19 |
20 | public Document(string fileName)
21 | {
22 | _fileName = fileName;
23 | LoadDocument(fileName);
24 | }
25 |
26 | private void LoadDocument(string fileName)
27 | {
28 | Console.WriteLine("Executing expensive action: loading a file from disk");
29 | // fake loading...
30 | Thread.Sleep(1000);
31 |
32 | Title = "An expensive document";
33 | Content = "Lots and lots of content";
34 | AuthorId = 1;
35 | LastAccessed = DateTimeOffset.UtcNow;
36 | }
37 |
38 | public void DisplayDocument()
39 | {
40 | Console.WriteLine($"Title: {Title}, Content: {Content}");
41 | }
42 | }
43 |
44 | ///
45 | /// Subject
46 | ///
47 | public interface IDocument
48 | {
49 | void DisplayDocument();
50 | }
51 |
52 | ///
53 | /// Proxy
54 | ///
55 | public class DocumentProxy : IDocument
56 | {
57 | // avoid creating the document until we need it
58 | private Lazy _document;
59 | private string _fileName;
60 |
61 | public DocumentProxy(string fileName)
62 | {
63 | _fileName = fileName;
64 | _document = new Lazy(() => new Document(_fileName));
65 | }
66 |
67 | public void DisplayDocument()
68 | {
69 | _document.Value.DisplayDocument();
70 | }
71 | }
72 |
73 | public class ProtectedDocumentProxy : IDocument
74 | {
75 | private string _fileName;
76 | private string _userRole;
77 | private DocumentProxy _documentProxy;
78 |
79 | public ProtectedDocumentProxy(string fileName,
80 | string userRole)
81 | {
82 | _fileName = fileName;
83 | _userRole = userRole;
84 | _documentProxy = new DocumentProxy(_fileName);
85 | }
86 |
87 | public void DisplayDocument()
88 | {
89 | Console.WriteLine($"Entering DisplayDocument in {nameof(ProtectedDocumentProxy)}.");
90 |
91 | if (_userRole != "Viewer")
92 | {
93 | throw new UnauthorizedAccessException();
94 | }
95 |
96 | _documentProxy.DisplayDocument();
97 |
98 | Console.WriteLine($"Exiting DisplayDocument in {nameof(ProtectedDocumentProxy)}.");
99 | }
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/Finished sample/Proxy/Program.cs:
--------------------------------------------------------------------------------
1 |
2 | // without proxy
3 | Console.WriteLine("Constructing document.");
4 | var myDocument = new Proxy.Document("MyDocument.pdf");
5 | Console.WriteLine("Document constructed.");
6 | myDocument.DisplayDocument();
7 |
8 | Console.WriteLine();
9 |
10 | // with proxy
11 | Console.WriteLine("Constructing document proxy.");
12 | var myDocumentProxy = new Proxy.DocumentProxy("MyDocument.pdf");
13 | Console.WriteLine("Document proxy constructed.");
14 | myDocumentProxy.DisplayDocument();
15 |
16 | Console.WriteLine();
17 |
18 | // with chained proxy
19 | Console.WriteLine("Constructing protected document proxy.");
20 | var myProtectedDocumentProxy = new Proxy.ProtectedDocumentProxy("MyDocument.pdf", "Viewer");
21 | Console.WriteLine("Protected document proxy constructed.");
22 | myProtectedDocumentProxy.DisplayDocument();
23 |
24 | Console.WriteLine();
25 |
26 | // with chained proxy, no access
27 | Console.WriteLine("Constructing protected document proxy.");
28 | myProtectedDocumentProxy = new Proxy.ProtectedDocumentProxy("MyDocument.pdf", "AnotherRole");
29 | Console.WriteLine("Protected document proxy constructed.");
30 | myProtectedDocumentProxy.DisplayDocument();
31 |
--------------------------------------------------------------------------------
/Finished sample/Proxy/Proxy.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Singleton/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Singleton
8 | {
9 | ///
10 | /// Singleton
11 | ///
12 | public class Logger
13 | {
14 | //private static Logger? _instance;
15 |
16 | private static readonly Lazy _lazyLogger
17 | = new Lazy(() => new Logger());
18 |
19 | ///
20 | /// Instance
21 | ///
22 | public static Logger Instance
23 | {
24 | get { return _lazyLogger.Value; }
25 | //get
26 | //{
27 | // if (_instance == null)
28 | // {
29 | // _instance = new Logger();
30 | // }
31 | // return _instance;
32 | //}
33 | }
34 |
35 | protected Logger()
36 | {
37 | }
38 |
39 | ///
40 | /// SingletonOperation
41 | ///
42 | public void Log(string message)
43 | {
44 | Console.WriteLine($"Message to log: {message}");
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/Finished sample/Singleton/Program.cs:
--------------------------------------------------------------------------------
1 | using Singleton;
2 |
3 | // call the property getter twice
4 | var instance1 = Logger.Instance;
5 | var instance2 = Logger.Instance;
6 |
7 | if (instance1 == instance2 && instance2 == Logger.Instance)
8 | {
9 | Console.WriteLine("Instances are the same.");
10 | }
11 |
12 | instance1.Log($"Message from {nameof(instance1)}");
13 | // or
14 | instance1.Log($"Message from {nameof(instance2)}");
15 | // or
16 | Logger.Instance.Log($"Message from {nameof(Logger.Instance)}");
--------------------------------------------------------------------------------
/Finished sample/Singleton/Singleton.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/State/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace State
8 | {
9 | ///
10 | /// State
11 | ///
12 | public abstract class BankAccountState
13 | {
14 | public BankAccount BankAccount { get; protected set; } = null!;
15 | public decimal Balance { get; protected set; }
16 |
17 | public abstract void Deposit(decimal amount);
18 | public abstract void Withdraw(decimal amount);
19 | }
20 |
21 | ///
22 | /// ConcreteState
23 | ///
24 | public class OverdrawnState : BankAccountState
25 | {
26 | public OverdrawnState(decimal balance, BankAccount bankAccount)
27 | {
28 | Balance = balance;
29 | BankAccount = bankAccount;
30 | }
31 |
32 | ///
33 | /// Handle a deposit
34 | ///
35 | public override void Deposit(decimal amount)
36 | {
37 | Console.WriteLine($"In {GetType()}, depositing {amount}");
38 | Balance += amount;
39 | if (Balance >= 0)
40 | {
41 | // change state to regular
42 | BankAccount.BankAccountState = new RegularState(Balance, BankAccount);
43 | }
44 | }
45 |
46 | ///
47 | /// Handle withdrawing
48 | ///
49 | public override void Withdraw(decimal amount)
50 | {
51 | // cannot withdraw
52 | Console.WriteLine($"In {GetType()}, cannot withdraw, balance {Balance}");
53 | }
54 | }
55 |
56 | ///
57 | /// ConcreteState
58 | ///
59 | public class RegularState : BankAccountState
60 | {
61 | public RegularState(decimal balance, BankAccount bankAccount)
62 | {
63 | Balance = balance;
64 | BankAccount = bankAccount;
65 | }
66 |
67 | ///
68 | /// Handle a deposit
69 | ///
70 | public override void Deposit(decimal amount)
71 | {
72 | Console.WriteLine($"In {GetType()}, depositing {amount}");
73 | Balance += amount;
74 | if (Balance >= 1000)
75 | {
76 | // change state to gold
77 | BankAccount.BankAccountState = new GoldState(Balance, BankAccount);
78 | }
79 | }
80 |
81 | ///
82 | /// Handle withdrawing
83 | ///
84 | public override void Withdraw(decimal amount)
85 | {
86 | Console.WriteLine($"In {GetType()}, withdrawing {amount} from {Balance}");
87 | // change state to overdrawn when withdrawing results in less than zero
88 | Balance -= amount;
89 | if (Balance < 0)
90 | {
91 | // change state to overdrawn
92 | BankAccount.BankAccountState = new OverdrawnState(Balance, BankAccount);
93 | }
94 | }
95 | }
96 |
97 | ///
98 | /// ConcreteState
99 | ///
100 | public class GoldState : BankAccountState
101 | {
102 | public GoldState(decimal balance, BankAccount bankAccount)
103 | {
104 | Balance = balance;
105 | BankAccount = bankAccount;
106 | }
107 |
108 | ///
109 | /// Handle a deposit
110 | ///
111 | public override void Deposit(decimal amount)
112 | {
113 | Console.WriteLine($"In {GetType()}, depositing {amount} + 10% bonus: {amount/10}");
114 | Balance += amount + (amount/10);
115 | }
116 |
117 | ///
118 | /// Handle withdrawing
119 | ///
120 | public override void Withdraw(decimal amount)
121 | {
122 | Console.WriteLine($"In {GetType()}, withdrawing {amount} from {Balance}");
123 | // change state to overdrawn when withdrawing results in less than zero
124 | Balance -= amount;
125 | if (Balance < 1000 && Balance >= 0)
126 | {
127 | // change state to regular
128 | BankAccount.BankAccountState = new RegularState(Balance, BankAccount);
129 | }
130 | else if (Balance < 0)
131 | {
132 | // change state to overdrawn
133 | BankAccount.BankAccountState = new OverdrawnState(Balance, BankAccount);
134 | }
135 | }
136 | }
137 |
138 | ///
139 | /// Context
140 | ///
141 | public class BankAccount
142 | {
143 | public BankAccountState BankAccountState { get; set; }
144 | public decimal Balance { get { return BankAccountState.Balance; } }
145 |
146 | public BankAccount()
147 | {
148 | BankAccountState = new RegularState(200, this);
149 | }
150 |
151 | ///
152 | /// Request a deposit
153 | ///
154 | public void Deposit(decimal amount)
155 | {
156 | // let the current state handle the deposit
157 | BankAccountState.Deposit(amount);
158 | }
159 |
160 | ///
161 | /// Request a withdrawal
162 | ///
163 | public void Withdraw(decimal amount)
164 | {
165 | // let the current state handle the withdrawel
166 | BankAccountState.Withdraw(amount);
167 | }
168 | }
169 | }
170 |
--------------------------------------------------------------------------------
/Finished sample/State/Program.cs:
--------------------------------------------------------------------------------
1 | using State;
2 |
3 | BankAccount bankAccount = new();
4 | bankAccount.Deposit(100);
5 | bankAccount.Withdraw(500);
6 | bankAccount.Withdraw(100);
7 | // deposit enough to go to gold
8 | bankAccount.Deposit(2000);
9 | // should be in gold now
10 | bankAccount.Deposit(100);
11 | // back to overdraw
12 | bankAccount.Withdraw(3000);
13 | // should transition to regular
14 | bankAccount.Deposit(3000);
15 | // should still be in regular
16 | bankAccount.Deposit(100);
--------------------------------------------------------------------------------
/Finished sample/State/State.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Strategy/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Strategy
2 | {
3 | ///
4 | /// Strategy
5 | ///
6 | public interface IExportService
7 | {
8 | void Export(Order order);
9 | }
10 |
11 | ///
12 | /// ConcreteStrategy
13 | ///
14 | public class JsonExportService : IExportService
15 | {
16 | public void Export(Order order)
17 | {
18 | Console.WriteLine($"Exporting {order.Name} to Json.");
19 | }
20 | }
21 |
22 | ///
23 | /// ConcreteStrategy
24 | ///
25 | public class XMLExportService : IExportService
26 | {
27 | public void Export(Order order)
28 | {
29 | Console.WriteLine($"Exporting {order.Name} to XML.");
30 | }
31 | }
32 |
33 | ///
34 | /// ConcreteStrategy
35 | ///
36 | public class CSVExportService : IExportService
37 | {
38 | public void Export(Order order)
39 | {
40 | Console.WriteLine($"Exporting {order.Name} to CSV.");
41 | }
42 | }
43 |
44 | ///
45 | /// Context
46 | ///
47 | public class Order
48 | {
49 | public string Customer { get; set; }
50 | public int Amount { get; set; }
51 | public string Name { get; set; }
52 | public string? Description { get; set; }
53 |
54 | public Order(string customer, int amount, string name)
55 | {
56 | Customer = customer;
57 | Amount = amount;
58 | Name = name;
59 | }
60 |
61 | public void Export(IExportService exportService)
62 | {
63 | if (exportService is null)
64 | {
65 | throw new ArgumentNullException(nameof(exportService));
66 | }
67 |
68 | exportService.Export(this);
69 | }
70 | }
71 | }
72 |
--------------------------------------------------------------------------------
/Finished sample/Strategy/Program.cs:
--------------------------------------------------------------------------------
1 | using Strategy;
2 |
3 | var order = new Order("Marvin Software", 5, "Visual Studio License");
4 | order.Export(new CSVExportService());
5 | order.Export(new JsonExportService());
--------------------------------------------------------------------------------
/Finished sample/Strategy/Strategy.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/TemplateMethod/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace TemplateMethod
8 | {
9 | ///
10 | /// AbstractClass
11 | ///
12 | public abstract class MailParser
13 | {
14 | public virtual void FindServer()
15 | {
16 | Console.WriteLine("Finding server...");
17 | }
18 |
19 | public abstract void AuthenticateToServer();
20 |
21 |
22 | public virtual string ParseHtmlMailBody(string identifier)
23 | {
24 | Console.WriteLine("Parsing HTML mail body...");
25 | return $"This is the body of mail with id {identifier}";
26 | }
27 |
28 | ///
29 | /// Template method
30 | ///
31 | public string ParseMailBody(string identifier)
32 | {
33 | Console.WriteLine("Parsing mail body (in template method)...");
34 | FindServer();
35 | AuthenticateToServer();
36 | return ParseHtmlMailBody(identifier);
37 | }
38 | }
39 |
40 | public class ExchangeMailParser : MailParser
41 | {
42 | public override void AuthenticateToServer()
43 | {
44 | Console.WriteLine("Connecting to Exchange");
45 | }
46 | }
47 |
48 | public class ApacheMailParser : MailParser
49 | {
50 | public override void AuthenticateToServer()
51 | {
52 | Console.WriteLine("Connecting to Apache");
53 | }
54 | }
55 |
56 | public class EudoraMailParser : MailParser
57 | {
58 | public override void FindServer()
59 | {
60 | Console.WriteLine("Finding Eudora server through a custom algorithm...");
61 | }
62 | public override void AuthenticateToServer()
63 | {
64 | Console.WriteLine("Connecting to Eudora");
65 | }
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Finished sample/TemplateMethod/Program.cs:
--------------------------------------------------------------------------------
1 | using TemplateMethod;
2 |
3 | ExchangeMailParser exchangeMailParser = new();
4 | Console.WriteLine(exchangeMailParser.ParseMailBody("bf3a298c-9990-4b02-873d-3d3c98ad16d2"));
5 | Console.WriteLine();
6 |
7 | ApacheMailParser apacheMailParser = new();
8 | Console.WriteLine(apacheMailParser.ParseMailBody("07b8a8c7-c598-4b6c-9049-ecce9fe4a56b"));
9 | Console.WriteLine();
10 |
11 | EudoraMailParser eudoraMailParser = new();
12 | Console.WriteLine(eudoraMailParser.ParseMailBody("789fe935-ced2-4fbd-865b-172909560a93"));
--------------------------------------------------------------------------------
/Finished sample/TemplateMethod/TemplateMethod.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Finished sample/Visitor/Implementation.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 |
7 | namespace Visitor
8 | {
9 | /////
10 | ///// Visitor
11 | /////
12 | //public interface IVisitor
13 | //{
14 | // void VisitCustomer(Customer customer);
15 | // void VisitEmployee(Employee employee);
16 | //}
17 |
18 | ///
19 | /// Visitor (alternative)
20 | ///
21 | public interface IVisitor
22 | {
23 | void Visit(IElement element);
24 | }
25 |
26 | ///
27 | /// Element
28 | ///
29 | public interface IElement
30 | {
31 | void Accept(IVisitor visitor);
32 | }
33 |
34 | ///
35 | /// ConcreteElement
36 | ///
37 | public class Customer : IElement
38 | {
39 | public decimal AmountOrdered { get; private set; }
40 | public decimal Discount { get; set; }
41 | public string Name { get; private set; }
42 |
43 | public Customer(string name, decimal amountOrdered)
44 | {
45 | Name = name;
46 | AmountOrdered = amountOrdered;
47 | }
48 |
49 | public void Accept(IVisitor visitor)
50 | {
51 | // alternative
52 | // visitor.VisitCustomer(this);
53 | visitor.Visit(this);
54 | Console.WriteLine($"Visited {nameof(Customer)} {Name}, discount given: {Discount}");
55 | }
56 | }
57 |
58 | ///
59 | /// ConcreteElement
60 | ///
61 | public class Employee : IElement
62 | {
63 | public int YearsEmployed { get; private set; }
64 | public decimal Discount { get; set; }
65 | public string Name { get; private set; }
66 |
67 | public Employee(string name, int yearsEmployed)
68 | {
69 | Name = name;
70 | YearsEmployed = yearsEmployed;
71 | }
72 |
73 | public void Accept(IVisitor visitor)
74 | {
75 | // alternative
76 | // visitor.VisitEmployee(this);
77 | visitor.Visit(this);
78 | Console.WriteLine($"Visited {nameof(Employee)} {Name}, discount given: {Discount}");
79 | }
80 | }
81 |
82 | /////
83 | ///// ConcreteVisitor
84 | /////
85 | //public class DiscountVisitor : IVisitor
86 | //{
87 | // public decimal TotalDiscountGiven { get; set; }
88 |
89 | // public void VisitCustomer(Customer customer)
90 | // {
91 | // // percentage of total amount
92 | // var discount = customer.AmountOrdered / 10;
93 | // // set it on the customer
94 | // customer.Discount = discount;
95 | // // add it to the total amount
96 | // TotalDiscountGiven += discount;
97 | // }
98 |
99 | // public void VisitEmployee(Employee employee)
100 | // {
101 | // // fixed value depending on the amount of years employed
102 | // var discount = employee.YearsEmployed < 10 ? 100 : 200;
103 | // // set it on the employee
104 | // employee.Discount = discount;
105 | // // add it to the total amount
106 | // TotalDiscountGiven += discount;
107 | // }
108 | //}
109 |
110 | ///
111 | /// ConcreteVisitor (alternative)
112 | ///
113 | public class DiscountVisitor : IVisitor
114 | {
115 | public decimal TotalDiscountGiven { get; set; }
116 |
117 | public void Visit(IElement element)
118 | {
119 | if (element is Customer)
120 | {
121 | VisitCustomer((Customer)element);
122 | }
123 | else if (element is Employee)
124 | {
125 | VisitEmployee((Employee)element);
126 | }
127 | }
128 |
129 | private void VisitCustomer(Customer customer)
130 | {
131 | // percentage of total amount
132 | var discount = customer.AmountOrdered / 10;
133 | // set it on the customer
134 | customer.Discount = discount;
135 | // add it to the total amount
136 | TotalDiscountGiven += discount;
137 | }
138 |
139 | private void VisitEmployee(Employee employee)
140 | {
141 | // fixed value depending on the amount of years employed
142 | var discount = employee.YearsEmployed < 10 ? 100 : 200;
143 | // set it on the employee
144 | employee.Discount = discount;
145 | // add it to the total amount
146 | TotalDiscountGiven += discount;
147 | }
148 | }
149 |
150 | ///
151 | /// ObjectStructure
152 | ///
153 | public class Container
154 | {
155 | public List Employees { get; set; } = new();
156 | public List Customers { get; set; } = new();
157 |
158 | public void Accept(IVisitor visitor)
159 | {
160 | foreach (var employee in Employees)
161 | {
162 | employee.Accept(visitor);
163 | }
164 | foreach (var customer in Customers)
165 | {
166 | customer.Accept(visitor);
167 | }
168 | }
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/Finished sample/Visitor/Program.cs:
--------------------------------------------------------------------------------
1 | using Visitor;
2 |
3 | // create container & add conrete elements
4 | var container = new Container();
5 | container.Customers.Add(new Customer("Sophie", 500));
6 | container.Customers.Add(new Customer("Karen", 1000));
7 | container.Customers.Add(new Customer("Sven", 800));
8 | container.Employees.Add(new Employee("Kevin", 18));
9 | container.Employees.Add(new Employee("Tom", 5));
10 |
11 | // create visitor
12 | DiscountVisitor discountVisitor = new();
13 |
14 | // pass it through
15 | container.Accept(discountVisitor);
16 |
17 | // write out gathered amount
18 | Console.WriteLine($"Total discount: {discountVisitor.TotalDiscountGiven}");
--------------------------------------------------------------------------------
/Finished sample/Visitor/Visitor.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # C#10 Design Patterns
2 | Fully functioning sample code for my C#10 Design Patterns course over at Pluralsight. The samples cover all 23 Gang of Four design patterns, with a real-life example for each pattern.
3 |
--------------------------------------------------------------------------------
/Starter solution/AbstractFactory/AbstractFactory.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/AbstractFactory/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace AbstractFactory
2 | {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Starter solution/AbstractFactory/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Abstract Factory";
--------------------------------------------------------------------------------
/Starter solution/Adapter/Adapter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Adapter/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Adapter";
--------------------------------------------------------------------------------
/Starter solution/Adapter/Properties/launchSettings.json:
--------------------------------------------------------------------------------
1 | {
2 | "profiles": {
3 | "WSL": {
4 | "commandName": "WSL2",
5 | "distributionName": ""
6 | }
7 | }
8 | }
--------------------------------------------------------------------------------
/Starter solution/Bridge/Bridge.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Bridge/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Bridge
2 | {
3 | }
--------------------------------------------------------------------------------
/Starter solution/Bridge/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Bridge";
--------------------------------------------------------------------------------
/Starter solution/Builder/Builder.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Builder/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace BuilderPattern
2 | {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Starter solution/Builder/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Builder";
--------------------------------------------------------------------------------
/Starter solution/ChainOfResponsibility/ChainOfResponsibility.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/ChainOfResponsibility/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace ChainOfResponsibility
2 | {
3 | }
--------------------------------------------------------------------------------
/Starter solution/ChainOfResponsibility/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Chain of Responsibility";
--------------------------------------------------------------------------------
/Starter solution/Command/Command.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Command/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Command
2 | {
3 | }
--------------------------------------------------------------------------------
/Starter solution/Command/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Command";
--------------------------------------------------------------------------------
/Starter solution/Composite/Composite.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Composite/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Composite
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Composite/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Composite";
--------------------------------------------------------------------------------
/Starter solution/Decorator/Decorator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Decorator/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Decorator
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Decorator/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Decorator";
--------------------------------------------------------------------------------
/Starter solution/DesignPatterns.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 17
4 | VisualStudioVersion = 17.0.31903.59
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "A - Creational", "A - Creational", "{B0F237CE-4C3B-467B-AB3F-7FED90583AB9}"
7 | EndProject
8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "01 - Singleton", "01 - Singleton", "{482F9D07-99EE-4BF6-975F-295E89ECBCF9}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "02 - Factory Method", "02 - Factory Method", "{E8783E99-05CD-4F71-B166-0FC1B90FC2C2}"
11 | EndProject
12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "03 - Abstract Factory", "03 - Abstract Factory", "{4763D927-A2E7-4322-8134-7D24DDD947F1}"
13 | EndProject
14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FactoryMethod", "FactoryMethod\FactoryMethod.csproj", "{1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}"
15 | EndProject
16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AbstractFactory", "AbstractFactory\AbstractFactory.csproj", "{51C54352-1897-4749-BD39-660613C2235B}"
17 | EndProject
18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "04 -Builder", "04 -Builder", "{21752809-82A5-4212-8444-166F656129FC}"
19 | EndProject
20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Builder", "Builder\Builder.csproj", "{8552B7D4-35B3-4916-8D5D-D0479802BA35}"
21 | EndProject
22 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "05 - Prototype", "05 - Prototype", "{E0164F8E-AB94-4D3F-879A-6DB5F19FE84B}"
23 | EndProject
24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Prototype", "Prototype\Prototype.csproj", "{1C69FD15-5FD4-49AF-B31F-F2A3407364EC}"
25 | EndProject
26 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "B - Structural", "B - Structural", "{BA4514FF-AA9E-402C-B074-5A16FF91FD6E}"
27 | EndProject
28 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "06 - Adapter", "06 - Adapter", "{60637BBF-C1E5-47EF-9994-7870B51913A3}"
29 | EndProject
30 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Adapter", "Adapter\Adapter.csproj", "{4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}"
31 | EndProject
32 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "07 - Bridge", "07 - Bridge", "{7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2}"
33 | EndProject
34 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Bridge", "Bridge\Bridge.csproj", "{75E6E304-74F2-4D88-8589-4366B1A7684D}"
35 | EndProject
36 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "08 - Decorator", "08 - Decorator", "{46719157-346D-4BCE-B34D-55D2B5F2E93A}"
37 | EndProject
38 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Decorator", "Decorator\Decorator.csproj", "{12768030-04EF-4B1D-A0E9-7C96F6B73DBE}"
39 | EndProject
40 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "09 - Composite", "09 - Composite", "{E262BBA8-94AE-495C-85E6-FA54BA6A90A2}"
41 | EndProject
42 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Composite", "Composite\Composite.csproj", "{898A0BFF-C8CA-451D-BA92-DACABABFF53E}"
43 | EndProject
44 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "10 - Facade", "10 - Facade", "{BA606C2E-5A34-4290-AC23-58E5482CD300}"
45 | EndProject
46 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Facade", "Facade\Facade.csproj", "{2F4804B4-98BC-4DF6-8362-E96AA344A29A}"
47 | EndProject
48 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "11 - Proxy", "11 - Proxy", "{59DA4BDF-CFF5-4F06-8878-67F7900F37D7}"
49 | EndProject
50 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Proxy", "Proxy\Proxy.csproj", "{3BDFB38E-00D4-434E-8985-5D6661931089}"
51 | EndProject
52 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "12 - Flyweight", "12 - Flyweight", "{449D2089-E1E9-4097-8996-5E6E3D80B24C}"
53 | EndProject
54 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Flyweight", "Flyweight\Flyweight.csproj", "{ACCB5262-A3F5-4A77-864F-F27A3A3B5259}"
55 | EndProject
56 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Singleton", "Singleton\Singleton.csproj", "{347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}"
57 | EndProject
58 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "C - Behavioral", "C - Behavioral", "{EE357574-412D-4C9D-A167-5002DE164362}"
59 | EndProject
60 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "15 - Command", "15 - Command", "{7736AF2A-2B94-42FB-A793-5020973687C8}"
61 | EndProject
62 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Command", "Command\Command.csproj", "{BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}"
63 | EndProject
64 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "16 - Memento", "16 - Memento", "{C5BDE3AF-1138-4E87-8810-611687FFA522}"
65 | EndProject
66 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Memento", "Memento\Memento.csproj", "{176C219A-5E5A-4710-B360-E09DBC80690D}"
67 | EndProject
68 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "20 - State", "20 - State", "{0CC41725-98B1-4F2E-ACB9-3A0F47F65E97}"
69 | EndProject
70 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "State", "State\State.csproj", "{9D29533E-7C7F-4B72-9331-81EA8A9957D7}"
71 | EndProject
72 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "14 - Strategy", "14 - Strategy", "{62068BDC-394F-4A1E-825B-C7DE137900BB}"
73 | EndProject
74 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Strategy", "Strategy\Strategy.csproj", "{6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}"
75 | EndProject
76 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "17 - Mediator", "17 - Mediator", "{F71B79ED-777C-4707-9D38-CF15A476C55B}"
77 | EndProject
78 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mediator", "Mediator\Mediator.csproj", "{913A71BA-D3BC-41F1-BA44-888AB8658630}"
79 | EndProject
80 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "18 - Chain of Responsibility", "18 - Chain of Responsibility", "{71A327E1-BA13-466C-B1A6-FA4551E8A670}"
81 | EndProject
82 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ChainOfResponsibility", "ChainOfResponsibility\ChainOfResponsibility.csproj", "{8768B99D-DC81-4951-BA46-8F1D10631C69}"
83 | EndProject
84 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "19 - Observer", "19 - Observer", "{162BE83E-9D99-494B-8464-BB0BEE0BCC81}"
85 | EndProject
86 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Observer", "Observer\Observer.csproj", "{8FA532ED-DB9D-47E7-AD53-31881953993A}"
87 | EndProject
88 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "21 - Iterator", "21 - Iterator", "{818D8FDD-1C97-48D8-AEEF-A7F193AA9187}"
89 | EndProject
90 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Iterator", "Iterator\Iterator.csproj", "{D4FEF27B-1C98-495E-93EF-1AC36ED810AB}"
91 | EndProject
92 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "22 - Visitor", "22 - Visitor", "{173BC868-E7AE-43AE-A05E-FD9BEEA980C7}"
93 | EndProject
94 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Visitor", "Visitor\Visitor.csproj", "{E3A37190-E73D-4F91-A249-9B5D18CF83AF}"
95 | EndProject
96 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "13 - Template Method", "13 - Template Method", "{1622583F-64EB-4F30-8D38-E897FF8FA140}"
97 | EndProject
98 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemplateMethod", "TemplateMethod\TemplateMethod.csproj", "{97CB88F2-868A-457A-90EB-89F55B9CBBF4}"
99 | EndProject
100 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "23 - Interpreter", "23 - Interpreter", "{FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB}"
101 | EndProject
102 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Interpreter", "Interpreter\Interpreter.csproj", "{39C0A16A-B9F1-444B-9D5D-F46BBB84F746}"
103 | EndProject
104 | Global
105 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
106 | Debug|Any CPU = Debug|Any CPU
107 | Release|Any CPU = Release|Any CPU
108 | EndGlobalSection
109 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
110 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
111 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
112 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
113 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE}.Release|Any CPU.Build.0 = Release|Any CPU
114 | {51C54352-1897-4749-BD39-660613C2235B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
115 | {51C54352-1897-4749-BD39-660613C2235B}.Debug|Any CPU.Build.0 = Debug|Any CPU
116 | {51C54352-1897-4749-BD39-660613C2235B}.Release|Any CPU.ActiveCfg = Release|Any CPU
117 | {51C54352-1897-4749-BD39-660613C2235B}.Release|Any CPU.Build.0 = Release|Any CPU
118 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
119 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Debug|Any CPU.Build.0 = Debug|Any CPU
120 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Release|Any CPU.ActiveCfg = Release|Any CPU
121 | {8552B7D4-35B3-4916-8D5D-D0479802BA35}.Release|Any CPU.Build.0 = Release|Any CPU
122 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
123 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Debug|Any CPU.Build.0 = Debug|Any CPU
124 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Release|Any CPU.ActiveCfg = Release|Any CPU
125 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC}.Release|Any CPU.Build.0 = Release|Any CPU
126 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
127 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
128 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
129 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5}.Release|Any CPU.Build.0 = Release|Any CPU
130 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
131 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Debug|Any CPU.Build.0 = Debug|Any CPU
132 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Release|Any CPU.ActiveCfg = Release|Any CPU
133 | {75E6E304-74F2-4D88-8589-4366B1A7684D}.Release|Any CPU.Build.0 = Release|Any CPU
134 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
135 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
136 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
137 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE}.Release|Any CPU.Build.0 = Release|Any CPU
138 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
139 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Debug|Any CPU.Build.0 = Debug|Any CPU
140 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Release|Any CPU.ActiveCfg = Release|Any CPU
141 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E}.Release|Any CPU.Build.0 = Release|Any CPU
142 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
143 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Debug|Any CPU.Build.0 = Debug|Any CPU
144 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Release|Any CPU.ActiveCfg = Release|Any CPU
145 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A}.Release|Any CPU.Build.0 = Release|Any CPU
146 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
147 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Debug|Any CPU.Build.0 = Debug|Any CPU
148 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Release|Any CPU.ActiveCfg = Release|Any CPU
149 | {3BDFB38E-00D4-434E-8985-5D6661931089}.Release|Any CPU.Build.0 = Release|Any CPU
150 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
151 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Debug|Any CPU.Build.0 = Debug|Any CPU
152 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Release|Any CPU.ActiveCfg = Release|Any CPU
153 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259}.Release|Any CPU.Build.0 = Release|Any CPU
154 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
155 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Debug|Any CPU.Build.0 = Debug|Any CPU
156 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Release|Any CPU.ActiveCfg = Release|Any CPU
157 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C}.Release|Any CPU.Build.0 = Release|Any CPU
158 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
159 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Debug|Any CPU.Build.0 = Debug|Any CPU
160 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Release|Any CPU.ActiveCfg = Release|Any CPU
161 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D}.Release|Any CPU.Build.0 = Release|Any CPU
162 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
163 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Debug|Any CPU.Build.0 = Debug|Any CPU
164 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Release|Any CPU.ActiveCfg = Release|Any CPU
165 | {176C219A-5E5A-4710-B360-E09DBC80690D}.Release|Any CPU.Build.0 = Release|Any CPU
166 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
167 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Debug|Any CPU.Build.0 = Debug|Any CPU
168 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Release|Any CPU.ActiveCfg = Release|Any CPU
169 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7}.Release|Any CPU.Build.0 = Release|Any CPU
170 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
171 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Debug|Any CPU.Build.0 = Debug|Any CPU
172 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Release|Any CPU.ActiveCfg = Release|Any CPU
173 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812}.Release|Any CPU.Build.0 = Release|Any CPU
174 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
175 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Debug|Any CPU.Build.0 = Debug|Any CPU
176 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Release|Any CPU.ActiveCfg = Release|Any CPU
177 | {913A71BA-D3BC-41F1-BA44-888AB8658630}.Release|Any CPU.Build.0 = Release|Any CPU
178 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
179 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Debug|Any CPU.Build.0 = Debug|Any CPU
180 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Release|Any CPU.ActiveCfg = Release|Any CPU
181 | {8768B99D-DC81-4951-BA46-8F1D10631C69}.Release|Any CPU.Build.0 = Release|Any CPU
182 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
183 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Debug|Any CPU.Build.0 = Debug|Any CPU
184 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Release|Any CPU.ActiveCfg = Release|Any CPU
185 | {8FA532ED-DB9D-47E7-AD53-31881953993A}.Release|Any CPU.Build.0 = Release|Any CPU
186 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
187 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Debug|Any CPU.Build.0 = Debug|Any CPU
188 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Release|Any CPU.ActiveCfg = Release|Any CPU
189 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB}.Release|Any CPU.Build.0 = Release|Any CPU
190 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
191 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
192 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
193 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF}.Release|Any CPU.Build.0 = Release|Any CPU
194 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
196 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
197 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4}.Release|Any CPU.Build.0 = Release|Any CPU
198 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
199 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Debug|Any CPU.Build.0 = Debug|Any CPU
200 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Release|Any CPU.ActiveCfg = Release|Any CPU
201 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746}.Release|Any CPU.Build.0 = Release|Any CPU
202 | EndGlobalSection
203 | GlobalSection(SolutionProperties) = preSolution
204 | HideSolutionNode = FALSE
205 | EndGlobalSection
206 | GlobalSection(NestedProjects) = preSolution
207 | {482F9D07-99EE-4BF6-975F-295E89ECBCF9} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
208 | {E8783E99-05CD-4F71-B166-0FC1B90FC2C2} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
209 | {4763D927-A2E7-4322-8134-7D24DDD947F1} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
210 | {1CDF5F40-3F42-4339-9702-8BC7EA4D9DEE} = {E8783E99-05CD-4F71-B166-0FC1B90FC2C2}
211 | {51C54352-1897-4749-BD39-660613C2235B} = {4763D927-A2E7-4322-8134-7D24DDD947F1}
212 | {21752809-82A5-4212-8444-166F656129FC} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
213 | {8552B7D4-35B3-4916-8D5D-D0479802BA35} = {21752809-82A5-4212-8444-166F656129FC}
214 | {E0164F8E-AB94-4D3F-879A-6DB5F19FE84B} = {B0F237CE-4C3B-467B-AB3F-7FED90583AB9}
215 | {1C69FD15-5FD4-49AF-B31F-F2A3407364EC} = {E0164F8E-AB94-4D3F-879A-6DB5F19FE84B}
216 | {60637BBF-C1E5-47EF-9994-7870B51913A3} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
217 | {4E31DC34-C2C1-45C4-BC2D-21AFF98BDEB5} = {60637BBF-C1E5-47EF-9994-7870B51913A3}
218 | {7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
219 | {75E6E304-74F2-4D88-8589-4366B1A7684D} = {7A473B8C-26AF-4CAE-B3E5-E0F091F7F4D2}
220 | {46719157-346D-4BCE-B34D-55D2B5F2E93A} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
221 | {12768030-04EF-4B1D-A0E9-7C96F6B73DBE} = {46719157-346D-4BCE-B34D-55D2B5F2E93A}
222 | {E262BBA8-94AE-495C-85E6-FA54BA6A90A2} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
223 | {898A0BFF-C8CA-451D-BA92-DACABABFF53E} = {E262BBA8-94AE-495C-85E6-FA54BA6A90A2}
224 | {BA606C2E-5A34-4290-AC23-58E5482CD300} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
225 | {2F4804B4-98BC-4DF6-8362-E96AA344A29A} = {BA606C2E-5A34-4290-AC23-58E5482CD300}
226 | {59DA4BDF-CFF5-4F06-8878-67F7900F37D7} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
227 | {3BDFB38E-00D4-434E-8985-5D6661931089} = {59DA4BDF-CFF5-4F06-8878-67F7900F37D7}
228 | {449D2089-E1E9-4097-8996-5E6E3D80B24C} = {BA4514FF-AA9E-402C-B074-5A16FF91FD6E}
229 | {ACCB5262-A3F5-4A77-864F-F27A3A3B5259} = {449D2089-E1E9-4097-8996-5E6E3D80B24C}
230 | {347B70DD-B108-4DB3-B36E-01DC8ADB9C2C} = {482F9D07-99EE-4BF6-975F-295E89ECBCF9}
231 | {7736AF2A-2B94-42FB-A793-5020973687C8} = {EE357574-412D-4C9D-A167-5002DE164362}
232 | {BA3B3232-7FC9-43AF-9AFE-E4F5211B4F1D} = {7736AF2A-2B94-42FB-A793-5020973687C8}
233 | {C5BDE3AF-1138-4E87-8810-611687FFA522} = {EE357574-412D-4C9D-A167-5002DE164362}
234 | {176C219A-5E5A-4710-B360-E09DBC80690D} = {C5BDE3AF-1138-4E87-8810-611687FFA522}
235 | {0CC41725-98B1-4F2E-ACB9-3A0F47F65E97} = {EE357574-412D-4C9D-A167-5002DE164362}
236 | {9D29533E-7C7F-4B72-9331-81EA8A9957D7} = {0CC41725-98B1-4F2E-ACB9-3A0F47F65E97}
237 | {62068BDC-394F-4A1E-825B-C7DE137900BB} = {EE357574-412D-4C9D-A167-5002DE164362}
238 | {6E87E3A0-ECDB-47DD-B9E8-D9E31A27D812} = {62068BDC-394F-4A1E-825B-C7DE137900BB}
239 | {F71B79ED-777C-4707-9D38-CF15A476C55B} = {EE357574-412D-4C9D-A167-5002DE164362}
240 | {913A71BA-D3BC-41F1-BA44-888AB8658630} = {F71B79ED-777C-4707-9D38-CF15A476C55B}
241 | {71A327E1-BA13-466C-B1A6-FA4551E8A670} = {EE357574-412D-4C9D-A167-5002DE164362}
242 | {8768B99D-DC81-4951-BA46-8F1D10631C69} = {71A327E1-BA13-466C-B1A6-FA4551E8A670}
243 | {162BE83E-9D99-494B-8464-BB0BEE0BCC81} = {EE357574-412D-4C9D-A167-5002DE164362}
244 | {8FA532ED-DB9D-47E7-AD53-31881953993A} = {162BE83E-9D99-494B-8464-BB0BEE0BCC81}
245 | {818D8FDD-1C97-48D8-AEEF-A7F193AA9187} = {EE357574-412D-4C9D-A167-5002DE164362}
246 | {D4FEF27B-1C98-495E-93EF-1AC36ED810AB} = {818D8FDD-1C97-48D8-AEEF-A7F193AA9187}
247 | {173BC868-E7AE-43AE-A05E-FD9BEEA980C7} = {EE357574-412D-4C9D-A167-5002DE164362}
248 | {E3A37190-E73D-4F91-A249-9B5D18CF83AF} = {173BC868-E7AE-43AE-A05E-FD9BEEA980C7}
249 | {1622583F-64EB-4F30-8D38-E897FF8FA140} = {EE357574-412D-4C9D-A167-5002DE164362}
250 | {97CB88F2-868A-457A-90EB-89F55B9CBBF4} = {1622583F-64EB-4F30-8D38-E897FF8FA140}
251 | {FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB} = {EE357574-412D-4C9D-A167-5002DE164362}
252 | {39C0A16A-B9F1-444B-9D5D-F46BBB84F746} = {FBA7CDD7-9BF5-4272-9414-C9A4DB6D5BDB}
253 | EndGlobalSection
254 | GlobalSection(ExtensibilityGlobals) = postSolution
255 | SolutionGuid = {F84E1DA5-656F-4C72-9FBA-4A07AD3EE35E}
256 | EndGlobalSection
257 | EndGlobal
258 |
--------------------------------------------------------------------------------
/Starter solution/Facade/Facade.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Facade/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Facade
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Facade/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Facade";
--------------------------------------------------------------------------------
/Starter solution/FactoryMethod/FactoryMethod.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/FactoryMethod/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace FactoryMethod
2 | {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Starter solution/FactoryMethod/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Factory Method";
--------------------------------------------------------------------------------
/Starter solution/Flyweight/Flyweight.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Flyweight/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Flyweight
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Flyweight/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Flyweight";
--------------------------------------------------------------------------------
/Starter solution/Interpreter/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Interpreter
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Interpreter/Interpreter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Interpreter/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Interpreter";
--------------------------------------------------------------------------------
/Starter solution/Iterator/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Iterator
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Iterator/Iterator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Iterator/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Iterator";
--------------------------------------------------------------------------------
/Starter solution/Mediator/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Mediator
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Mediator/Mediator.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Mediator/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Mediator";
--------------------------------------------------------------------------------
/Starter solution/Memento/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Memento
2 | {
3 | }
--------------------------------------------------------------------------------
/Starter solution/Memento/Memento.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Memento/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Memento";
--------------------------------------------------------------------------------
/Starter solution/Observer/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Observer
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Observer/Observer.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Observer/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Observer";
--------------------------------------------------------------------------------
/Starter solution/Prototype/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Prototype
2 | {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Starter solution/Prototype/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Prototype";
--------------------------------------------------------------------------------
/Starter solution/Prototype/Prototype.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Proxy/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Proxy
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Proxy/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Proxy";
--------------------------------------------------------------------------------
/Starter solution/Proxy/Proxy.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Singleton/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Singleton
2 | {
3 |
4 | }
5 |
--------------------------------------------------------------------------------
/Starter solution/Singleton/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Singleton";
--------------------------------------------------------------------------------
/Starter solution/Singleton/Singleton.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/State/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace State
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/State/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "State";
--------------------------------------------------------------------------------
/Starter solution/State/State.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Strategy/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Strategy
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Strategy/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Strategy";
--------------------------------------------------------------------------------
/Starter solution/Strategy/Strategy.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/TemplateMethod/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace TemplateMethod
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/TemplateMethod/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Template Method";
--------------------------------------------------------------------------------
/Starter solution/TemplateMethod/TemplateMethod.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Starter solution/Visitor/Implementation.cs:
--------------------------------------------------------------------------------
1 | namespace Visitor
2 | {
3 | }
4 |
--------------------------------------------------------------------------------
/Starter solution/Visitor/Program.cs:
--------------------------------------------------------------------------------
1 | Console.Title = "Visitor";
--------------------------------------------------------------------------------
/Starter solution/Visitor/Visitor.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | net6.0
6 | enable
7 | enable
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------