├── .gitattributes
├── .gitignore
├── AbstractFactory
├── AbstractFactory.vcxproj
├── AbstractFactory.vcxproj.filters
├── Factory.h
├── Product.h
├── README.md
└── main.cpp
├── Adapter
├── Adaptee.h
├── Adapter.vcxproj
├── Adapter.vcxproj.filters
├── ITarget.h
├── README.md
└── main.cpp
├── Bridge
├── Bridge.vcxproj
├── Bridge.vcxproj.filters
├── Paint.h
├── README.md
├── Shape.h
└── main.cpp
├── Builder
├── Builder.vcxproj
├── Builder.vcxproj.filters
├── House.h
├── HouseBuilder.h
├── HouseDirector.h
├── README.md
└── main.cpp
├── Chain of Responsibility
├── Chain of Responsibility.vcxproj
├── Chain of Responsibility.vcxproj.filters
├── Handler.h
├── README.md
├── Request.h
└── main.cpp
├── Command
├── Command.h
├── Command.vcxproj
├── Command.vcxproj.filters
├── Image.h
├── README.md
└── main.cpp
├── Composite
├── Component.h
├── Composite.vcxproj
├── Composite.vcxproj.filters
├── README.md
└── main.cpp
├── Decorator
├── Canvas.h
├── Decorator.h
├── Decorator.vcxproj
├── Decorator.vcxproj.filters
├── README.md
└── main.cpp
├── DesignPatterns.sln
├── Facade
├── Facade.vcxproj
├── Facade.vcxproj.filters
├── MovieFacade.h
├── README.md
└── main.cpp
├── FactoryMethod
├── CarFactory.h
├── CarProduct.h
├── FactoryMethod.vcxproj
├── FactoryMethod.vcxproj.filters
├── README.md
└── main.cpp
├── Flyweight
├── Flyweight.vcxproj
├── Flyweight.vcxproj.filters
├── README.md
├── Tool.h
├── Toolbox.h
└── main.cpp
├── Interpreter
├── Analyzer.h
├── Expression.h
├── Interpreter.vcxproj
├── Interpreter.vcxproj.filters
├── README.md
└── main.cpp
├── Iterator
├── Aggregate.h
├── ConcreteAggregate.h
├── ConcreteIterator.h
├── Iterator.h
├── Iterator.vcxproj
├── Iterator.vcxproj.filters
├── README.md
└── main.cpp
├── Mediator
├── Mediator.cpp
├── Mediator.h
├── Mediator.vcxproj
├── Mediator.vcxproj.filters
├── Person.h
├── README.md
└── main.cpp
├── Memento
├── Memento.h
├── Memento.vcxproj
├── Memento.vcxproj.filters
├── Originator.h
├── README.md
└── main.cpp
├── Observer
├── 1.txt
├── Observer.h
├── Observer.vcxproj
├── Observer.vcxproj.filters
├── README.md
├── Subject.h
└── main.cpp
├── Prototype
├── Product.h
├── Prototype.vcxproj
├── Prototype.vcxproj.filters
├── README.md
└── main.cpp
├── Proxy
├── Proxy.cpp
├── Proxy.h
├── Proxy.vcxproj
├── Proxy.vcxproj.filters
├── README.md
├── Subject.h
├── SubjectImpl.cpp
├── SubjectImpl.h
└── main.cpp
├── README.md
├── Singleton
├── README.md
├── Singleton.h
├── Singleton.vcxproj
├── Singleton.vcxproj.filters
├── Singleton1.h
├── Singleton2.h
├── Singleton3.h
├── Singleton4.h
└── main.cpp
├── State
├── Person.cpp
├── Person.h
├── README.md
├── State.cpp
├── State.h
├── State.vcxproj
├── State.vcxproj.filters
└── main.cpp
├── Strategy
├── README.md
├── Sales.h
├── Strategy.vcxproj
├── Strategy.vcxproj.filters
└── main.cpp
├── Template
├── README.md
├── Template.h
├── Template.vcxproj
├── Template.vcxproj.filters
└── main.cpp
└── Visitor
├── Element.cpp
├── Element.h
├── README.md
├── Visitor.h
├── Visitor.vcxproj
├── Visitor.vcxproj.filters
└── main.cpp
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.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 | # Build results
17 | [Dd]ebug/
18 | [Dd]ebugPublic/
19 | [Rr]elease/
20 | [Rr]eleases/
21 | x64/
22 | x86/
23 | [Aa][Rr][Mm]/
24 | [Aa][Rr][Mm]64/
25 | bld/
26 | [Bb]in/
27 | [Oo]bj/
28 | [Ll]og/
29 |
30 | # Visual Studio 2015/2017 cache/options directory
31 | .vs/
32 | # Uncomment if you have tasks that create the project's static files in wwwroot
33 | #wwwroot/
34 |
35 | # Visual Studio 2017 auto generated files
36 | Generated\ Files/
37 |
38 | # MSTest test Results
39 | [Tt]est[Rr]esult*/
40 | [Bb]uild[Ll]og.*
41 |
42 | # NUNIT
43 | *.VisualState.xml
44 | TestResult.xml
45 |
46 | # Build Results of an ATL Project
47 | [Dd]ebugPS/
48 | [Rr]eleasePS/
49 | dlldata.c
50 |
51 | # Benchmark Results
52 | BenchmarkDotNet.Artifacts/
53 |
54 | # .NET Core
55 | project.lock.json
56 | project.fragment.lock.json
57 | artifacts/
58 |
59 | # StyleCop
60 | StyleCopReport.xml
61 |
62 | # Files built by Visual Studio
63 | *_i.c
64 | *_p.c
65 | *_h.h
66 | *.ilk
67 | *.meta
68 | *.obj
69 | *.iobj
70 | *.pch
71 | *.pdb
72 | *.ipdb
73 | *.pgc
74 | *.pgd
75 | *.rsp
76 | *.sbr
77 | *.tlb
78 | *.tli
79 | *.tlh
80 | *.tmp
81 | *.tmp_proj
82 | *_wpftmp.csproj
83 | *.log
84 | *.vspscc
85 | *.vssscc
86 | .builds
87 | *.pidb
88 | *.svclog
89 | *.scc
90 |
91 | # Chutzpah Test files
92 | _Chutzpah*
93 |
94 | # Visual C++ cache files
95 | ipch/
96 | *.aps
97 | *.ncb
98 | *.opendb
99 | *.opensdf
100 | *.sdf
101 | *.cachefile
102 | *.VC.db
103 | *.VC.VC.opendb
104 |
105 | # Visual Studio profiler
106 | *.psess
107 | *.vsp
108 | *.vspx
109 | *.sap
110 |
111 | # Visual Studio Trace Files
112 | *.e2e
113 |
114 | # TFS 2012 Local Workspace
115 | $tf/
116 |
117 | # Guidance Automation Toolkit
118 | *.gpState
119 |
120 | # ReSharper is a .NET coding add-in
121 | _ReSharper*/
122 | *.[Rr]e[Ss]harper
123 | *.DotSettings.user
124 |
125 | # JustCode is a .NET coding add-in
126 | .JustCode
127 |
128 | # TeamCity is a build add-in
129 | _TeamCity*
130 |
131 | # DotCover is a Code Coverage Tool
132 | *.dotCover
133 |
134 | # AxoCover is a Code Coverage Tool
135 | .axoCover/*
136 | !.axoCover/settings.json
137 |
138 | # Visual Studio code coverage results
139 | *.coverage
140 | *.coveragexml
141 |
142 | # NCrunch
143 | _NCrunch_*
144 | .*crunch*.local.xml
145 | nCrunchTemp_*
146 |
147 | # MightyMoose
148 | *.mm.*
149 | AutoTest.Net/
150 |
151 | # Web workbench (sass)
152 | .sass-cache/
153 |
154 | # Installshield output folder
155 | [Ee]xpress/
156 |
157 | # DocProject is a documentation generator add-in
158 | DocProject/buildhelp/
159 | DocProject/Help/*.HxT
160 | DocProject/Help/*.HxC
161 | DocProject/Help/*.hhc
162 | DocProject/Help/*.hhk
163 | DocProject/Help/*.hhp
164 | DocProject/Help/Html2
165 | DocProject/Help/html
166 |
167 | # Click-Once directory
168 | publish/
169 |
170 | # Publish Web Output
171 | *.[Pp]ublish.xml
172 | *.azurePubxml
173 | # Note: Comment the next line if you want to checkin your web deploy settings,
174 | # but database connection strings (with potential passwords) will be unencrypted
175 | *.pubxml
176 | *.publishproj
177 |
178 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
179 | # checkin your Azure Web App publish settings, but sensitive information contained
180 | # in these scripts will be unencrypted
181 | PublishScripts/
182 |
183 | # NuGet Packages
184 | *.nupkg
185 | # The packages folder can be ignored because of Package Restore
186 | **/[Pp]ackages/*
187 | # except build/, which is used as an MSBuild target.
188 | !**/[Pp]ackages/build/
189 | # Uncomment if necessary however generally it will be regenerated when needed
190 | #!**/[Pp]ackages/repositories.config
191 | # NuGet v3's project.json files produces more ignorable files
192 | *.nuget.props
193 | *.nuget.targets
194 |
195 | # Microsoft Azure Build Output
196 | csx/
197 | *.build.csdef
198 |
199 | # Microsoft Azure Emulator
200 | ecf/
201 | rcf/
202 |
203 | # Windows Store app package directories and files
204 | AppPackages/
205 | BundleArtifacts/
206 | Package.StoreAssociation.xml
207 | _pkginfo.txt
208 | *.appx
209 |
210 | # Visual Studio cache files
211 | # files ending in .cache can be ignored
212 | *.[Cc]ache
213 | # but keep track of directories ending in .cache
214 | !?*.[Cc]ache/
215 |
216 | # Others
217 | ClientBin/
218 | ~$*
219 | *~
220 | *.dbmdl
221 | *.dbproj.schemaview
222 | *.jfm
223 | *.pfx
224 | *.publishsettings
225 | orleans.codegen.cs
226 |
227 | # Including strong name files can present a security risk
228 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
229 | #*.snk
230 |
231 | # Since there are multiple workflows, uncomment next line to ignore bower_components
232 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
233 | #bower_components/
234 |
235 | # RIA/Silverlight projects
236 | Generated_Code/
237 |
238 | # Backup & report files from converting an old project file
239 | # to a newer Visual Studio version. Backup files are not needed,
240 | # because we have git ;-)
241 | _UpgradeReport_Files/
242 | Backup*/
243 | UpgradeLog*.XML
244 | UpgradeLog*.htm
245 | ServiceFabricBackup/
246 | *.rptproj.bak
247 |
248 | # SQL Server files
249 | *.mdf
250 | *.ldf
251 | *.ndf
252 |
253 | # Business Intelligence projects
254 | *.rdl.data
255 | *.bim.layout
256 | *.bim_*.settings
257 | *.rptproj.rsuser
258 | *- Backup*.rdl
259 |
260 | # Microsoft Fakes
261 | FakesAssemblies/
262 |
263 | # GhostDoc plugin setting file
264 | *.GhostDoc.xml
265 |
266 | # Node.js Tools for Visual Studio
267 | .ntvs_analysis.dat
268 | node_modules/
269 |
270 | # Visual Studio 6 build log
271 | *.plg
272 |
273 | # Visual Studio 6 workspace options file
274 | *.opt
275 |
276 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
277 | *.vbw
278 |
279 | # Visual Studio LightSwitch build output
280 | **/*.HTMLClient/GeneratedArtifacts
281 | **/*.DesktopClient/GeneratedArtifacts
282 | **/*.DesktopClient/ModelManifest.xml
283 | **/*.Server/GeneratedArtifacts
284 | **/*.Server/ModelManifest.xml
285 | _Pvt_Extensions
286 |
287 | # Paket dependency manager
288 | .paket/paket.exe
289 | paket-files/
290 |
291 | # FAKE - F# Make
292 | .fake/
293 |
294 | # JetBrains Rider
295 | .idea/
296 | *.sln.iml
297 |
298 | # CodeRush personal settings
299 | .cr/personal
300 |
301 | # Python Tools for Visual Studio (PTVS)
302 | __pycache__/
303 | *.pyc
304 |
305 | # Cake - Uncomment if you are using it
306 | # tools/**
307 | # !tools/packages.config
308 |
309 | # Tabs Studio
310 | *.tss
311 |
312 | # Telerik's JustMock configuration file
313 | *.jmconfig
314 |
315 | # BizTalk build output
316 | *.btp.cs
317 | *.btm.cs
318 | *.odx.cs
319 | *.xsd.cs
320 |
321 | # OpenCover UI analysis results
322 | OpenCover/
323 |
324 | # Azure Stream Analytics local run output
325 | ASALocalRun/
326 |
327 | # MSBuild Binary and Structured Log
328 | *.binlog
329 |
330 | # NVidia Nsight GPU debugger configuration file
331 | *.nvuser
332 |
333 | # MFractors (Xamarin productivity tool) working folder
334 | .mfractor/
335 |
336 | # Local History for Visual Studio
337 | .localhistory/
338 |
339 | # BeatPulse healthcheck temp database
340 | healthchecksdb
--------------------------------------------------------------------------------
/AbstractFactory/AbstractFactory.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/AbstractFactory/Factory.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Product.h"
3 |
4 | class DBFactory
5 | {
6 | public:
7 | virtual DBConnection* CreateDBConnection(std::string strConn) = 0;
8 | virtual DBCommand* CreateDBCommand(std::string strCmd) = 0;
9 | virtual ~DBFactory(void) {};
10 | };
11 |
12 | class SQLDBFactory : public DBFactory
13 | {
14 | public:
15 | virtual DBConnection* CreateDBConnection(std::string strConn)
16 | {
17 | return new SQLDBConnection(strConn);
18 | }
19 | virtual DBCommand* CreateDBCommand(std::string strCmd)
20 | {
21 | return new SQLDBCommand(strCmd);
22 | }
23 | };
24 |
25 | class OracleDBFactory : public DBFactory
26 | {
27 | public:
28 | virtual DBConnection* CreateDBConnection(std::string strConn)
29 | {
30 | return new OracleDBConnection(strConn);
31 | }
32 | virtual DBCommand* CreateDBCommand(std::string strCmd)
33 | {
34 | return new OracleDBCommand(strCmd);
35 | }
36 | };
37 |
38 |
--------------------------------------------------------------------------------
/AbstractFactory/Product.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 |
5 | class DBConnection
6 | {
7 | public:
8 | DBConnection(std::string strConn) {};
9 | virtual std::string Type() = 0;
10 |
11 | virtual ~DBConnection(void) {};
12 | };
13 |
14 | class SQLDBConnection : public DBConnection
15 | {
16 | public:
17 | SQLDBConnection(std::string strConn) : DBConnection(strConn) {}
18 |
19 | virtual std::string Type()
20 | {
21 | return "SQL";
22 | }
23 | };
24 |
25 | class OracleDBConnection : public DBConnection
26 | {
27 | public:
28 | OracleDBConnection(std::string strConn) : DBConnection(strConn) {}
29 |
30 | virtual std::string Type()
31 | {
32 | return "Oracle";
33 | }
34 | };
35 |
36 | class DBCommand
37 | {
38 | public:
39 | DBCommand(std::string strCmd) {}
40 | virtual bool Excute(DBConnection* dc) = 0;
41 | virtual ~DBCommand(void) {};
42 | };
43 |
44 | class SQLDBCommand : public DBCommand
45 | {
46 | public:
47 | SQLDBCommand(std::string strCmd) : DBCommand(strCmd) {}
48 | virtual bool Excute(DBConnection* dc)
49 | {
50 | if("SQL" != dc->Type())
51 | {
52 | std::cout << "not compatible connection!" << std::endl;
53 | return false;
54 | }
55 | std::cout << "sql command excuted!" << std::endl;
56 | return true;
57 | }
58 | };
59 |
60 | class OracleDBCommand : public DBCommand
61 | {
62 | public:
63 | OracleDBCommand(std::string strCmd) : DBCommand(strCmd) {}
64 | virtual bool Excute(DBConnection* dc)
65 | {
66 | if("Oracle" != dc->Type())
67 | {
68 | std::cout << "not compatible connection!" << std::endl;
69 | return false;
70 | }
71 | std::cout << "oracle command excuted!" << std::endl;
72 | return true;
73 | }
74 | };
--------------------------------------------------------------------------------
/AbstractFactory/README.md:
--------------------------------------------------------------------------------
1 | # AbstractFactroy
2 | ## 1 动机
3 | 一系列相互依赖的对象的创建工作;或由于需求变化,往往存在的更多系列对象的创建工作
4 | ## 2 定义
5 | 提供一个接口,让该接口负责一系列相关或相互依赖对象的创建工作,无需指定它们的具体类
6 | ## 3 特点
7 | - 如果不应对多系列对象的创建,则无需使用抽象工厂,用工厂方法即可。
8 | - 同一系列对象之间相互依赖或相互作用,不同系列对象之间不能相互依赖。
9 | - AbstractFactory主要应对系列对象的需求变动,缺点在于难以应对新对象的需求变动。
10 | ## 4 代码举例
11 | - DBFactory基类包含创建数据库连接DBConnection,和创建数据库命令DBCommand两个接口。
12 | - DBConnection与DBCommand基类,分别各自派生SQL与Oracle类型的子类。
13 | - Factory派生SQLSQLDBFactory和OracleSQLDBFactory子类,实现的两个创建接口,返回对应SQL或Oracle类型的连接与命令。
14 | - DBCommand执行Excute接口需要传入DBConnection作为参数,不同系列的DBCommand与DBConnection不能兼容。
--------------------------------------------------------------------------------
/AbstractFactory/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Product.h"
2 | #include "Factory.h"
3 |
4 | void main()
5 | {
6 | DBFactory* pFac = new SQLDBFactory();
7 |
8 | DBConnection* pConn = pFac->CreateDBConnection("some sql connection string");
9 | DBCommand* pCmd = pFac->CreateDBCommand("some sql command");
10 |
11 | pCmd->Excute(pConn);
12 | }
--------------------------------------------------------------------------------
/Adapter/Adaptee.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 |
5 | class IAdaptee
6 | {
7 | public:
8 | virtual void SpecificRequest() = 0;
9 | };
10 |
11 | class OldClass : public IAdaptee
12 | {
13 | public:
14 | virtual void SpecificRequest()
15 | {
16 | std::cout << "specific request!" << std::endl;
17 | }
18 | };
19 |
20 |
--------------------------------------------------------------------------------
/Adapter/Adapter.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {8C224153-51BF-4ABB-96B7-CFCB77B4FFF1}
24 | Adapter
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 |
91 |
92 | Console
93 | true
94 |
95 |
96 |
97 |
98 | Level3
99 | true
100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | true
102 |
103 |
104 | Console
105 | true
106 |
107 |
108 |
109 |
110 | Level3
111 | true
112 | true
113 | true
114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
115 | true
116 |
117 |
118 | Console
119 | true
120 | true
121 | true
122 |
123 |
124 |
125 |
126 | Level3
127 | true
128 | true
129 | true
130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
131 | true
132 |
133 |
134 | Console
135 | true
136 | true
137 | true
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/Adapter/Adapter.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Adapter/ITarget.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Adaptee.h"
3 |
4 | class ITarget
5 | {
6 | public:
7 | virtual void Request() = 0;
8 | virtual ~ITarget() {};
9 | };
10 |
11 | class Adapter : public ITarget
12 | {
13 | public:
14 | Adapter(IAdaptee* pAdaptee) : m_pAdaptee(pAdaptee)
15 | {}
16 | virtual void Request()
17 | {
18 | m_pAdaptee->SpecificRequest();
19 | }
20 | private:
21 | IAdaptee* m_pAdaptee;
22 | };
--------------------------------------------------------------------------------
/Adapter/README.md:
--------------------------------------------------------------------------------
1 | # Adapter
2 | ## 1 动机
3 | 一些现存的对象放在新环境中应用,但不满足新环境的接口要求。
4 | 如何应对在这种前移变化,利用现存对象的实现,同时满足新环境的要求。
5 | ## 2 定义
6 | 将一个类的接口转换成客户希望的另一种接口。是原来由于接口不兼容而不能一起工作的那些类可以一起工作
7 | ## 3 特点
8 | - 在希望复用一些现有类,但接口与复杂环境要求不一致的情况下很常用,如遗留代码复用,类库迁移等。
9 | - Adapter有对象适配器和类适配器。类适配器采用多继承实现,不推荐;对象适配器采用对象组合,更符合松耦合精神。
10 | - Adapter实现可以很灵活不必拘泥于具体形式,完全可以将现存对象作为新接口的方法参数,达到适配的目的。
11 | ## 4 代码举例
12 | - ITarget类的Request接口为需要使用的新环境下的接口
13 | - IAdaptee类派生的OldClass,包含现存类的SpecificRequest方法
14 | - ITarget派生一个适配器类,包含一个IAdaptee指针,实现Request接口,内部调用IAdaptee指针的SpecificRequest方法。
--------------------------------------------------------------------------------
/Adapter/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Adaptee.h"
2 | #include "ITarget.h"
3 |
4 | void main()
5 | {
6 | IAdaptee* pAdaptee = new OldClass();
7 | ITarget* pItarget = new Adapter(pAdaptee);
8 | pItarget->Request();
9 |
10 | delete pAdaptee; pAdaptee = nullptr;
11 | delete pItarget; pItarget = nullptr;
12 | }
--------------------------------------------------------------------------------
/Bridge/Bridge.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {90883A48-BA8E-4F2B-8756-FDFF43359021}
24 | Bridge
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 | true
91 |
92 |
93 | Console
94 | true
95 |
96 |
97 | true
98 |
99 |
100 |
101 |
102 | Level3
103 | true
104 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
105 | true
106 |
107 |
108 | Console
109 | true
110 |
111 |
112 |
113 |
114 | Level3
115 | true
116 | true
117 | true
118 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
119 | true
120 |
121 |
122 | Console
123 | true
124 | true
125 | true
126 |
127 |
128 |
129 |
130 | Level3
131 | true
132 | true
133 | true
134 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
135 | true
136 |
137 |
138 | Console
139 | true
140 | true
141 | true
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
153 |
154 |
--------------------------------------------------------------------------------
/Bridge/Bridge.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 源文件
20 |
21 |
22 |
23 |
24 | 头文件
25 |
26 |
27 | 头文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Bridge/Paint.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | class Paint
5 | {
6 | public:
7 | virtual void Draw(void) = 0;
8 | virtual ~Paint(void) {};
9 | };
10 |
11 |
12 | class RedPaint : public Paint
13 | {
14 | public:
15 | virtual void Draw()
16 | {
17 | std::cout << "Color Red!" << std::endl;
18 | }
19 | };
20 |
21 | class BluePaint : public Paint
22 | {
23 | public:
24 | virtual void Draw()
25 | {
26 | std::cout << "Color Blue!" << std::endl;
27 | }
28 | };
29 |
30 |
--------------------------------------------------------------------------------
/Bridge/README.md:
--------------------------------------------------------------------------------
1 | # Bridge
2 | ## 1 动机
3 | 由于某些类型的固有实现逻辑是它们具有两个乃至多个的变化维度。
4 | ## 2 定义
5 | 将抽象部分与它的实现部分分离,使它们都可以独立地变化。
6 | ## 3 特点
7 | - Bridge模式解耦了抽象与实现之间的固有绑定,使它们可以独立的沿着各自的维度变化。
8 | - Bridge有时类似于多继承方案,但多继承往往违背单一职责原则,复用性较差,Bridge比多继承方案更好。
9 | - Bridge应用在有两个非常强大变化维度的情况下,对于有多个变化维度是,可以使用Bridge扩展模式。
10 | ## 4 代码举例
11 | - 不同的图形Shape(如矩形、圆形……)可以显示不同的颜色(红色、蓝色……)。
12 | - 直接从具体图形类下派生出不同的颜色的图形类,会导致类的扩张。
13 | - 将显示Show业务的抽象与实现分离,实现交给Paint基类的Draw方法。
14 | - Shape基类包含Paint指针,Paint可派生子类以支持不同的颜色,如RedPaint、BluePaint……
--------------------------------------------------------------------------------
/Bridge/Shape.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "Paint.h"
3 |
4 | class Shape
5 | {
6 | public:
7 | Shape(Paint* pt) : m_pt(pt) {}
8 |
9 | virtual ~Shape(void) {};
10 |
11 | virtual void Show()
12 | {
13 | m_pt->Draw();
14 | }
15 |
16 | private:
17 | Paint* m_pt;
18 | };
19 |
20 | class Rect : public Shape
21 | {
22 | public:
23 | Rect(Paint* pt) : Shape(pt) {}
24 |
25 | virtual void Show()
26 | {
27 | std::cout << "Rect: ";
28 | Shape::Show();
29 | }
30 | };
31 |
32 |
33 | class Circle : public Shape
34 | {
35 | public:
36 | Circle(Paint* pt) : Shape(pt) {}
37 |
38 | virtual void Show()
39 | {
40 | std::cout << "Circle: ";
41 | Shape::Show();
42 | }
43 | };
44 |
--------------------------------------------------------------------------------
/Bridge/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Paint.h"
2 | #include "Shape.h"
3 |
4 | void main()
5 | {
6 | Paint* pt = new RedPaint();
7 | Shape* shp = new Circle(pt);
8 |
9 | shp->Show();
10 |
11 | delete pt; pt = nullptr;
12 | delete shp; shp = nullptr;
13 | }
--------------------------------------------------------------------------------
/Builder/Builder.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 | 头文件
26 |
27 |
28 |
29 |
30 | 源文件
31 |
32 |
33 |
--------------------------------------------------------------------------------
/Builder/House.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | class House
4 | {
5 | public:
6 | virtual ~House(void) {};
7 | };
8 |
9 |
10 | class StoneHouse : public House
11 | {
12 |
13 | };
14 |
15 | class GlassHouse : public House
16 | {
17 |
18 | };
19 |
--------------------------------------------------------------------------------
/Builder/HouseBuilder.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include "House.h"
4 |
5 | class HouseBuilder
6 | {
7 | public:
8 | virtual ~HouseBuilder(void) {};
9 | public:
10 | virtual void BuildGround() = 0;
11 | virtual void BuildWall() = 0;
12 | virtual void BuildWindowAndDoor() = 0;
13 | virtual void BuildRoof() = 0;
14 | virtual House* GetResult() = 0;
15 |
16 | private:
17 | House* m_pHouse;
18 | };
19 |
20 | class StoneHouseBuilder : public HouseBuilder
21 | {
22 | public:
23 | virtual void BuildGround()
24 | {
25 | std::cout << "build stone house goroud" << std::endl;
26 | }
27 | virtual void BuildWall()
28 | {
29 | std::cout << "build stone house wall" << std::endl;
30 | }
31 | virtual void BuildWindowAndDoor()
32 | {
33 | std::cout << "build stone house window and door" << std::endl;
34 | }
35 | virtual void BuildRoof()
36 | {
37 | std::cout << "build stone house roof" << std::endl;
38 | }
39 | virtual House* GetResult()
40 | {
41 | return new StoneHouse();
42 | }
43 | };
44 |
45 |
--------------------------------------------------------------------------------
/Builder/HouseDirector.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "HouseBuilder.h"
3 |
4 | class HouseDirector
5 | {
6 | public:
7 | HouseDirector(HouseBuilder* pHouseBuilder):m_pHouseBuilder(pHouseBuilder) {}
8 | virtual House* Construct()
9 | {
10 | m_pHouseBuilder->BuildGround();
11 | m_pHouseBuilder->BuildWall();
12 | m_pHouseBuilder->BuildWall();
13 | m_pHouseBuilder->BuildWall();
14 | m_pHouseBuilder->BuildWall();
15 | m_pHouseBuilder->BuildWindowAndDoor();
16 | m_pHouseBuilder->BuildRoof();
17 |
18 | return m_pHouseBuilder->GetResult();
19 | }
20 | private:
21 | HouseBuilder* m_pHouseBuilder;
22 | };
23 |
24 |
--------------------------------------------------------------------------------
/Builder/README.md:
--------------------------------------------------------------------------------
1 | # Builder
2 | ## 1 动机
3 | 一个复杂对象的创建工作由各个部分的子对象用一定算法构成,各部分面临剧烈变化,而组合算法稳定。
4 | ## 2 定义
5 | 将一个复杂对象的构建与其表示相分离,是同样的构建过程可以创建不同的表示。
6 | ## 3 特点
7 | - Builder用分步骤构建一个对象,分步骤是稳定算法,而复杂对象个各个部分经常变化。
8 | - 哪里变化就在那里封装。Builder用于应对复杂对象各部分频繁的需求变动,缺点在于应对分步骤构建算法的需求变动。
9 | - Builder模式中要注意不同语言中构造器内调用虚函数的差别。
10 | ## 4 代码举例
11 | - 建房子的流程打地基、砌墙、装门窗、装房顶等流程稳定不变。但房子类型不同,建造的各流程细节不同。
12 | - House基类派生StoneHouse石头房子,GlassHouse玻璃房子。
13 | - HouseBuilder基类提供打地基、砌墙、装门窗、装房顶、获取结果的操作接口,派生StoneHouseBuilder类,实现各个接口。
14 | - HouseDirector类,包含一个HouseBuilder指针和Construct方法,在方法内部调用构建房子的算法流程,并获取结果并返回。
15 | - 使用时HouseBuilder传入不同的HouseBuilder子类指针,通过Construct方法获取不同的House子类对象。
16 | ## 5 Tips
17 | C++构造函数调用虚函数是静态绑定,无法实现多态。
--------------------------------------------------------------------------------
/Builder/main.cpp:
--------------------------------------------------------------------------------
1 | #include "HouseDirector.h"
2 | #include "HouseBuilder.h"
3 |
4 | void main()
5 | {
6 | HouseBuilder* pHouseBuilder = new StoneHouseBuilder();
7 | HouseDirector* pHouseDirector = new HouseDirector(pHouseBuilder);
8 |
9 | House* pHouse = pHouseDirector->Construct();
10 |
11 | delete pHouse; pHouse = nullptr;
12 | delete pHouseDirector; pHouseDirector = nullptr;
13 | delete pHouseBuilder; pHouseBuilder = nullptr;
14 | }
--------------------------------------------------------------------------------
/Chain of Responsibility/Chain of Responsibility.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {B236B4D5-0DF5-4D12-A454-75E6603AAEB4}
24 | ChainofResponsibility
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 |
91 |
92 | Console
93 | true
94 |
95 |
96 |
97 |
98 | Level3
99 | true
100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | true
102 |
103 |
104 | Console
105 | true
106 |
107 |
108 |
109 |
110 | Level3
111 | true
112 | true
113 | true
114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
115 | true
116 |
117 |
118 | Console
119 | true
120 | true
121 | true
122 |
123 |
124 |
125 |
126 | Level3
127 | true
128 | true
129 | true
130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
131 | true
132 |
133 |
134 | Console
135 | true
136 | true
137 | true
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/Chain of Responsibility/Chain of Responsibility.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Chain of Responsibility/Handler.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include "Request.h"
4 |
5 | class Handler
6 | {
7 | public:
8 | Handler(Handler* pNextHandler) :
9 | m_pNextHandler(pNextHandler) {}
10 |
11 | void Handle(Request* pRequest)
12 | {
13 | if (canHandleRequest(pRequest))
14 | HandleRequest(pRequest);
15 | else{
16 | if (nullptr != m_pNextHandler)
17 | m_pNextHandler->Handle(pRequest);
18 | }
19 | }
20 | protected:
21 | virtual bool canHandleRequest(Request* pRequest) = 0;
22 | virtual void HandleRequest(Request* pRequest) = 0;
23 |
24 | private:
25 | Handler* m_pNextHandler;
26 | };
27 |
28 | class Boss : public Handler
29 | {
30 | public:
31 | Boss(Handler* pNextHandler) : Handler(pNextHandler) {}
32 | protected:
33 | virtual bool canHandleRequest(Request* pRequest)
34 | {
35 | return true;
36 | }
37 | virtual void HandleRequest(Request* pRequest)
38 | {
39 | std::cout << "Boss handled the request : " << pRequest->GetDescription() << std::endl;
40 | }
41 | };
42 |
43 | class Manager : public Handler
44 | {
45 | public:
46 | Manager(Handler* pNextHandler) : Handler(pNextHandler) {}
47 | protected:
48 | virtual bool canHandleRequest(Request* pRequest)
49 | {
50 | return RequestLevel::Request_Level3 == pRequest->GetRequestLevel()
51 | || RequestLevel::Request_Level2 == pRequest->GetRequestLevel();
52 |
53 | }
54 | virtual void HandleRequest(Request* pRequest)
55 | {
56 | std::cout << "Manager handled the request : " << pRequest->GetDescription() << std::endl;
57 | }
58 | };
59 |
60 | class Headman : public Handler
61 | {
62 | public:
63 | Headman(Handler* pNextHandler) : Handler(pNextHandler) {}
64 | protected:
65 | virtual bool canHandleRequest(Request* pRequest)
66 | {
67 | return RequestLevel::Request_Level3 == pRequest->GetRequestLevel();
68 | }
69 | virtual void HandleRequest(Request* pRequest)
70 | {
71 | std::cout << "Headman handled the request : " << pRequest->GetDescription() << std::endl;
72 | }
73 | };
--------------------------------------------------------------------------------
/Chain of Responsibility/README.md:
--------------------------------------------------------------------------------
1 | # Chain of responsibility
2 | ## 1 动机
3 | 一个请求被多个对象处理,每个请求在运行时只有一个接受者,如果显示指定,将给请求的发送者和接受者带来耦合。
4 | ## 2 定义
5 | 每个对象都有处理请求的机会,将这些对象连城一条链,沿着链条传递请求,知道有一个处理它为止。
6 | ## 3 特点
7 | - 一个请求可能有多个接受者,但最后真正的接受者只有一个,这时请求者和接受者的耦合可能出现变化脆弱的症状。
8 | - 运行时动态添/修改请求的处理职责;
9 | - 责任链到末尾仍不处理请求时,应当有一个缺省的处理机制。
10 | ## 4 代码举例
11 | - Request基类包含请求级别,请求描述。请求级别分为一二三级,一级最高。
12 | - Handler基类包含一个后继者的Handler指针,在构造函数中传入,以及canHandleRequest、HandleRequest两个虚函数,分别传入Request对象指针,由子类实现。
13 | - Handler基类包含Handle方法,传入Request对象指针,如果canHandleRequest判断自身可以处理请求,则调用HandleRequest完成处理,如果不能,交给后继者Handler的Handle函数处理。
14 | - Handler基类派生Headman组长、Manager经理、Boss老板三个类、Manager作为Headman后继,Boss作为Manager后继,Boss无需后继。
15 | - Headman可以处理三级请求如请假,Manager可以处理二级以下请求,如请求加薪,Boss可以处理所有请求,如请求升职。
--------------------------------------------------------------------------------
/Chain of Responsibility/Request.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | enum class RequestLevel
5 | {
6 | Request_Level1,
7 | Request_Level2,
8 | Request_Level3
9 | };
10 |
11 | class Request
12 | {
13 | public:
14 | Request(std::string strDescription, RequestLevel rl):
15 | m_strDescription(strDescription), m_RequestLevel(rl) {}
16 |
17 | std::string GetDescription()
18 | {
19 | return m_strDescription;
20 | }
21 |
22 | RequestLevel GetRequestLevel()
23 | {
24 | return m_RequestLevel;
25 | }
26 | private:
27 | std::string m_strDescription;
28 | RequestLevel m_RequestLevel;
29 | };
30 |
31 |
--------------------------------------------------------------------------------
/Chain of Responsibility/main.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Chain of Responsibility/main.cpp
--------------------------------------------------------------------------------
/Command/Command.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include "Image.h"
4 |
5 | class Command
6 | {
7 | public:
8 | virtual ~Command() {}
9 | virtual void Undo() = 0;
10 | virtual void Redo() = 0;
11 | virtual void Zooming(double fltScale) = 0;
12 | };
13 |
14 | class ImageProcessCommand : public Command
15 | {
16 | public:
17 | ImageProcessCommand(Image* pImgTarget)
18 | : m_pImgTarget(pImgTarget), m_nCurOperation(0){}
19 |
20 | virtual void Undo()
21 | {
22 | if (m_nCurOperation == 0) return;
23 | m_pImgTarget->Scale(1.0 / vecOperateState[m_nCurOperation - 1]);
24 | m_nCurOperation--;
25 | }
26 |
27 | virtual void Redo()
28 | {
29 | if (m_nCurOperation >= vecOperateState.size()) return;
30 | m_pImgTarget->Scale(vecOperateState[m_nCurOperation]);
31 | m_nCurOperation++;
32 | }
33 |
34 | virtual void Zooming(double fltScale)
35 | {
36 | m_pImgTarget->Scale(fltScale);
37 | vecOperateState.push_back(fltScale);
38 | m_nCurOperation++;
39 | }
40 |
41 | private:
42 | Image* m_pImgTarget;
43 | std::vector vecOperateState;
44 | int m_nCurOperation;
45 | };
46 |
47 |
--------------------------------------------------------------------------------
/Command/Command.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {9016B75A-9E09-4827-9C7A-C7CCC14DBDBD}
24 | Command
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 |
91 |
92 | Console
93 | true
94 |
95 |
96 |
97 |
98 | Level3
99 | true
100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | true
102 |
103 |
104 | Console
105 | true
106 |
107 |
108 |
109 |
110 | Level3
111 | true
112 | true
113 | true
114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
115 | true
116 |
117 |
118 | Console
119 | true
120 | true
121 | true
122 |
123 |
124 |
125 |
126 | Level3
127 | true
128 | true
129 | true
130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
131 | true
132 |
133 |
134 | Console
135 | true
136 | true
137 | true
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
150 |
--------------------------------------------------------------------------------
/Command/Command.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Command/Image.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | class Image
5 | {
6 | public:
7 | Image(int nWidth, int nHeight) :m_nWidth(nWidth), m_nHeight(nHeight) {}
8 |
9 | void Scale(double fltScale){
10 | if (0 < fltScale) {
11 | m_nHeight = (int)(m_nHeight * fltScale);
12 | m_nWidth = (int)(m_nWidth * fltScale);
13 | }
14 | }
15 |
16 | void Show()
17 | {
18 | std::cout << "Image info: " << m_nWidth << " * " << m_nHeight << std::endl;
19 | }
20 |
21 | private:
22 | int m_nWidth;
23 | int m_nHeight;
24 | };
25 |
26 |
--------------------------------------------------------------------------------
/Command/README.md:
--------------------------------------------------------------------------------
1 | # Command
2 | ## 1 动机
3 | - 行为请求者与行为实现者之间紧耦合,如对行为的记录、撤销、重做、事务处理等,这种不能抵御变化的紧耦合是不合适的。
4 | ## 2 定义
5 | - 将请求(行为)封装为对象,使得可以用不同的请求对客户进行参数化,对请求排队或记录请求日志,支持可撤销的操作。
6 | ## 3 特点
7 | - Command目的在于行为请求者和行为实现者解耦,将行为抽象为对象
8 | - 实现Command的具体子类有时需要保存一些额外的状态信息(记录命令执行信息,以便支持日志或Redo、Undo)
9 | - 可通过Composite模式将命令封装为复合命令
10 | - Command与C++的函数对象类似。Command以面向对象中的接口实现来定位行为接口规范,更严格但是性能有损失;函数对象用函数签名来定义行为接口规范,更灵活,性能更高。
11 | ## 4 代码举例
12 | - Image类包含SetScale方法,可以实现图片缩放比例。
13 | - Command基类提供Redo、Undo、缩放图片Zooming三个接口。
14 | - Command派生ImageProcessCommand(行为请求者),实现三个接口,包含Image指针(行为实现者)在构造函数中传入,包含一个历史缩放比例的数组,和当前操作游标(初始为0)
15 | - Zooming执行时,通过Image指针执行SetScale方法,数组压入缩放参数,游标加一
16 | - Undo时,获取缩放比例数组当前游标上一处的缩放比例,执行缩放的逆操作,游标减一;
17 | - Redo时,获取当前游标处的缩放比例执行缩放,游标加一。
18 | ## 5 Tips
19 | 有一种观点认为,设计模式的就是为了弥补特定语言设计的不足而出现的,成熟的设计模式慢慢称为语言本身所支持的一种特性。
--------------------------------------------------------------------------------
/Command/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Command.h"
2 | #include "Image.h"
3 |
4 | void main()
5 | {
6 | Image* pImage = new Image(400, 600);
7 | ImageProcessCommand ipc(pImage);
8 | ipc.Zooming(2.0);
9 | ipc.Zooming(2.0);
10 | pImage->Show();
11 |
12 | ipc.Undo();
13 | pImage->Show();
14 | ipc.Undo();
15 | pImage->Show();
16 |
17 | ipc.Redo();
18 | ipc.Redo();
19 | pImage->Show();
20 |
21 | delete pImage; pImage = nullptr;
22 | }
--------------------------------------------------------------------------------
/Composite/Component.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Composite/Component.h
--------------------------------------------------------------------------------
/Composite/Composite.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {DCAE5416-425D-4908-8F9D-C00D72DCEB9E}
24 | Composite
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 |
91 |
92 | Console
93 | true
94 |
95 |
96 |
97 |
98 | Level3
99 | true
100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | true
102 |
103 |
104 | Console
105 | true
106 |
107 |
108 |
109 |
110 | Level3
111 | true
112 | true
113 | true
114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
115 | true
116 |
117 |
118 | Console
119 | true
120 | true
121 | true
122 |
123 |
124 |
125 |
126 | Level3
127 | true
128 | true
129 | true
130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
131 | true
132 |
133 |
134 | Console
135 | true
136 | true
137 | true
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/Composite/Composite.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 |
23 |
24 | 源文件
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Composite/README.md:
--------------------------------------------------------------------------------
1 | # Composite
2 | ## 1 动机
3 | - 客户代码过多的依赖对象容器复杂的内部实现结构,对象内部实现结构(非抽象接口)的变化将引起客户代码频繁的变化,不易维护扩展。
4 | - 客户代码与复杂容器对象的解耦,让容器自己来实现复杂结构,客户用起来像简单对象一样。
5 | ## 2 定义
6 | 将对象组合成树形结构,以表示部分整体的层次结构,Composite使用户对单个对象和组合对象的使用具有一致性。
7 | ## 3 特点
8 | - Composite将客户与对象一对多的关系转换为一对一关系,客户可以一致地处理简单对象和容器(继承的同时,包含一对多的组合)。
9 | - 客户代码与抽象接口依赖,而不与对象容器的内部结构依赖,从而较好应对变化。
10 | - 具体实现中,可以让父对象中的子对象反向回溯;
11 | - 如果父对象有频繁的遍历需求,可使用缓存技巧改善效率
12 | ## 4 代码举例
13 | - Component抽象类包含了添加子对象Add、移除子对象Add、和Remove方法;
14 | - Component派生CompositeComponent组合类,包含一个Component类对象的容器,实现Add与Remove方法,Operation方法中循环调用容器中每一个Component对象的Operation方法。
15 | - Component派生LeafComponent叶对象,实现Add,Remove函数为空,表明叶对象无法添加子对象。
--------------------------------------------------------------------------------
/Composite/main.cpp:
--------------------------------------------------------------------------------
1 | #include "Component.h"
2 |
3 | void main()
4 | {
5 | Component* pRoot = new CompositeComponent("Root");
6 |
7 | Component* pType1 = new CompositeComponent("Type1");
8 | Component* pLeaf11 = new LeafComponent("Leaf1-1");
9 | Component* pLeaf12 = new LeafComponent("Leaf1-2");
10 | pType1->Add(pLeaf11);
11 | pType1->Add(pLeaf12);
12 |
13 | Component* pType2 = new CompositeComponent("Type2");
14 | Component* pLeaf21 = new LeafComponent("Leaf2-1");
15 | Component* pLeaf22 = new LeafComponent("Leaf2-2");
16 | pType2->Add(pLeaf21);
17 | pType2->Add(pLeaf22);
18 |
19 | pRoot->Add(pType1);
20 | pRoot->Add(pType2);
21 | pRoot->Process();
22 |
23 | delete pRoot; pRoot = nullptr;
24 | delete pType1; pType1 = nullptr;
25 | delete pLeaf11; pLeaf11 = nullptr;
26 | delete pLeaf12; pLeaf12 = nullptr;
27 | delete pType2; pType2 = nullptr;
28 | delete pLeaf21; pLeaf21 = nullptr;
29 | delete pLeaf22; pLeaf22 = nullptr;
30 |
31 |
32 | std::cin.get();
33 | }
--------------------------------------------------------------------------------
/Decorator/Canvas.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 |
5 | class Canvas
6 | {
7 | public:
8 | virtual void Draw() = 0;
9 | virtual ~Canvas(void) {};
10 | };
11 |
12 | class BlueCanvas : public Canvas
13 | {
14 | public:
15 | virtual void Draw()
16 | {
17 | std::cout << "blue canvas" <Draw();
11 | }
12 |
13 | protected:
14 | Canvas* m_canvas;
15 | };
16 |
17 | class BorderCanvas : public Decorator
18 | {
19 | public:
20 | BorderCanvas(Canvas* cvs) : Decorator(cvs) {}
21 | virtual void Draw()
22 | {
23 | m_canvas->Draw();
24 | addedOperation();
25 | }
26 |
27 | void addedOperation()
28 | {
29 | std::cout << "with border" <Draw();
40 | addedOperation();
41 | }
42 |
43 | void addedOperation()
44 | {
45 | std::cout << "with frame" <
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Decorator/README.md:
--------------------------------------------------------------------------------
1 | # Decorator
2 | ## 1 动机
3 | 某些情况下,过度使用继承来扩展对象功能,由于继承引入的静态特质,是扩展方式缺乏灵活性,同时子类增多,各种子类组合导致子类膨胀。
4 | ## 2 定义
5 | 动态给一个对象增加一些额外的职责,就增加功能而言Decarator比生成子类更加灵活。可以消除重复代码,减少类数量。
6 | ## 3 特点
7 | - 在接口上表现为IS-A,但实现上表现为HAS-A,同时使用了继承与组合。
8 | - 重点并非为了解决多子类衍生问题,重点在于解决主题类在多个方向上的功能扩展。
9 | ## 4 代码举例
10 | - 假设主体类n个, 画布Canvas基类如n个不同颜色的画布BlueCanvas、RedCanvas……实现基类Draw()接口。
11 | - 功能扩展方式有m个,对画布做不同形式的装饰,加边界、加相框等。BorderDecorator、FrameDecorator
12 | - 如果沿着主体类继承,类的总数为1+n+n*m,基类1个、不同颜色画布类n个、每个不同颜色进行不同装饰的新类n*m个
13 | - 如果采用装饰模式,将主体类与扩展类通过组合解耦,让主体类与扩展类分支继承,总的类数为,1+n+1+m。
14 | - Decorator类继承Canvas类,同时包含一个Canvas指针,构造函数中传入该指针;
15 | - Decorator派各个子类,如BorderDecorator,增加绘制边界的方法,实现Draw时,调用基类Canvas的Draw方法和绘制边界的方法,实现功能扩展。
--------------------------------------------------------------------------------
/Decorator/main.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Decorator/main.cpp
--------------------------------------------------------------------------------
/Facade/Facade.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | Win32
7 |
8 |
9 | Release
10 | Win32
11 |
12 |
13 | Debug
14 | x64
15 |
16 |
17 | Release
18 | x64
19 |
20 |
21 |
22 | 16.0
23 | {EF132754-2EA2-4C65-9F9C-67A362AB72F0}
24 | Facade
25 | 10.0
26 |
27 |
28 |
29 | Application
30 | true
31 | v142
32 | Unicode
33 |
34 |
35 | Application
36 | false
37 | v142
38 | true
39 | Unicode
40 |
41 |
42 | Application
43 | true
44 | v142
45 | Unicode
46 |
47 |
48 | Application
49 | false
50 | v142
51 | true
52 | Unicode
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 | true
74 |
75 |
76 | true
77 |
78 |
79 | false
80 |
81 |
82 | false
83 |
84 |
85 |
86 | Level3
87 | true
88 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
89 | true
90 |
91 |
92 | Console
93 | true
94 |
95 |
96 |
97 |
98 | Level3
99 | true
100 | _DEBUG;_CONSOLE;%(PreprocessorDefinitions)
101 | true
102 |
103 |
104 | Console
105 | true
106 |
107 |
108 |
109 |
110 | Level3
111 | true
112 | true
113 | true
114 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
115 | true
116 |
117 |
118 | Console
119 | true
120 | true
121 | true
122 |
123 |
124 |
125 |
126 | Level3
127 | true
128 | true
129 | true
130 | NDEBUG;_CONSOLE;%(PreprocessorDefinitions)
131 | true
132 |
133 |
134 | Console
135 | true
136 | true
137 | true
138 |
139 |
140 |
141 |
142 |
143 |
144 |
145 |
146 |
147 |
148 |
149 |
--------------------------------------------------------------------------------
/Facade/Facade.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 |
23 |
24 | 源文件
25 |
26 |
27 |
--------------------------------------------------------------------------------
/Facade/MovieFacade.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Facade/MovieFacade.h
--------------------------------------------------------------------------------
/Facade/README.md:
--------------------------------------------------------------------------------
1 | # Facade
2 | ## 1 动机
3 | 组件的客户和组件中的各个子系统有过多的耦合,随着客户和子系统的演化,过多的耦合面临挑战。如何解耦。
4 | ## 2 定义
5 | 为子系统中的一组接口提供一致(稳定)的界面,是这一子系统更容易使用(复用)。
6 | ## 3 特点
7 | - Facade实现了组件内部子系统与客户程序的解耦,子系统内部的变化不影响Facade接口的变化
8 | - 不使用Facade,子系统内部变化时,导致客户程序大量修改;使用Facade之后,客户程序少量甚至不用修改,只需要集中修改Facade接口的实现。
9 | - Facade更注重结构层次看系统,而不是从单个类的层次看系统。
10 | - Facade不是集装箱,Facade组件内部应该是相互耦合关系比较大的一些列组件(高内聚)。
11 | ## 4 代码举例
12 | - 看电影需要操作投影仪Projector、音响Amplifier、录像机DVDPlayer、灯光Light等设备。
13 | - 让客户去直接操作各类设备,难以较好的应对变化。
14 | - 设计一个MovieFacade类,公有继承Projector,Amplifier,DVDPlayer,Light,提供OpenMovie和CloseMovie方法,在方法内部操作设备。
15 | - 用户程序规避了复杂设备的使用,只需要使用MovieFacade的简单接口。
--------------------------------------------------------------------------------
/Facade/main.cpp:
--------------------------------------------------------------------------------
1 | #include "MovieFacade.h"
2 |
3 | void main()
4 | {
5 | MovieFacade* pMovieFacade = new MovieFacade();
6 | pMovieFacade->OpenMovie();
7 | pMovieFacade->CloseMovie();
8 | }
--------------------------------------------------------------------------------
/FactoryMethod/CarFactory.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include "CarProduct.h"
3 |
4 | class CarFactory
5 | {
6 | public:
7 | virtual CarProduct* CreateCar() = 0;
8 | virtual ~CarFactory(void) {};
9 | };
10 |
11 | class CarAudiFactory : public CarFactory
12 | {
13 | public:
14 | virtual CarProduct* CreateCar()
15 | {
16 | return new CarAudi();
17 | }
18 | };
19 |
20 | class CarBenzFactory : public CarFactory
21 | {
22 | public:
23 | virtual CarProduct* CreateCar()
24 | {
25 | return new CarBenz();
26 | }
27 | };
28 |
29 | class CarBmwFactory : public CarFactory
30 | {
31 | public:
32 | virtual CarProduct* CreateCar()
33 | {
34 | return new CarBmw();
35 | }
36 | };
--------------------------------------------------------------------------------
/FactoryMethod/CarProduct.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | class CarProduct
5 | {
6 | public:
7 | virtual std::string CarName(void) = 0;
8 | virtual ~CarProduct(void) {};
9 | };
10 |
11 | class CarAudi : public CarProduct
12 | {
13 | public:
14 | virtual std::string CarName(void)
15 | {
16 | return "Audi";
17 | }
18 | };
19 |
20 | class CarBenz : public CarProduct
21 | {
22 | public:
23 | virtual std::string CarName(void)
24 | {
25 | return "Benz";
26 | }
27 | };
28 |
29 | class CarBmw : public CarProduct
30 | {
31 | public:
32 | virtual std::string CarName(void)
33 | {
34 | return "BMW";
35 | }
36 | };
--------------------------------------------------------------------------------
/FactoryMethod/FactoryMethod.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/FactoryMethod/README.md:
--------------------------------------------------------------------------------
1 | # Factroy Method
2 | ## 1 动机
3 | 软件构建过程中经常面临对象创建工作,由于需求变化,创建的对象的具体类型经常变化。绕过常规的对象创建方法,提供一种封装机制来避免客户程序与创建工作的紧耦合。
4 | ## 2 定义
5 | 定义一个创建对象的接口,让子类决定实例化哪一个类,FactoryMethod使一个类的实例化延迟(解耦)的子类实现。
6 | ## 3 特点
7 | - 避免了类对象的使用者和具体类型之间的耦合。
8 | - 将具体对象的创建工作延迟到子类,实现了一种扩展而非更改的策略。
9 | - FactoryMethod解决单个对象的需求变化,缺点在于要求创建的方法和参数相同。
10 | ## 4 代码举例
11 | - CarProduct基类派生出AudiCar,BenzCar,BmwCar三个子类;
12 | - CarFactory基类提供CreateCar接口返回CarProduct类指针;
13 | - CarFactory基类派生出对应的AudiCarFactory,BenzCarFactory,BmwCarFactory三个工厂,实现CreateCar接口,new出对象的子类对象并返回;
14 | - 优化了简单工厂模式中工厂类的职责相对过重,增加新的产品需要修改工厂类的判断逻辑的弊端
15 | ## 5 Tips
16 | 设计模式或面向对象设计很多时候并不是把变化消灭掉了,而是赶到一个地方集中处理,而不是在软件系统中跳来跳去。
--------------------------------------------------------------------------------
/FactoryMethod/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | #include "CarFactory.h"
4 | #include "CarProduct.h"
5 |
6 | void main()
7 | {
8 | CarFactory* pFac = new CarAudiFactory();
9 | CarProduct* pCar = pFac->CreateCar();
10 |
11 | std::cout << pCar->CarName();
12 |
13 | delete pFac; pFac = nullptr;
14 | delete pCar; pCar = nullptr;
15 | }
--------------------------------------------------------------------------------
/Flyweight/Flyweight.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF}
6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx
7 |
8 |
9 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
10 | h;hh;hpp;hxx;hm;inl;inc;ipp;xsd
11 |
12 |
13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01}
14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms
15 |
16 |
17 |
18 |
19 | 头文件
20 |
21 |
22 | 头文件
23 |
24 |
25 |
26 |
27 | 源文件
28 |
29 |
30 |
--------------------------------------------------------------------------------
/Flyweight/README.md:
--------------------------------------------------------------------------------
1 | # Flyweight
2 | ## 1 动机
3 | 纯对象方案中,大量细粒度的对象充斥系统,从而带来运行代价(主要指内存);如何避免大量细粒度对象的问题,使用户透明的运用面向对象的方法进行操作。
4 | ## 2 定义
5 | 运用共享技术有效支持大量细粒度的对象。
6 | ## 3 特点
7 | - 解决了面向对象的代价问题,不触及面向对象的抽象问题。
8 | - 通过对象共享降低对象的个数,从而降低内存压力。
9 | - 具体实现方面要注意对象状态的处理。
10 | - 对象数量大导致内存开销要根据实际情况评估,不能凭空臆断。
11 | ## 4 代码举例
12 | - Tool工具类派生Hammer锤子类与Saw锯子类,工具有各自的名字Name和使用方法Use
13 | - ToolFactory使用简单工厂模式,根据名字完成工具对象的创建。
14 | - ToolBox类采用提供GetTool方法,通过工具名字返回工具对象。
15 | - ToolsBox包含map容器,GetTool时,优先查找返回创建过的工具对象,没创建过才新建,同时存入map并返回。
16 |
--------------------------------------------------------------------------------
/Flyweight/Tool.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 |
5 | class Tool
6 | {
7 | public:
8 | Tool(std::string strName) : m_strName(strName){}
9 | virtual ~Tool() {}
10 | virtual void Use() = 0;
11 |
12 | private:
13 | std::string m_strName;
14 | };
15 |
16 | class Hammer : public Tool
17 | {
18 | public:
19 | Hammer() : Tool("Hammer") {}
20 | virtual void Use()
21 | {
22 | std::cout << " Hammering!" << std::endl;
23 | }
24 | };
25 |
26 |
27 | class Saw : public Tool
28 | {
29 | public:
30 | Saw() : Tool("Saw") {}
31 | virtual void Use()
32 | {
33 | std::cout << " Sawing" << std::endl;
34 | }
35 | };
36 |
37 | class ToolFactory
38 | {
39 | public:
40 | Tool* CreateTool(std::string strName)
41 | {
42 | if ("Hammer" == strName)
43 | {
44 | return new Hammer();
45 | }
46 | else if ("Saw" == strName)
47 | {
48 | return new Saw();
49 | }
50 | else
51 | {
52 | return nullptr;
53 | }
54 | }
55 | };
--------------------------------------------------------------------------------
/Flyweight/Toolbox.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include