├── .gitignore
├── CSharp.DesignPatterns.AbstractFactory
├── App.config
├── CSharp.DesignPatterns.AbstractFactory.csproj
├── Contracts
│ └── IForwardingAgent.cs
├── Entities
│ ├── Address.cs
│ └── Order.cs
├── Factories
│ ├── BrazilForwardingAgentFactory.cs
│ ├── ForwardingAgentFactory.cs
│ ├── ForwardingAgentFactoryProvider.cs
│ └── USAForwardingAgentFactory.cs
├── ForwardingAgents
│ ├── DHLForwardingAgent.cs
│ ├── FictionalCompanyForwardingAgent.cs
│ └── SedexForwardingAgent.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── Services
│ └── OrderService.cs
├── CSharp.DesignPatterns.Adapter
├── App.config
├── CSharp.DesignPatterns.Adapter.csproj
├── Cache
│ ├── ApplicationCacheAdapter.cs
│ └── NullObjectCacheAdapter.cs
├── Contracts
│ ├── ICache.cs
│ └── IToDoRepository.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── Repositories
│ └── ToDoRepository.cs
└── Services
│ └── ToDoService.cs
├── CSharp.DesignPatterns.Builder
├── App.config
├── Builders
│ ├── BaseWebRequestBuilder.cs
│ ├── HttpWebRequestBuilder.cs
│ └── ProxyHttpWebRequestBuilder.cs
├── CSharp.DesignPatterns.Builder.csproj
├── Contracts
│ └── IWebRequestBuilder.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── Services
│ └── SearchService.cs
├── CSharp.DesignPatterns.Composite
├── Abstractions
│ └── File.cs
├── App.config
├── CSharp.DesignPatterns.Composite.csproj
├── Domain
│ ├── FileComposite.cs
│ ├── FolderFile.cs
│ └── SystemFile.cs
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── CSharp.DesignPatterns.Decorator
├── App.config
├── CSharp.DesignPatterns.Decorator.csproj
├── Contracts
│ ├── IPrice.cs
│ └── IProductRepository.cs
├── Decorators
│ └── ExchangeRatePriceDecorator.cs
├── Domain
│ ├── ECurrency.cs
│ ├── Price.cs
│ └── Product.cs
├── Extensions
│ └── PriceExtensions.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
├── Repositories
│ └── ProductRepository.cs
└── Services
│ └── ProductService.cs
├── CSharp.DesignPatterns.Observer
├── App.config
├── CSharp.DesignPatterns.Observer.csproj
├── Contracts
│ ├── INewsObserver.cs
│ └── NewsChangeEventArgs.cs
├── Domain
│ ├── Editor.cs
│ ├── News.cs
│ ├── NewsSection.cs
│ └── Reader.cs
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── CSharp.DesignPatterns.Proxy
├── App.config
├── CSharp.DesignPatterns.Proxy.csproj
├── Contracts
│ └── IImage.cs
├── Implementations
│ ├── HighResolutionImage.cs
│ └── ImageProxy.cs
├── Program.cs
├── Properties
│ ├── AssemblyInfo.cs
│ ├── Resources.Designer.cs
│ ├── Resources.resx
│ ├── Settings.Designer.cs
│ └── Settings.settings
├── frmImage.Designer.cs
├── frmImage.cs
└── frmImage.resx
├── CSharp.DesignPatterns.Singleton
├── App.config
├── CSharp.DesignPatterns.Singleton.csproj
├── Container
│ └── IoCContainer.cs
├── Contracts
│ └── IToDoRepository.cs
├── Program.cs
├── Properties
│ └── AssemblyInfo.cs
└── Repositories
│ ├── ToDoRepositoryADO.cs
│ └── ToDoRepositoryEntity.cs
├── CSharp.DesignPatterns.Strategy
├── App.config
├── CSharp.DesignPatterns.Strategy.csproj
├── Contracts
│ └── IBasketDiscountStrategy.cs
├── DiscountStrategies
│ ├── NoDiscountStrategy.cs
│ ├── PerItemPercentualDiscountStrategy.cs
│ └── PercentageDiscountStrategy.cs
├── Domain
│ ├── Basket.cs
│ ├── EDiscountType.cs
│ └── Product.cs
├── Factories
│ └── DiscountStrategyFactory.cs
├── Program.cs
└── Properties
│ └── AssemblyInfo.cs
├── CSharp.DesignPatterns.TemplateMethod
├── App.config
├── CSharp.DesignPatterns.TemplateMethod.csproj
├── Context
│ └── ExampleDbContext.cs
├── Mapping
│ ├── Base
│ │ └── MappingTemplate.cs
│ └── UserMapping.cs
├── Migrations
│ ├── 201707051924134_Test.Designer.cs
│ ├── 201707051924134_Test.cs
│ ├── 201707051924134_Test.resx
│ └── Configuration.cs
├── Model
│ └── User.cs
├── Properties
│ └── AssemblyInfo.cs
└── packages.config
├── CSharp.DesignPatterns.sln
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.suo
8 | *.user
9 | *.userosscache
10 | *.sln.docstates
11 |
12 | # User-specific files (MonoDevelop/Xamarin Studio)
13 | *.userprefs
14 |
15 | # Build results
16 | [Dd]ebug/
17 | [Dd]ebugPublic/
18 | [Rr]elease/
19 | [Rr]eleases/
20 | x64/
21 | x86/
22 | bld/
23 | [Bb]in/
24 | [Oo]bj/
25 | [Ll]og/
26 |
27 | # Visual Studio 2015 cache/options directory
28 | .vs/
29 | # Uncomment if you have tasks that create the project's static files in wwwroot
30 | #wwwroot/
31 |
32 | # MSTest test Results
33 | [Tt]est[Rr]esult*/
34 | [Bb]uild[Ll]og.*
35 |
36 | # NUNIT
37 | *.VisualState.xml
38 | TestResult.xml
39 |
40 | # Build Results of an ATL Project
41 | [Dd]ebugPS/
42 | [Rr]eleasePS/
43 | dlldata.c
44 |
45 | # .NET Core
46 | project.lock.json
47 | project.fragment.lock.json
48 | artifacts/
49 | **/Properties/launchSettings.json
50 |
51 | *_i.c
52 | *_p.c
53 | *_i.h
54 | *.ilk
55 | *.meta
56 | *.obj
57 | *.pch
58 | *.pdb
59 | *.pgc
60 | *.pgd
61 | *.rsp
62 | *.sbr
63 | *.tlb
64 | *.tli
65 | *.tlh
66 | *.tmp
67 | *.tmp_proj
68 | *.log
69 | *.vspscc
70 | *.vssscc
71 | .builds
72 | *.pidb
73 | *.svclog
74 | *.scc
75 |
76 | # Chutzpah Test files
77 | _Chutzpah*
78 |
79 | # Visual C++ cache files
80 | ipch/
81 | *.aps
82 | *.ncb
83 | *.opendb
84 | *.opensdf
85 | *.sdf
86 | *.cachefile
87 | *.VC.db
88 | *.VC.VC.opendb
89 |
90 | # Visual Studio profiler
91 | *.psess
92 | *.vsp
93 | *.vspx
94 | *.sap
95 |
96 | # TFS 2012 Local Workspace
97 | $tf/
98 |
99 | # Guidance Automation Toolkit
100 | *.gpState
101 |
102 | # ReSharper is a .NET coding add-in
103 | _ReSharper*/
104 | *.[Rr]e[Ss]harper
105 | *.DotSettings.user
106 |
107 | # JustCode is a .NET coding add-in
108 | .JustCode
109 |
110 | # TeamCity is a build add-in
111 | _TeamCity*
112 |
113 | # DotCover is a Code Coverage Tool
114 | *.dotCover
115 |
116 | # Visual Studio code coverage results
117 | *.coverage
118 | *.coveragexml
119 |
120 | # NCrunch
121 | _NCrunch_*
122 | .*crunch*.local.xml
123 | nCrunchTemp_*
124 |
125 | # MightyMoose
126 | *.mm.*
127 | AutoTest.Net/
128 |
129 | # Web workbench (sass)
130 | .sass-cache/
131 |
132 | # Installshield output folder
133 | [Ee]xpress/
134 |
135 | # DocProject is a documentation generator add-in
136 | DocProject/buildhelp/
137 | DocProject/Help/*.HxT
138 | DocProject/Help/*.HxC
139 | DocProject/Help/*.hhc
140 | DocProject/Help/*.hhk
141 | DocProject/Help/*.hhp
142 | DocProject/Help/Html2
143 | DocProject/Help/html
144 |
145 | # Click-Once directory
146 | publish/
147 |
148 | # Publish Web Output
149 | *.[Pp]ublish.xml
150 | *.azurePubxml
151 | # TODO: Comment the next line if you want to checkin your web deploy settings
152 | # but database connection strings (with potential passwords) will be unencrypted
153 | *.pubxml
154 | *.publishproj
155 |
156 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
157 | # checkin your Azure Web App publish settings, but sensitive information contained
158 | # in these scripts will be unencrypted
159 | PublishScripts/
160 |
161 | # NuGet Packages
162 | *.nupkg
163 | # The packages folder can be ignored because of Package Restore
164 | **/packages/*
165 | # except build/, which is used as an MSBuild target.
166 | !**/packages/build/
167 | # Uncomment if necessary however generally it will be regenerated when needed
168 | #!**/packages/repositories.config
169 | # NuGet v3's project.json files produces more ignorable files
170 | *.nuget.props
171 | *.nuget.targets
172 |
173 | # Microsoft Azure Build Output
174 | csx/
175 | *.build.csdef
176 |
177 | # Microsoft Azure Emulator
178 | ecf/
179 | rcf/
180 |
181 | # Windows Store app package directories and files
182 | AppPackages/
183 | BundleArtifacts/
184 | Package.StoreAssociation.xml
185 | _pkginfo.txt
186 |
187 | # Visual Studio cache files
188 | # files ending in .cache can be ignored
189 | *.[Cc]ache
190 | # but keep track of directories ending in .cache
191 | !*.[Cc]ache/
192 |
193 | # Others
194 | ClientBin/
195 | ~$*
196 | *~
197 | *.dbmdl
198 | *.dbproj.schemaview
199 | *.jfm
200 | *.pfx
201 | *.publishsettings
202 | orleans.codegen.cs
203 |
204 | # Since there are multiple workflows, uncomment next line to ignore bower_components
205 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
206 | #bower_components/
207 |
208 | # RIA/Silverlight projects
209 | Generated_Code/
210 |
211 | # Backup & report files from converting an old project file
212 | # to a newer Visual Studio version. Backup files are not needed,
213 | # because we have git ;-)
214 | _UpgradeReport_Files/
215 | Backup*/
216 | UpgradeLog*.XML
217 | UpgradeLog*.htm
218 |
219 | # SQL Server files
220 | *.mdf
221 | *.ldf
222 | *.ndf
223 |
224 | # Business Intelligence projects
225 | *.rdl.data
226 | *.bim.layout
227 | *.bim_*.settings
228 |
229 | # Microsoft Fakes
230 | FakesAssemblies/
231 |
232 | # GhostDoc plugin setting file
233 | *.GhostDoc.xml
234 |
235 | # Node.js Tools for Visual Studio
236 | .ntvs_analysis.dat
237 | node_modules/
238 |
239 | # Typescript v1 declaration files
240 | typings/
241 |
242 | # Visual Studio 6 build log
243 | *.plg
244 |
245 | # Visual Studio 6 workspace options file
246 | *.opt
247 |
248 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
249 | *.vbw
250 |
251 | # Visual Studio LightSwitch build output
252 | **/*.HTMLClient/GeneratedArtifacts
253 | **/*.DesktopClient/GeneratedArtifacts
254 | **/*.DesktopClient/ModelManifest.xml
255 | **/*.Server/GeneratedArtifacts
256 | **/*.Server/ModelManifest.xml
257 | _Pvt_Extensions
258 |
259 | # Paket dependency manager
260 | .paket/paket.exe
261 | paket-files/
262 |
263 | # FAKE - F# Make
264 | .fake/
265 |
266 | # JetBrains Rider
267 | .idea/
268 | *.sln.iml
269 |
270 | # CodeRush
271 | .cr/
272 |
273 | # Python Tools for Visual Studio (PTVS)
274 | __pycache__/
275 | *.pyc
276 |
277 | # Cake - Uncomment if you are using it
278 | # tools/**
279 | # !tools/packages.config
280 |
281 | # Telerik's JustMock configuration file
282 | *.jmconfig
283 |
284 | # BizTalk build output
285 | *.btp.cs
286 | *.btm.cs
287 | *.odx.cs
288 | *.xsd.cs
289 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/CSharp.DesignPatterns.AbstractFactory.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {C161C76B-3E4B-4D68-A598-32966813B3A1}
8 | Exe
9 | Properties
10 | CSharp.DesignPatterns.AbstractFactory
11 | CSharp.DesignPatterns.AbstractFactory
12 | v4.6.2
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
72 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Contracts/IForwardingAgent.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Entities;
2 |
3 | namespace CSharp.DesignPatterns.AbstractFactory.Contracts
4 | {
5 | ///
6 | /// Defines the contract of a forwarding agent.
7 | ///
8 | public interface IForwardingAgent
9 | {
10 | ///
11 | /// Generates a tracking id to an order.
12 | ///
13 | /// Order that needs a tracking id.
14 | void SetTrackingIdTo(Order order);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Entities/Address.cs:
--------------------------------------------------------------------------------
1 | namespace CSharp.DesignPatterns.AbstractFactory.Entities
2 | {
3 | ///
4 | /// Represents an address.
5 | ///
6 | public class Address
7 | {
8 | ///
9 | /// Country code.
10 | ///
11 | public ECountryCode CountryCode { get; private set; }
12 |
13 | public Address(ECountryCode countryCode)
14 | {
15 | CountryCode = countryCode;
16 | }
17 | }
18 |
19 | ///
20 | /// Country code.
21 | ///
22 | public enum ECountryCode : short
23 | {
24 | Brazil = 1,
25 | USA = 2
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Entities/Order.cs:
--------------------------------------------------------------------------------
1 | namespace CSharp.DesignPatterns.AbstractFactory.Entities
2 | {
3 | ///
4 | /// Represents an order requested to delivery.
5 | ///
6 | public class Order
7 | {
8 | ///
9 | /// Tracking Id generated to identify and to track the order.
10 | ///
11 | public string TrackingId { get; set; }
12 |
13 | ///
14 | /// Order's total price.
15 | ///
16 | public decimal TotalPrice { get; private set; }
17 |
18 | ///
19 | /// Order's weight. It's used to determine the forwarding agent which will delivery the order.
20 | ///
21 | public double Weight { get; private set; }
22 |
23 | ///
24 | /// Order's shipping address.
25 | ///
26 | public Address ShippingAddress { get; private set; }
27 |
28 |
29 | public Order(decimal totalPrice, double weight, Address shippingAddress)
30 | {
31 | TotalPrice = totalPrice;
32 | Weight = weight;
33 | ShippingAddress = shippingAddress;
34 | }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Factories/BrazilForwardingAgentFactory.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 | using CSharp.DesignPatterns.AbstractFactory.ForwardingAgents;
4 |
5 | namespace CSharp.DesignPatterns.AbstractFactory.Factories
6 | {
7 | ///
8 | /// Brazil forwading agents' factory.
9 | ///
10 | public class BrazilForwardingAgentFactory : ForwardingAgentFactory
11 | {
12 | ///
13 | /// Creates a forwarding agent according to an order.
14 | ///
15 | public override IForwardingAgent GetForwardingAgentFor(Order order)
16 | {
17 | if (order.Weight > 10 || order.TotalPrice > 1000)
18 | return new DHLForwardingAgent();
19 | return new SedexForwardingAgent();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Factories/ForwardingAgentFactory.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 |
4 | namespace CSharp.DesignPatterns.AbstractFactory.Factories
5 | {
6 | ///
7 | /// Generates forwarding agents accourding to an order.
8 | ///
9 | public abstract class ForwardingAgentFactory
10 | {
11 | ///
12 | /// Creates a forwarding agent according to an order.
13 | ///
14 | public abstract IForwardingAgent GetForwardingAgentFor(Order order);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Factories/ForwardingAgentFactoryProvider.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Entities;
2 | using System;
3 |
4 | namespace CSharp.DesignPatterns.AbstractFactory.Factories
5 | {
6 | ///
7 | /// Creates a fowarding agent factory accourding to a determined address.
8 | ///
9 | public static class ForwardingAgentFactoryProvider
10 | {
11 | ///
12 | /// Verifica o código do país do endereço para criar a factory de despachantes de correio.
13 | /// Verifies the country code and returns the respective forwarding agend factory.
14 | ///
15 | /// Address.
16 | /// Forwarding agent factory.
17 | public static ForwardingAgentFactory GetFactoryFor(Address address)
18 | {
19 | switch (address.CountryCode)
20 | {
21 | case ECountryCode.Brazil:
22 | return new BrazilForwardingAgentFactory();
23 | case ECountryCode.USA:
24 | return new USAForwardingAgentFactory();
25 | default:
26 | throw new NotImplementedException("There are not forwarding agents available for this country.");
27 | }
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Factories/USAForwardingAgentFactory.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 | using CSharp.DesignPatterns.AbstractFactory.ForwardingAgents;
4 |
5 | namespace CSharp.DesignPatterns.AbstractFactory.Factories
6 | {
7 | ///
8 | /// USA forwading agents' factory.
9 | ///
10 | public class USAForwardingAgentFactory : ForwardingAgentFactory
11 | {
12 | ///
13 | /// Creates a forwarding agent according to an order.
14 | ///
15 | public override IForwardingAgent GetForwardingAgentFor(Order order)
16 | {
17 | if (order.Weight > 10 || order.TotalPrice > 1000)
18 | return new DHLForwardingAgent();
19 | return new FictionalCompanyForwardingAgent();
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/ForwardingAgents/DHLForwardingAgent.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 |
4 | namespace CSharp.DesignPatterns.AbstractFactory.ForwardingAgents
5 | {
6 | ///
7 | /// DHL's forwarding agent provider.
8 | ///
9 | public class DHLForwardingAgent : IForwardingAgent
10 | {
11 | ///
12 | /// Generates a tracking id to an order.
13 | ///
14 | /// Order that needs a tracking id.
15 | public void SetTrackingIdTo(Order order)
16 | {
17 | // Hard-coded to simplify the example.
18 | order.TrackingId = "DHL-XXXX-XXXX-XXXX-XXXX";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/ForwardingAgents/FictionalCompanyForwardingAgent.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 |
4 | namespace CSharp.DesignPatterns.AbstractFactory.ForwardingAgents
5 | {
6 | ///
7 | /// Sedex' forwading agent service.
8 | ///
9 | public class FictionalCompanyForwardingAgent : IForwardingAgent
10 | {
11 | ///
12 | /// Generates a tracking id to an order.
13 | ///
14 | /// Order that needs a tracking id.
15 | public void SetTrackingIdTo(Order order)
16 | {
17 | // Hard-coded to simplify the example.
18 | order.TrackingId = "FCPNY-XXXX-XXXX-XXXX-XXXX";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/ForwardingAgents/SedexForwardingAgent.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 |
4 | namespace CSharp.DesignPatterns.AbstractFactory.ForwardingAgents
5 | {
6 | ///
7 | /// Sedex' forwading agent service.
8 | ///
9 | public class SedexForwardingAgent : IForwardingAgent
10 | {
11 | ///
12 | /// Generates a tracking id to an order.
13 | ///
14 | /// Order that needs a tracking id.
15 | public void SetTrackingIdTo(Order order)
16 | {
17 | // Hard-coded to simplify the example.
18 | order.TrackingId = "SEDEX-XXXX-XXXX-XXXX-XXXX";
19 | }
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Program.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Entities;
2 | using CSharp.DesignPatterns.AbstractFactory.Services;
3 | using System;
4 |
5 | namespace CSharp.DesignPatterns.AbstractFactory
6 | {
7 | class Program
8 | {
9 | ///
10 | /// Abstract Factory pattern example. In this example, we have an ecommerce application that has products
11 | /// to be delivered in different countries. Accourding to the shipping address of an order, there are
12 | /// different forwarding agents available to that country which can delivery the requests. The agent is
13 | /// determined at runtime by some criterias, as the order's weight and total price, and the country where
14 | /// the order must be delivered. Two abstract factories are used to determine these agents, one for Brazil
15 | /// and one for USA. A factory provider, which applies the Factory Method pattern, determines the forwarding
16 | /// agent factory that will be used. To simplify the example, these fowarding agents only create a tracking
17 | /// id to every order, simulating a traceability requisite of the system.
18 | ///
19 | /// The advantage to use this approach is to separete object creation responsability in a separeted class
20 | /// that is ready to expand if we need new forwarding agents. We don't need to spend lots of time changing
21 | /// the order service structure to add new countries and agents to the application.
22 | ///
23 | static void Main(string[] args)
24 | {
25 | var brazilAddress = new Address(ECountryCode.Brazil);
26 | var usaAddress = new Address(ECountryCode.USA);
27 |
28 | var brazilOrder1 = new Order(1100M, 12, brazilAddress);
29 | var brazilOrder2 = new Order(500M, 2, brazilAddress);
30 | var usaOrder1 = new Order(1100M, 12, usaAddress);
31 | var usaOrder2 = new Order(500M, 2, usaAddress);
32 |
33 | var orderService = new OrderService();
34 | orderService.Dispatch(brazilOrder1);
35 | orderService.Dispatch(brazilOrder2);
36 | orderService.Dispatch(usaOrder1);
37 | orderService.Dispatch(usaOrder2);
38 |
39 | Console.WriteLine("Brazil Order 1: " + brazilOrder1.TrackingId);
40 | Console.WriteLine("Brazil Order 2: " + brazilOrder2.TrackingId);
41 | Console.WriteLine("USA Order 1: " + usaOrder1.TrackingId);
42 | Console.WriteLine("USA Order 2: " + usaOrder2.TrackingId);
43 |
44 | Console.ReadKey();
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
1 | using System.Reflection;
2 | using System.Runtime.CompilerServices;
3 | using System.Runtime.InteropServices;
4 |
5 | // General Information about an assembly is controlled through the following
6 | // set of attributes. Change these attribute values to modify the information
7 | // associated with an assembly.
8 | [assembly: AssemblyTitle("CSharp.DesignPatterns.AbstractFactory")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("CSharp.DesignPatterns.AbstractFactory")]
13 | [assembly: AssemblyCopyright("Copyright © 2017")]
14 | [assembly: AssemblyTrademark("")]
15 | [assembly: AssemblyCulture("")]
16 |
17 | // Setting ComVisible to false makes the types in this assembly not visible
18 | // to COM components. If you need to access a type in this assembly from
19 | // COM, set the ComVisible attribute to true on that type.
20 | [assembly: ComVisible(false)]
21 |
22 | // The following GUID is for the ID of the typelib if this project is exposed to COM
23 | [assembly: Guid("c161c76b-3e4b-4d68-a598-32966813b3a1")]
24 |
25 | // Version information for an assembly consists of the following four values:
26 | //
27 | // Major Version
28 | // Minor Version
29 | // Build Number
30 | // Revision
31 | //
32 | // You can specify all the values or you can default the Build and Revision Numbers
33 | // by using the '*' as shown below:
34 | // [assembly: AssemblyVersion("1.0.*")]
35 | [assembly: AssemblyVersion("1.0.0.0")]
36 | [assembly: AssemblyFileVersion("1.0.0.0")]
37 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.AbstractFactory/Services/OrderService.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.AbstractFactory.Contracts;
2 | using CSharp.DesignPatterns.AbstractFactory.Entities;
3 | using CSharp.DesignPatterns.AbstractFactory.Factories;
4 |
5 | namespace CSharp.DesignPatterns.AbstractFactory.Services
6 | {
7 | ///
8 | /// Service that manipulates orders to dispatch them.
9 | ///
10 | public class OrderService
11 | {
12 | ///
13 | /// Dispatches an order.
14 | ///
15 | /// Order to dispatch.
16 | public void Dispatch(Order order)
17 | {
18 | ForwardingAgentFactory factory = ForwardingAgentFactoryProvider.GetFactoryFor(order.ShippingAddress);
19 | IForwardingAgent forwardingAgent = factory.GetForwardingAgentFor(order);
20 | forwardingAgent.SetTrackingIdTo(order);
21 | // Remaining code flow.
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/CSharp.DesignPatterns.Adapter.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {34E21A8D-E0F0-4189-89D1-F635E19963CD}
8 | Exe
9 | Properties
10 | CSharp.DesignPatterns.Adapter
11 | CSharp.DesignPatterns.Adapter
12 | v4.6.2
13 | 512
14 | true
15 |
16 |
17 | AnyCPU
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | AnyCPU
28 | pdbonly
29 | true
30 | bin\Release\
31 | TRACE
32 | prompt
33 | 4
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
68 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/Cache/ApplicationCacheAdapter.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.Adapter.Contracts;
2 | using System;
3 | using System.Runtime.Caching;
4 |
5 | namespace CSharp.DesignPatterns.Adapter.Cache
6 | {
7 | ///
8 | /// Adapter that uses application cache to store and retrieve data.
9 | ///
10 | public class ApplicationCacheAdapter : ICache
11 | {
12 | ///
13 | /// Internally uses memory cache to store and retrieve data.
14 | ///
15 | private ObjectCache _cache;
16 |
17 | public ApplicationCacheAdapter()
18 | {
19 | _cache = new MemoryCache("memory_cache");
20 | }
21 |
22 | ///
23 | /// Returns some data from cache.
24 | ///
25 | /// Object type to return.
26 | /// Key that identifies the object.
27 | /// Stored object.
28 | public T Get(string key) => (T)_cache.Get(key);
29 |
30 | ///
31 | /// Removes some data from cache.
32 | ///
33 | /// Key that identifies the object.
34 | public void Remove(string key)
35 | {
36 | _cache.Remove(key);
37 | }
38 |
39 | ///
40 | /// Stores some data in cache.
41 | ///
42 | /// Key to identify the object.
43 | /// Object to store.
44 | public void Store(string key, object @object)
45 | {
46 | _cache.Add(key, @object, new CacheItemPolicy { SlidingExpiration = TimeSpan.FromMinutes(30) });
47 | }
48 | }
49 | }
50 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/Cache/NullObjectCacheAdapter.cs:
--------------------------------------------------------------------------------
1 | using CSharp.DesignPatterns.Adapter.Contracts;
2 |
3 | namespace CSharp.DesignPatterns.Adapter.Cache
4 | {
5 | ///
6 | /// Null object pattern applied to cache adapter. It's used in situations we don't want to have cache.
7 | ///
8 | public class NullObjectCacheAdapter : ICache
9 | {
10 | ///
11 | /// Returns some data from cache.
12 | ///
13 | /// Object type to return.
14 | /// Key that identifies the object.
15 | /// Stored object.
16 | public T Get(string key) => default(T);
17 |
18 | ///
19 | /// Removes some data from cache.
20 | ///
21 | /// Key that identifies the object.
22 | public void Remove(string key) { }
23 |
24 | ///
25 | /// Stores some data in cache.
26 | ///
27 | /// Key to identify the object.
28 | /// Object to store.
29 | public void Store(string key, object @object) { }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/Contracts/ICache.cs:
--------------------------------------------------------------------------------
1 | namespace CSharp.DesignPatterns.Adapter.Contracts
2 | {
3 | ///
4 | /// Defines the methods a cache class must have to implement.
5 | ///
6 | public interface ICache
7 | {
8 | ///
9 | /// Removes some data from cache.
10 | ///
11 | /// Key that identifies the object.
12 | void Remove(string key);
13 |
14 | ///
15 | /// Stores some data in cache.
16 | ///
17 | /// Key to identify the object.
18 | /// Object to store.
19 | void Store(string key, object @object);
20 |
21 | ///
22 | /// Returns some data from cache.
23 | ///
24 | /// Object type to return.
25 | /// Key that identifies the object.
26 | /// Stored object.
27 | T Get(string key);
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/CSharp.DesignPatterns.Adapter/Contracts/IToDoRepository.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace CSharp.DesignPatterns.Adapter.Contracts
4 | {
5 | ///
6 | /// Fictional entity repository.
7 | ///
8 | public interface IToDoRepository
9 | {
10 | ///
11 | /// Returns a ToDo objects list given by a fictional category name.
12 | ///
13 | /// Category name.
14 | /// Object list.
15 | IList