├── .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 3 | 4 | #include "Tool.h" 5 | 6 | class ToolBox 7 | { 8 | public: 9 | ToolBox(ToolFactory* pFac) : m_pFac(pFac) {} 10 | 11 | virtual ~ToolBox() 12 | { 13 | for (auto iter = m_mapTools.begin(); iter != m_mapTools.end(); iter++) 14 | { 15 | delete (*iter).second; 16 | (*iter).second = nullptr; 17 | } 18 | } 19 | 20 | Tool* GetTool(std::string strName) 21 | { 22 | auto iter = m_mapTools.find(strName); 23 | if (iter != m_mapTools.end()) 24 | { 25 | return m_mapTools[strName]; 26 | } 27 | else 28 | { 29 | Tool* pTool = m_pFac->CreateTool(strName); 30 | if (nullptr != pTool) 31 | m_mapTools[strName] = pTool; 32 | return pTool; 33 | } 34 | } 35 | 36 | private: 37 | std::map m_mapTools; 38 | ToolFactory* m_pFac; 39 | }; 40 | 41 | -------------------------------------------------------------------------------- /Flyweight/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Toolbox.h" 2 | #include "Tool.h" 3 | 4 | void main() 5 | { 6 | ToolFactory* pFac = new ToolFactory(); 7 | ToolBox* pBox = new ToolBox(pFac); 8 | 9 | Tool* pTool = pBox->GetTool("Hammer"); 10 | pTool->Use(); 11 | 12 | } -------------------------------------------------------------------------------- /Interpreter/Analyzer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include "Expression.h" 5 | 6 | class Analyzer 7 | { 8 | public: 9 | Expression* Analyse(std::string strExpression) 10 | { 11 | std::stack stack; 12 | Expression* left = nullptr; 13 | Expression* right = nullptr; 14 | for (size_t i = 0; i < strExpression.size(); i++) 15 | { 16 | switch (strExpression[i]) 17 | { 18 | case '+': 19 | left = stack.top(); 20 | right = new VarExpression(strExpression[++i]); 21 | stack.push(new AddExpression(left, right)); 22 | break; 23 | case '-': 24 | left = stack.top(); 25 | right = new VarExpression(strExpression[++i]); 26 | stack.push(new SubExpression(left, right)); 27 | break; 28 | default: 29 | stack.push(new VarExpression(strExpression[i])); 30 | break; 31 | } 32 | } 33 | return stack.top(); 34 | } 35 | }; 36 | 37 | -------------------------------------------------------------------------------- /Interpreter/Expression.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class Expression 5 | { 6 | public: 7 | virtual int Interpreter(std::map var) = 0; 8 | virtual ~Expression() {} 9 | }; 10 | 11 | class VarExpression : public Expression 12 | { 13 | public: 14 | VarExpression(const char& key) : mKey(key) {} 15 | virtual int Interpreter(std::map var) 16 | { 17 | return var[mKey]; 18 | } 19 | private: 20 | char mKey; 21 | }; 22 | 23 | class SymbolExpression : public Expression 24 | { 25 | public: 26 | SymbolExpression(Expression* left, Expression* right) 27 | : pExpressionLeft(left), pExpressionRight(right) {} 28 | ~SymbolExpression() 29 | { 30 | delete pExpressionLeft; pExpressionLeft = nullptr; 31 | delete pExpressionRight; pExpressionRight = nullptr; 32 | } 33 | 34 | virtual int Interpreter(std::map var) = 0; 35 | protected: 36 | Expression* pExpressionLeft; 37 | Expression* pExpressionRight; 38 | }; 39 | 40 | class AddExpression : public SymbolExpression 41 | { 42 | public: 43 | AddExpression(Expression* left, Expression* right) 44 | : SymbolExpression(left, right) {} 45 | virtual int Interpreter(std::map var) 46 | { 47 | return pExpressionLeft->Interpreter(var) 48 | + pExpressionRight->Interpreter(var); 49 | } 50 | }; 51 | 52 | class SubExpression : public SymbolExpression 53 | { 54 | public: 55 | SubExpression(Expression* left, Expression* right) 56 | : SymbolExpression(left, right) {} 57 | virtual int Interpreter(std::map var) 58 | { 59 | return pExpressionLeft->Interpreter(var) 60 | - pExpressionRight->Interpreter(var); 61 | } 62 | }; 63 | -------------------------------------------------------------------------------- /Interpreter/Interpreter.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 | {1C66A9F8-A6DC-439F-BFB2-84C823F0F188} 24 | Interpreter 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 | -------------------------------------------------------------------------------- /Interpreter/Interpreter.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 | -------------------------------------------------------------------------------- /Interpreter/README.md: -------------------------------------------------------------------------------- 1 | # Interpreter 2 | ## 1 动机 3 | 某一领域问题比较复杂,类似结构不断重发发现,使用普通编程方式将面临频繁变化。将特定领域问题表达为某种规则语法下的句子,用解析器解析句子,从而解决问题。 4 | ## 2 定义 5 | 给定一个语言,定义他的文法的一种表示,并定义一种解析器,由解析器使用该表示来解析语言中的句子。 6 | ## 3 特点 7 | - Interpreter应用场合是本模式应用中的难点,需满足业务规则频繁变化,类似结构不断重复且容易抽象为语法规则。 8 | - Interpreter模式表示文法,可以用面向对象的方式扩展文法 9 | - Interpreter适用于简单文法,复杂文法(可能大致比较大的类层次结构)需要用语法分析生成器这样的标准工具。 10 | ## 4 代码举例 11 | - 实例代码展示的是通过解析器模式解决+-二则运算,map记录运算式中的每一个字符对应的数值。 12 | - Expression基类包含Interpreter接口,传入map,返回运算值int。 13 | - Expression派生VarExpression构造函数传入char,实现Interpreter,返回map中key为char的value 14 | - Expression派生SymbolExpression,包含left、right两个Express*指针,构造函数传入。 15 | - SymbolExpression派生AddExpression,SubExpression,分别返回left与right解析结果的和与差。 16 | - Analyzer类传入二则运算算式,通过栈操作解析算式,利用Expression解析算法,返回结果。 17 | 18 | -------------------------------------------------------------------------------- /Interpreter/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Analyzer.h" 3 | #include "Expression.h" 4 | 5 | void main() 6 | { 7 | std::string strExpression = "a+b-c+d"; 8 | std::map var; 9 | var.insert(std::make_pair('a', 5)); 10 | var.insert(std::make_pair('b', 3)); 11 | var.insert(std::make_pair('c', 1)); 12 | var.insert(std::make_pair('d', 9)); 13 | 14 | Analyzer analyzer; 15 | Expression* pExpression = analyzer.Analyse(strExpression); 16 | std::cout << "Result: " << pExpression->Interpreter(var) << std::endl; 17 | delete pExpression; 18 | 19 | std::cin.get(); 20 | } -------------------------------------------------------------------------------- /Iterator/Aggregate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include "Iterator.h" 4 | 5 | template 6 | class Aggregate 7 | { 8 | public: 9 | virtual ~Aggregate() {} 10 | virtual Iterator* CreateIterator() = 0; 11 | virtual void Push(T* t) = 0; 12 | virtual T* Pop(const int nIndex) = 0; 13 | virtual int Size() = 0; 14 | }; -------------------------------------------------------------------------------- /Iterator/ConcreteAggregate.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Aggregate.h" 3 | #include "ConcreteIterator.h" 4 | 5 | template 6 | class ConcreteAggregate : public Aggregate 7 | { 8 | public: 9 | ~ConcreteAggregate() 10 | { 11 | for (auto iter : m_vecValue) 12 | { 13 | delete iter; 14 | iter = nullptr; 15 | } 16 | m_vecValue.clear(); 17 | } 18 | virtual Iterator* CreateIterator() 19 | { 20 | return new ConcreteIterator(this); 21 | } 22 | virtual void Push(T* t) 23 | { 24 | m_vecValue.push_back(t); 25 | } 26 | virtual T* Pop(const int nIndex) 27 | { 28 | T* t(nullptr); 29 | if (nIndex < Size()) 30 | t = m_vecValue[nIndex]; 31 | return t; 32 | } 33 | virtual int Size() 34 | { 35 | return m_vecValue.size(); 36 | } 37 | 38 | public: 39 | std::vector m_vecValue; 40 | }; 41 | 42 | -------------------------------------------------------------------------------- /Iterator/ConcreteIterator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Iterator.h" 3 | #include "Aggregate.h" 4 | 5 | template 6 | class ConcreteIterator : public Iterator 7 | { 8 | public: 9 | ConcreteIterator(Aggregate* pAggregate) : m_pAggregate(pAggregate), m_nCurr(0) {} 10 | virtual void First() 11 | { 12 | m_nCurr = 0; 13 | } 14 | 15 | virtual void Next() 16 | { 17 | if (m_nCurr < m_pAggregate->Size()) 18 | m_nCurr++; 19 | } 20 | virtual bool IsDone() 21 | { 22 | return m_nCurr >= m_pAggregate->Size(); 23 | } 24 | virtual T* CurrentItem() 25 | { 26 | T* t(nullptr); 27 | if (m_nCurr < m_pAggregate->Size()) 28 | t = m_pAggregate->Pop(m_nCurr); 29 | return t; 30 | } 31 | private: 32 | Aggregate* m_pAggregate; 33 | int m_nCurr; 34 | }; 35 | -------------------------------------------------------------------------------- /Iterator/Iterator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | template 4 | class Iterator 5 | { 6 | public: 7 | virtual void First() = 0; 8 | virtual void Next() = 0; 9 | virtual bool IsDone() = 0; 10 | virtual T* CurrentItem() = 0; 11 | }; -------------------------------------------------------------------------------- /Iterator/Iterator.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 | {52BDFCE4-5C1D-4DA5-91BA-4C1D6308E97E} 24 | Iterator 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 | 151 | 152 | -------------------------------------------------------------------------------- /Iterator/Iterator.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 | 源文件 34 | 35 | 36 | -------------------------------------------------------------------------------- /Iterator/README.md: -------------------------------------------------------------------------------- 1 | # Iterator 2 | ## 1 动机 3 | 集合对象内部经常变化,不暴露内部结构的同时让外部客户透明地范文元素,这是同一算法应用在多个集合对象上称为可能。 4 | ## 2 定义 5 | 提供一种方法顺序访问一个聚合对象中的各种元素,而又不暴露该对象的内部表示。 6 | ## 3 特点 7 | - 迭代抽象:访问一个聚合对象内部,无需暴露内部表示。 8 | - 迭代多态:为遍历不同集合提供统一接口,支持同样算法应用在不同集合结构上 9 | - 健壮性考虑:遍历时更改所在集合的结构会导致问题。 10 | ## 4 代码举例 11 | - Aggregate基类提供创建迭代器CreateIterator、添加元素Push、获取指定序号元素Pop、获取集合大小Size方法。 12 | - Aggregate派生实际的集合类ConcreteAggregate类,内部包含一个元素T的数组对象,实现以上基类虚方法。 13 | - Iterator基类提供First、Next、IsDone、CurrentItem接口 14 | - Iterator派生实际迭代器类,包含一个集合指针和游标,构造函数时传入集合指零针,游标指; 15 | - Fisrt函数内游标归零、Next函数内游标在集合大小范围内加一、IsDone判断游标是否大于或等于集合大小、CurrentItem返回集合内序号为游标的对象。 -------------------------------------------------------------------------------- /Iterator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "ConcreteAggregate.h" 2 | #include "ConcreteIterator.h" 3 | #include 4 | 5 | void main() 6 | { 7 | Aggregate* pAggr = new ConcreteAggregate(); 8 | pAggr->Push(new int(1)); 9 | pAggr->Push(new int(2)); 10 | pAggr->Push(new int(3)); 11 | 12 | Iterator* pIter = pAggr->CreateIterator(); 13 | for (pIter->First(); !pIter->IsDone(); pIter->Next()) 14 | { 15 | std::cout << *(pIter->CurrentItem()) << std::endl; 16 | } 17 | 18 | delete pAggr; pAggr = nullptr; 19 | delete pIter; pIter = nullptr; 20 | } -------------------------------------------------------------------------------- /Mediator/Mediator.cpp: -------------------------------------------------------------------------------- 1 | #include "Mediator.h" 2 | 3 | #include "Person.h" 4 | 5 | 6 | HouseMediator::~HouseMediator() 7 | { 8 | auto iter1 = pListRenter.begin(); 9 | while (iter1 != pListRenter.end()) { 10 | delete* iter1; 11 | *iter1 = nullptr; 12 | iter1++; 13 | } 14 | pListRenter.clear(); 15 | 16 | auto iter2 = pListLandlord.begin(); 17 | while (iter2 != pListLandlord.end()) { 18 | delete* iter2; 19 | *iter2 = nullptr; 20 | iter2++; 21 | } 22 | pListLandlord.clear(); 23 | } 24 | 25 | void HouseMediator::AddRenter(Person* renter) 26 | { 27 | if (nullptr != renter) 28 | pListRenter.push_back(renter); 29 | } 30 | 31 | void HouseMediator::AddLandlord(Person* landlord) 32 | { 33 | if (nullptr != landlord) 34 | pListLandlord.push_back(landlord); 35 | } 36 | 37 | void HouseMediator::Send(Person* personFrom, const std::string& message) 38 | { 39 | if (Person::Type_Renter == personFrom->GetType()) 40 | { 41 | auto iter = pListLandlord.begin(); 42 | while (iter != pListLandlord.end()) { 43 | (*iter)->GetMessage(message); 44 | iter++; 45 | } 46 | } 47 | else 48 | { 49 | auto iter = pListRenter.begin(); 50 | while (iter != pListRenter.end()) { 51 | (*iter)->GetMessage(message); 52 | iter++; 53 | } 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /Mediator/Mediator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | 5 | class Person; 6 | 7 | class Mediator 8 | { 9 | public: 10 | virtual void Send(Person* personFrom, const std::string& message) = 0; 11 | virtual void AddRenter(Person* renter) = 0; 12 | virtual void AddLandlord(Person* landlord) = 0; 13 | 14 | virtual ~Mediator() {} 15 | }; 16 | 17 | class HouseMediator :public Mediator 18 | { 19 | public: 20 | ~HouseMediator(); 21 | 22 | virtual void AddRenter(Person* renter); 23 | virtual void AddLandlord(Person* landlord); 24 | 25 | 26 | virtual void Send(Person* personFrom, const std::string& message); 27 | 28 | private: 29 | std::list pListRenter; 30 | std::list pListLandlord; 31 | }; 32 | -------------------------------------------------------------------------------- /Mediator/Mediator.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 | {C01DC45E-6D6C-47C8-8811-E6FD7E8A0A05} 24 | Mediator 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 | 151 | -------------------------------------------------------------------------------- /Mediator/Mediator.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 | -------------------------------------------------------------------------------- /Mediator/Person.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Mediator/Person.h -------------------------------------------------------------------------------- /Mediator/README.md: -------------------------------------------------------------------------------- 1 | # Mediator 2 | ## 1 动机 3 | 软件构建过程中,存在对个对象互相关联交互的情况,对象之间维持复杂的引用关系,如果需求变化,引用关系面临不断的变化。 4 | ## 2 定义 5 | 用中介者封装一系列的交互对象,使各对象无需显式相互引用(编译时依赖转为运行时依赖),从而达到松耦合,并且可以独立的改变他们的交互。 6 | ## 3 特点 7 | - Mediator对多个对象的控制逻辑进行集中管理,“多个对象相互关联”变成“多个对象和一个中介者关联”,方便维护,可以抵御变化。 8 | - 随着控制逻辑的变化,Mediator可能变得相当复杂,可对Mediator对象进行分解处理。 9 | - Facade模式是系统间单向的对象关联关系的解耦;Mediator是系统内各个对象间双向关联关系的解耦。 10 | ## 4 代码举例 11 | - Mediator中介类派生房屋中介类HouseMediator,包含发消息Send,添加租客AddRenter,添加房东AddLandlord方法。 12 | - Person类,派生Renter租客类、Landlord房东类,子类构造函数提供中介者指针,构造时中介者内部,包含GetMessage与SendMessage方法。 13 | - Person子类SendMessage时,通过房屋中介者的Send方法发送消息。 14 | - 房屋中介者Send实现时,如果发消息的是租客,则让房东接收消息;如果发消息的是房东,则让租客接收消息。 -------------------------------------------------------------------------------- /Mediator/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Mediator/main.cpp -------------------------------------------------------------------------------- /Memento/Memento.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | typedef std::string State; 5 | 6 | class Originator; 7 | class Memento 8 | { 9 | friend class Originator; 10 | protected: 11 | Memento(State state) : mState(state) {} 12 | State GetState() 13 | { 14 | return mState; 15 | } 16 | 17 | void SetState(State state) 18 | { 19 | mState = state; 20 | } 21 | 22 | private: 23 | State mState; 24 | }; 25 | 26 | -------------------------------------------------------------------------------- /Memento/Memento.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 | {5CB663A5-3FA0-4D67-BDEE-9768413878A1} 24 | Memento 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 | -------------------------------------------------------------------------------- /Memento/Memento.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 | -------------------------------------------------------------------------------- /Memento/Originator.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Memento.h" 3 | 4 | class Originator 5 | { 6 | public: 7 | Originator(State state) : mState(state) {} 8 | void SetMemento(Memento* memento) 9 | { 10 | mState = memento->GetState(); 11 | } 12 | Memento* CreateMemento() 13 | { 14 | return new Memento(mState); 15 | } 16 | 17 | private: 18 | State mState; 19 | }; -------------------------------------------------------------------------------- /Memento/README.md: -------------------------------------------------------------------------------- 1 | # Memento 2 | ## 1 动机 3 | 某些对象在转换过程中,出于某种需要,要求能回溯到对象之前某个点时的状态,如果使用公有接口让其他对象的得到对象的状态,会暴露对象实现细节。 4 | ## 2 定义 5 | 不破坏封装性的前提下,捕获对象内部状态,并在该对象外部保存这个状态,以后可以将该对象回复之前的状态。 6 | ## 3 特点 7 | - Memento存储Originator对象的内部状态,需要时恢复。 8 | - Memento的核心是信息隐藏(内部的函数为Protected,不供外界调用,Originator为友元类,只给Originator使用),即Originator需要外界隐藏信息,保持封装性。 9 | - 现代语言运行时都有序列化支持,效率高而且准确,往往用序列化的方案实现Memento。 10 | ## 4 代码举例 11 | - Memento类包含GetState和SeState保护方法方法,生命Originator为友元类。 12 | - Originator包含State属性,提供CreateMemento方法,创建Memento备份。 13 | - 在必要时候调用SetMemento方法,传入Memento对象,调用对象GetState方法恢复状态。 14 | -------------------------------------------------------------------------------- /Memento/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Memento/main.cpp -------------------------------------------------------------------------------- /Observer/1.txt: -------------------------------------------------------------------------------- 1 | handling 1 of 4 2 | handling 2 of 4 3 | handling 3 of 4 4 | handling 4 of 4 5 | -------------------------------------------------------------------------------- /Observer/Observer.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class Subject; 4 | class Observer 5 | { 6 | public: 7 | virtual void Update(Subject* pSubject) = 0; 8 | virtual ~Observer(void) {}; 9 | }; 10 | 11 | -------------------------------------------------------------------------------- /Observer/Observer.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 | -------------------------------------------------------------------------------- /Observer/README.md: -------------------------------------------------------------------------------- 1 | # Observer 2 | ## 1 动机 3 | 通过建立依赖关系,一个对象状态变化,所有依赖的对象得到通知。如果依赖过于紧密,软件不能很好的抵御变化。 4 | ## 2 定义 5 | 定义对象一对多的依赖关系,当对象变化时,所有依赖它的对象的得到通知并自动更新。 6 | ## 3 特点 7 | - Stragety使我们可以独立改变目标和观察者,从而使二者松耦合。 8 | - 目标无需指定观察者,通知会自动传播 9 | - 基于事件的UI框架非常常用的设计模式,也是MVC模式的重要组成部分。 10 | ## 4 代码举例 11 | - 一个任务类,需要输出任务处理日志,支持控制台输出、文本输出;但后期可能需要支持其他输出方式。 12 | - 定义一个目标基类类,内部包含一个观察者列表,支持添加删除观察者,提供状态(本例以Log日志作为状态)的Get、Set接口 13 | - 定义一个观察者基类,提供Update更新接口,需要传入目标的指针。 14 | - 目标基类实现Notify通知方法,循环调用观察者的Update更新方法,传入this指针。 15 | - 目标基类派生任务类,任务run方法中,生成Log并调用SetLog;SetLog内部更新log字符串并Notify 16 | - 观察者基类派生控制台日志类和文本日志类,在Upddate更新方法中,通过目标的指针获取目标的Log信息,控制台日志打印输出,文本日志类写入txt文件。 17 | ## 5 Tips 18 | C++支持多继承,但是一般不推荐使用多继承,这会带来耦合性问题;推荐以一个主体类继承加多个抽象基类(接口)的方式实现。 -------------------------------------------------------------------------------- /Observer/Subject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | #include "Observer.h" 5 | 6 | class Subject 7 | { 8 | public: 9 | 10 | virtual ~Subject(void) {}; 11 | 12 | void addObserver(Observer* observer) 13 | { 14 | m_lstObserver.push_back(observer); 15 | } 16 | 17 | void removeObserver(Observer* observer) 18 | { 19 | m_lstObserver.remove(observer); 20 | } 21 | 22 | void Notify() 23 | { 24 | std::list::iterator iter = m_lstObserver.begin(); 25 | while (iter != m_lstObserver.end()) 26 | { 27 | (*iter)->Update(this); 28 | iter++; 29 | } 30 | } 31 | 32 | public: 33 | virtual std::string GetLog() = 0; 34 | virtual void SetLog(std::string strLog) = 0; 35 | 36 | private: 37 | std::list m_lstObserver; 38 | }; 39 | 40 | -------------------------------------------------------------------------------- /Observer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "Subject.h" 7 | #include "Observer.h" 8 | 9 | 10 | class ConsoleObserver : public Observer 11 | { 12 | public: 13 | virtual void Update(Subject* pSubject) 14 | { 15 | std::cout << pSubject->GetLog() << std::endl; 16 | } 17 | }; 18 | 19 | class LogObserver : public Observer 20 | { 21 | public: 22 | LogObserver(std::string strFile) { 23 | m_logger.open(strFile); 24 | } 25 | 26 | virtual void Update(Subject* pSubject) 27 | { 28 | m_logger << pSubject->GetLog() << std::endl; 29 | } 30 | 31 | ~LogObserver() 32 | { 33 | m_logger.close(); 34 | } 35 | 36 | private: 37 | std::ofstream m_logger; 38 | }; 39 | 40 | 41 | class Task : public Subject 42 | { 43 | public: 44 | void Run() 45 | { 46 | for (int i = 0; i < 4; i++) 47 | { 48 | std::stringstream ss; 49 | ss << "handling " << i + 1 << " of 4"; 50 | 51 | SetLog(ss.str()); 52 | } 53 | } 54 | 55 | virtual std::string GetLog() 56 | { 57 | return m_strLog; 58 | } 59 | 60 | virtual void SetLog(std::string strLog) 61 | { 62 | m_strLog = strLog; 63 | Notify(); 64 | } 65 | private: 66 | std::string m_strLog; 67 | 68 | }; 69 | 70 | void main() 71 | { 72 | Task task; 73 | 74 | ConsoleObserver co; 75 | LogObserver lo("1.txt"); 76 | 77 | task.addObserver(&co); 78 | task.addObserver(&lo); 79 | 80 | task.Run(); 81 | } -------------------------------------------------------------------------------- /Prototype/Product.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class Product 5 | { 6 | public: 7 | virtual Product* Clone() = 0; 8 | virtual void Show() {} 9 | virtual ~Product(void) {}; 10 | }; 11 | 12 | class NamedProduct : public Product 13 | { 14 | public: 15 | NamedProduct(const char* name) 16 | { 17 | if(name == nullptr) { 18 | mName = new char[1]; 19 | mName[0] = '\0'; 20 | } 21 | else { 22 | mName = new char[strlen(name)+1]; 23 | strcpy_s(mName, strlen(name) + 1, name); 24 | } 25 | } 26 | 27 | NamedProduct(const NamedProduct &np) 28 | { 29 | if(np.mName == nullptr) { 30 | mName = new char[1]; 31 | mName[0] = '\0'; 32 | } 33 | else { 34 | mName = new char[strlen(np.mName)+1]; 35 | strcpy_s(mName, strlen(np.mName) + 1, np.mName); 36 | } 37 | } 38 | 39 | ~NamedProduct() 40 | { 41 | delete mName; 42 | mName = nullptr; 43 | } 44 | 45 | virtual Product* Clone() 46 | { 47 | return new NamedProduct(*this); 48 | } 49 | 50 | virtual void Show() 51 | { 52 | std::cout << "Named Product: "<< mName < 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 | {5A8ED689-B83C-4FB9-A9E4-87ED82AF6E6D} 24 | Prototype 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 | -------------------------------------------------------------------------------- /Prototype/Prototype.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 | -------------------------------------------------------------------------------- /Prototype/README.md: -------------------------------------------------------------------------------- 1 | # Prototype 2 | ## 1 动机 3 | 结构复杂的对象的创建工作;由于需求的变化,这些对象经常面临剧烈变化,但是它们拥有稳定一直的接口。 4 | ## 2 定义 5 | 使用原型实例指定创建对象的的种类,然后通过拷贝这些原型来创建对象。 6 | ## 3 特点 7 | - Prototype同样用于隔离类对象的使用者与具体类型(易变类)之间的耦合,要求易变类具有稳定的接口 8 | - 通过原型克隆,使我们可以灵活地动态撞见拥有某些稳定接口的对象,所需工作只是注册一个新类的对象,在需要的地方clone。 9 | - Prototype模式的clone方法可以利用某些框架中的序列化来实现深拷贝。 10 | ## 4 代码举例 11 | - 定义Product基类,提供Clone接口,返回Product指针。 12 | - 派生类中实现Clone方法,使用C++的拷贝构造函数创建新对象,并返回。主要深拷贝和浅拷贝的差异。 -------------------------------------------------------------------------------- /Prototype/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Product.h" 2 | 3 | void main() 4 | { 5 | Product* product = new NamedProduct("hp computer"); 6 | 7 | Product* newProduct = product->Clone(); 8 | newProduct->Show(); 9 | 10 | delete product; product = nullptr; 11 | delete newProduct; newProduct = nullptr; 12 | 13 | } -------------------------------------------------------------------------------- /Proxy/Proxy.cpp: -------------------------------------------------------------------------------- 1 | #include "Proxy.h" 2 | #include "SubjectImpl.h" 3 | 4 | Proxy::Proxy() 5 | { 6 | m_pSubject = new SubjectImpl(); 7 | } 8 | 9 | void Proxy::Operation() 10 | { 11 | m_pSubject->Operation(); 12 | } 13 | -------------------------------------------------------------------------------- /Proxy/Proxy.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "Subject.h" 4 | 5 | class SubjectImpl; 6 | 7 | class Proxy : public Subject 8 | { 9 | public: 10 | Proxy(); 11 | virtual void Operation(); 12 | private: 13 | SubjectImpl* m_pSubject; 14 | }; 15 | 16 | 17 | -------------------------------------------------------------------------------- /Proxy/Proxy.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 | 源文件 34 | 35 | 36 | 源文件 37 | 38 | 39 | -------------------------------------------------------------------------------- /Proxy/README.md: -------------------------------------------------------------------------------- 1 | # Proxy 2 | ## 1 动机 3 | 在面向对象系统中,某些对象创建开销大,某些操作需要安全控制,或许要进程外的访问等原因,直接访问会给使用者或系统结构带来麻烦。 4 | ## 2 定义 5 | 为其他对象提供一些代理以控制(隔离,使用接口)对这个对象的访问 6 | ## 3 特点 7 | - 直接使用某些对象会带来很多问题,使用Proxy间接访问对象是常用解决手段。 8 | - Proxy实现的力度可能差异很多,可能是某个对象Copy On Write(写时复制机制),也可能是组件模块。 9 | - Proxy不要求接口完整性,只要能实现间接控制,损失一些透明性也可以接受。 10 | ## 4 代码举例 11 | - Subject基类包含Operation接口,派生SubjectImpl,但SubjectImpl头文件内部包含复杂结构, 12 | - Subject派生Proxy类,内部包含一个Subject对象指针,在Operation实现中调用该对象的的Operation方法。 13 | - 客户程序创建Proxy类对象,使用实际效果与SubjectImpl一样,但减少了客户对复杂结构的依赖。 -------------------------------------------------------------------------------- /Proxy/Subject.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | 6 | class Subject 7 | { 8 | public: 9 | virtual void Operation() = 0; 10 | virtual ~Subject() {} 11 | }; 12 | 13 | -------------------------------------------------------------------------------- /Proxy/SubjectImpl.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Proxy/SubjectImpl.cpp -------------------------------------------------------------------------------- /Proxy/SubjectImpl.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Subject.h" 3 | 4 | 5 | class SubjectImpl : public Subject 6 | { 7 | public: 8 | virtual void Operation(); 9 | 10 | private: 11 | std::map m_mapKeyValue1; 12 | std::map m_mapKeyValue2; 13 | std::map m_mapKeyValue3; 14 | std::map m_mapKeyValue4; 15 | std::map m_mapKeyValue5; 16 | std::map m_mapKeyValue6; 17 | }; 18 | 19 | -------------------------------------------------------------------------------- /Proxy/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Proxy/main.cpp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design Patterns 设计模式 2 | ## 创建型模式(Creational Pattern) 3 | 对类的实例化过程进行了抽象,能够将软件模块中对象的创建和对象的使用分离。 4 | - [抽象工厂模式(Abstract Factory)](https://github.com/ZhiminXu/DesignPatterns/tree/master/AbstractFactory) 5 | - [建造者模式(Builder)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Builder) 6 | - [工厂方法模式(Factory Method)](https://github.com/ZhiminXu/DesignPatterns/tree/master/FactoryMethod) 7 | - [原型模式(Prototype)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Prototype) 8 | - [单例模式(Singleton)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Singleton) 9 | ## 结构型模式(Structural Pattern) 10 | 结构型模式主要用于处理类或对象的组合,从而应对需求变化为对象结构带来的冲击。 11 | - [适配器模式(Adapter)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Adapter) 12 | - [桥接模式(Bridge)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Bridge) 13 | - [组合模式(Composite)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Composite) 14 | - [装饰模式(Decorator)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Decorator) 15 | - [外观模式(Facade)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Facade) 16 | - [享元模式(Flyweight)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Flyweight) 17 | - [代理模式(Proxy)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Proxy) 18 | ## 行为型模式(Behavioral Pattern) 19 | 行为型模式主要用于描述对类或对象怎样交互和怎样分配职责,即关注它们之间的相互作用。 20 | - [职责链模式(Chain of Responsibility)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Chain%20of%20Responsibility) 21 | - [命令模式(Command)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Command) 22 | - [解释器模式(Interpreter)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Interpreter) 23 | - [迭代器模式(Iterator)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Iterator) 24 | - [中介者模式(Mediator)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Mediator) 25 | - [备忘录模式(Memento)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Memento) 26 | - [观察者模式(Observer)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Observer) 27 | - [状态模式(State)](https://github.com/ZhiminXu/DesignPatterns/tree/master/State) 28 | - [策略模式(Strategy)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Strategy) 29 | - [模板方法模式(Template Method)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Template) 30 | - [访问者模式(Visitor)](https://github.com/ZhiminXu/DesignPatterns/tree/master/Visitor) 31 | -------------------------------------------------------------------------------- /Singleton/README.md: -------------------------------------------------------------------------------- 1 | # Singleton 2 | ## 1 动机 3 | 一些特殊类在系统中只能存在一个实例,才能保证逻辑正确或者良好的算法效率。绕开构造器,提供一个机制保证一个类只有一个实例。 4 | 这是类的设计者的责任而不是使用者的责任。 5 | ## 2 定义 6 | 保证一个类仅有一个实例,并提供一个该实例的全局访问点 7 | ## 3 特点 8 | - 实例构造器也可以设置为protected,为子类派生所用。 9 | - Singleton一般不要支持拷贝构造函数与Clonejiekou ,因为这可能导致多个对象实例,与设计初衷违背。 10 | - 要思考如何实现多线程安全的Singleton,注意双检查锁的正确实现。 11 | ## 4 代码举例 12 | - 总体思路:类的构造函数、拷贝构造函数、赋值构造函数)设为私有,包含一个私有的该类静态对象指针,提供公有的静态函数返回该指针。 13 | - Signleton.h 饿汉模式实现,类加载时完成实例的创建,线程安全。 14 | - Singleton1.h 懒汉模式实现,调用时单次检查,多线程不安全。 15 | - Signleton2.h 懒汉模式实现,调用时单次检查加锁,多线程安全,但是存在性能问题。 16 | - Singleton3.h 懒汉模式实现,调用时双检查锁,存在***内存读写reorder的情况***,同样不安全。 17 | - Singleton4.h 懒汉模式实现,调用时双检查锁,关闭内存reorder,多线程安全。 -------------------------------------------------------------------------------- /Singleton/Singleton.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Singleton/Singleton.h -------------------------------------------------------------------------------- /Singleton/Singleton.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 | 头文件 34 | 35 | 36 | 头文件 37 | 38 | 39 | -------------------------------------------------------------------------------- /Singleton/Singleton1.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Singleton/Singleton1.h -------------------------------------------------------------------------------- /Singleton/Singleton2.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Singleton/Singleton2.h -------------------------------------------------------------------------------- /Singleton/Singleton3.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Singleton/Singleton3.h -------------------------------------------------------------------------------- /Singleton/Singleton4.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Singleton/Singleton4.h -------------------------------------------------------------------------------- /Singleton/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | 3 | void main() 4 | { 5 | Singleton::Instance()->DoSomething(); 6 | Singleton::DestoryInstance(); 7 | } -------------------------------------------------------------------------------- /State/Person.cpp: -------------------------------------------------------------------------------- 1 | #include "Person.h" 2 | #include "State.h" 3 | 4 | void Person::OnKeyDown() 5 | { 6 | m_pState->Down(this); 7 | } 8 | 9 | void Person::OnKeyUp() 10 | { 11 | m_pState->Up(this); 12 | } 13 | 14 | void Person::MoveOn() 15 | { 16 | m_pState->Action(this); 17 | } 18 | 19 | void Person::SetState(State* pState) 20 | { 21 | m_pState = pState; 22 | } 23 | -------------------------------------------------------------------------------- /State/Person.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class State; 4 | 5 | class Person 6 | { 7 | public: 8 | Person(State* pState) : m_pState(pState) {} 9 | 10 | virtual void OnKeyDown(); 11 | virtual void OnKeyUp(); 12 | 13 | void MoveOn(); 14 | void SetState(State* pState); 15 | 16 | private: 17 | State* m_pState; 18 | }; 19 | 20 | -------------------------------------------------------------------------------- /State/README.md: -------------------------------------------------------------------------------- 1 | # State 2 | ## 1 动机 3 | 对象状态改变是行为也发生改变,如文档处于只读和读写状态是支持的行为不同,如何在运行时根据对象状态透明改变对象行为,而不为对象操作和状态转化之间引入紧耦合。 4 | ## 2 定义 5 | 允许一个对象在其内部状态改变是改变行为,使对象看起来似乎修改了行为 6 | ## 3 特点 7 | - State模式将所有与一个对象相关的行为都放入State子类,对象状态切换时,切换相应的State子类对象 8 | - 为不同的状态引入不同的对象,是状态转换更加明确,因为转换是原子性的,保证不会出现转换不一致的情况。 9 | - State变量没有实例变量时,可以使用单例模式,节省开销。 10 | ## 4 代码举例 11 | - 在游戏中,人Person可以站立、蹲着、趴下,三种状态行走方式不同,同时通过按键盘上下键,依次可以在三种装填间切换,站立按下切换为蹲着,蹲下按下切换为趴下,反之亦反。 12 | - Person包含状态对象指针,SetState方法,MoveOn行走方法,以及相应键盘上下键OnKeyDown、OnKeyUp方法。 13 | - State基类包含Up、Down、Action三个接口,均传入Person指针,派生站立状态StandState、蹲着状态SquatState、趴着状态LieProneState,各派生类各自处理状态切换和Action。 14 | - StandStae的Up方法不做任何处理(站立时不响应键盘上键,只相应下键),Down方法中将人的状态设置为SquatState,Action方法为人步行。 -------------------------------------------------------------------------------- /State/State.cpp: -------------------------------------------------------------------------------- 1 | #include "State.h" 2 | 3 | StandState* StandState::pStandState = new StandState(); 4 | SquatState* SquatState::pSquateState = new SquatState(); 5 | LieProneState* LieProneState::pLieProneState = new LieProneState(); 6 | 7 | StandState::AutoDeletor StandState::autoDeletor; 8 | SquatState::AutoDeletor SquatState::autoDeletor; 9 | LieProneState::AutoDeletor LieProneState::autoDeletor; 10 | 11 | 12 | void StandState::Down(Person* person) 13 | { 14 | person->SetState(SquatState::Instance()); 15 | } 16 | 17 | void SquatState::Down(Person* person) 18 | { 19 | person->SetState(LieProneState::Instance()); 20 | } 21 | 22 | void SquatState::Up(Person* person) 23 | { 24 | person->SetState(StandState::Instance()); 25 | } 26 | 27 | void LieProneState::Up(Person* person) 28 | { 29 | person->SetState(SquatState::Instance()); 30 | } 31 | -------------------------------------------------------------------------------- /State/State.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/State/State.h -------------------------------------------------------------------------------- /State/State.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 | {33A4EF66-2861-4C0A-A169-4852A7103EA5} 24 | State 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 | 151 | 152 | -------------------------------------------------------------------------------- /State/State.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 | 源文件 34 | 35 | 36 | -------------------------------------------------------------------------------- /State/main.cpp: -------------------------------------------------------------------------------- 1 | #include "State.h" 2 | #include "Person.h" 3 | 4 | void main() 5 | { 6 | Person* person = new Person(StandState::Instance()); 7 | person->MoveOn(); 8 | 9 | person->OnKeyDown(); 10 | person->MoveOn(); 11 | 12 | person->OnKeyDown(); 13 | person->MoveOn(); 14 | 15 | delete person; person = nullptr; 16 | } -------------------------------------------------------------------------------- /Strategy/README.md: -------------------------------------------------------------------------------- 1 | # Stragety 2 | ## 1 动机 3 | 对象使用的算法可能多样,将算法编码到对象可能导致对象复杂(过多的switch/if-else语句);同时支持不适用的算法也会带来性能负担(过多的switch/if-else语句可能导致大量的无效判断;同时不使用的代码太多,会挤占CPU高级缓存,降低性能)。 4 | ## 2 定义 5 | 定义一系列算法,把它们一个个封装起来,使它们可以互相替换(*变化*),从而可以是算法独立于客户程序(*稳定*)而变化。 6 | ## 3 特点 7 | - Stragety及其子类为组件提供了一系列可重用算法,可在运行时根据需要在算法之间切换 8 | - 含有许多条件判断的代码通常需要此模式 9 | - 如果Stragety对象没有多个实例变量,那么可以各个上下文共享一个对象(单例模式),从而节省对象开销。 10 | ## 4 代码举例 11 | - 不同客户类型打折力度不同,定义一个的折扣基类,提供折扣计算接口。 12 | - 折扣基类派生新客户折扣类、老客户折扣类、VIP客户折扣类,各自实现折扣计算方法。 13 | - 订单类传入不同的折扣类,就通过订单额乘以折扣进行最终金额计算。 -------------------------------------------------------------------------------- /Strategy/Sales.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | class SalesDiscount 4 | { 5 | public: 6 | virtual ~SalesDiscount() {} 7 | virtual float Discount() = 0; 8 | }; 9 | 10 | class NewConsumerDiscount : public SalesDiscount 11 | { 12 | public: 13 | virtual float Discount() 14 | { 15 | return 1.0; 16 | } 17 | }; 18 | 19 | class OldConsumerDiscount : public SalesDiscount 20 | { 21 | public: 22 | virtual float Discount() 23 | { 24 | return 0.95; 25 | } 26 | }; 27 | 28 | class VIPDiscount : public SalesDiscount 29 | { 30 | public: 31 | virtual float Discount() 32 | { 33 | return 0.85; 34 | } 35 | }; 36 | 37 | 38 | class SalesOrder 39 | { 40 | public: 41 | void SetDiscount(SalesDiscount* pDiscount) 42 | { 43 | m_pDiscount = pDiscount; 44 | } 45 | float Calculate(float fltAmount) 46 | { 47 | return fltAmount * m_pDiscount->Discount(); 48 | } 49 | private: 50 | SalesDiscount* m_pDiscount; 51 | }; 52 | 53 | -------------------------------------------------------------------------------- /Strategy/Strategy.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 | {477B8F0B-5828-44C7-B124-B4866B393469} 24 | Strategy 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 | -------------------------------------------------------------------------------- /Strategy/Strategy.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 | -------------------------------------------------------------------------------- /Strategy/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Sales.h" 2 | #include 3 | 4 | void main() 5 | { 6 | SalesDiscount* pDiscount = new VIPDiscount(); 7 | 8 | SalesOrder order; 9 | order.SetDiscount(pDiscount); 10 | 11 | std::cout << order.Calculate(200.0) << std::endl; 12 | 13 | delete pDiscount; pDiscount = nullptr; 14 | } -------------------------------------------------------------------------------- /Template/README.md: -------------------------------------------------------------------------------- 1 | # Template 2 | ## 1 动机 3 | 软件设计中,操作的骨架稳定而子步骤有改变需求或者子步骤算法无法与整体骨架同时实现。 4 | ## 2 定义 5 | 定义一个算法结构,将其中一些步骤延迟(虚函数)到子类中。 6 | ## 3 特点 7 | - 十分基础的模式,运用广泛。 8 | - 通过晚绑定(运行时绑定)实现“不要调用我,我来调用你”的反向控制 9 | - Template 调用的虚方法可以不实现,建议设为protected,因为单独方法在整理流程中才有意义,单独存在意义不大。 10 | ## 4 代码举例 11 | - 定义一个煮米的基类,实现算法骨架“加米-加水-开电饭煲-设置电饭煲-等待食物”的算法骨架 12 | - 从煮米基类派生一个米饭基类,重写“加水”函数,米与水1:2;重写“设置电饭煲”,将电饭煲设置为米饭模式。 13 | - 从煮米基类派生一个煮粥基类,重写“加水”函数,米与水1:5;重写“设置电饭煲”,将电饭煲设置为粥汤模式 -------------------------------------------------------------------------------- /Template/Template.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhiminXu/DesignPatterns/6da2e1ab60b34c164e9cb58d7c5b95cafbbcae0e/Template/Template.h -------------------------------------------------------------------------------- /Template/Template.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 | {BE2F4126-DB50-4092-9A87-F2E744FA8C11} 24 | Template 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 | -------------------------------------------------------------------------------- /Template/Template.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 | -------------------------------------------------------------------------------- /Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Template.h" 2 | 3 | void main() 4 | { 5 | TemplateCookedRice* pTemplate = new Porridge(50); 6 | pTemplate->Cooking(); 7 | delete pTemplate; pTemplate = nullptr; 8 | } -------------------------------------------------------------------------------- /Visitor/Element.cpp: -------------------------------------------------------------------------------- 1 | #include "Element.h" 2 | #include "Visitor.h" 3 | 4 | void ElementA::Accept(Visitor* pVisitor) 5 | { 6 | pVisitor->VisitElementA(this); 7 | } 8 | 9 | void ElementB::Accept(Visitor* pVisitor) 10 | { 11 | pVisitor->VisitElementB(this); 12 | } 13 | -------------------------------------------------------------------------------- /Visitor/Element.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | 4 | class Visitor; 5 | class Element 6 | { 7 | public: 8 | virtual void Accept(Visitor* pVisitor) = 0; 9 | virtual ~Element() {} 10 | }; 11 | 12 | class ElementA : public Element 13 | { 14 | public: 15 | virtual void Accept(Visitor* pVisitor); 16 | 17 | void OperationA() 18 | { 19 | std::cout << "ElementA Operation" << std::endl; 20 | } 21 | }; 22 | 23 | class ElementB : public Element 24 | { 25 | public: 26 | virtual void Accept(Visitor* pVisitor); 27 | void OperationB() 28 | { 29 | std::cout << "ElementB Operation" << std::endl; 30 | } 31 | }; -------------------------------------------------------------------------------- /Visitor/README.md: -------------------------------------------------------------------------------- 1 | # Visitor 2 | ## 1 动机 3 | 由于需求的变化,在类层级结构中需要增加新的行为,如果直接在基类中修改,将带来沉重的变更负担,甚至破坏原有设计。不改变类层次结构的前提下,运行时根据需要透明地为类层次结构动态的添加新操作。 4 | ## 2 定义 5 | 定义一个作用于某对象结构的各个元素的操作,使得可以在不改变各元素的类的前提下,扩展作用于这些元素的新操作。 6 | ## 3 特点 7 | - 通过双重分发来实现不改变Element类层次结构的前提下,在运行时动态添加新操作。 8 | - 双重分发包含两个多态分发,一个为Accept多态,一个为VisitElement多态 9 | - Visitor缺点在于廓镇Element类层次结构会导致Visitor类改变,因此Visitor适用于Element类层次结构稳定而操作经常改动的场景。 10 | - Visitor的应用要求在类设计阶段具有一定的预见性。 11 | ## 4 代码举例 12 | - Element基类包含接口Accept接口,传入Visitor指针;派生ElementA与ElementB两个子类 13 | - Visitor基类包含visitElementA、VisitElmentB接口,分别传入ElementA与ElementB指针 14 | - ElementA实现Accept接口,内部调用visitor的VisitiElementA方法,传入this指针,包含OperationA方法;ELementB类似。 15 | - Visitor基类派生ConcreteVisitor子类,实现visitElementA,调用ElementA的OperationA方法,并增加新的功能;ELementB类似。 -------------------------------------------------------------------------------- /Visitor/Visitor.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include "Element.h" 3 | 4 | class Visitor 5 | { 6 | public: 7 | virtual void VisitElementA(ElementA* pElementA) = 0; 8 | virtual void VisitElementB(ElementB* pElementB) = 0; 9 | virtual ~Visitor() {} 10 | }; 11 | 12 | 13 | class ConcreteVisitor : public Visitor 14 | { 15 | public: 16 | virtual void VisitElementA(ElementA* pElementA) 17 | { 18 | std::cout << "Added operation for ElementA" << std::endl; 19 | pElementA->OperationA(); 20 | } 21 | virtual void VisitElementB(ElementB* pElementB) 22 | { 23 | std::cout << "Added operation for ElementB" << std::endl; 24 | pElementB->OperationB(); 25 | } 26 | }; 27 | 28 | -------------------------------------------------------------------------------- /Visitor/Visitor.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 | {EFCB5891-2574-4D86-94F5-10BF4D6040F1} 24 | Visitor 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 | 151 | -------------------------------------------------------------------------------- /Visitor/Visitor.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 | -------------------------------------------------------------------------------- /Visitor/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Visitor.h" 2 | #include "Element.h" 3 | 4 | void main() 5 | { 6 | ElementA elementA; 7 | ConcreteVisitor visitor; 8 | elementA.Accept(&visitor); 9 | 10 | std::cin.get(); 11 | } --------------------------------------------------------------------------------