├── .gitattributes ├── .gitignore ├── LICENSE ├── Makefile ├── _static ├── AbatractFactory.jpg ├── AbatractFactory_run.jpg ├── Adapter.jpg ├── Adapter_classModel.jpg ├── Adapter_run.jpg ├── Bridge.jpg ├── Bridge_eg.jpg ├── Bridge_run.jpg ├── Builder.jpg ├── Builder_run.jpg ├── Command.jpg ├── Command_eg.jpg ├── Command_run.jpg ├── Decorator.jpg ├── Decorator_eg.jpg ├── Decorator_run.jpg ├── Facade.jpg ├── Facade_run.jpg ├── FactoryMethod.jpg ├── Flyweight.jpg ├── Flyweight_run.jpg ├── KFCBuilder.jpg ├── Mediator.jpg ├── Mediator_eg.jpg ├── Mediator_run.jpg ├── Obeserver.jpg ├── Obeserver_run.jpg ├── Proactor.jpg ├── Proxy.jpg ├── Proxy_run.jpg ├── Reactor.jpg ├── SimpleFactory.jpg ├── Singleton.jpg ├── Singleton_run.jpg ├── State.jpg ├── State_eg.jpg ├── State_run.jpg ├── Strategy.jpg ├── Strategy_run.jpg ├── SystemFactory.jpg ├── dp_relation.jpg ├── loger.jpg ├── seq_AbatractFactory.jpg ├── seq_Adapter.jpg ├── seq_Bridge.jpg ├── seq_Builder.jpg ├── seq_Command.jpg ├── seq_Command_eg.jpg ├── seq_Decorator.jpg ├── seq_Decorator_eg.jpg ├── seq_Facade.jpg ├── seq_FactoryMethod.jpg ├── seq_Flyweight.jpg ├── seq_Mediator.jpg ├── seq_Mediator_eg.jpg ├── seq_Obeserver.jpg ├── seq_Proactor.jpg ├── seq_Proxy.jpg ├── seq_Reactor.jpg ├── seq_SimpleFactory.jpg ├── seq_Singleton.jpg ├── seq_State.jpg ├── seq_State_eg.jpg ├── seq_Strategy.jpg ├── seq_loger.jpg ├── state1.png ├── state2.png ├── uml_AbatractClass.jpg ├── uml_aggregation.jpg ├── uml_association.jpg ├── uml_class_struct.jpg ├── uml_composition.jpg ├── uml_dependency.jpg ├── uml_generalization.jpg ├── uml_generalize.jpg └── uml_realize.jpg ├── behavioral_patterns ├── behavioral.rst ├── command.rst ├── mediator.rst ├── observer.rst ├── state.rst └── strategy.rst ├── code ├── AbstractFactory │ ├── AbstractFactory.cpp │ ├── AbstractFactory.h │ ├── AbstractProductA.cpp │ ├── AbstractProductA.h │ ├── AbstractProductB.cpp │ ├── AbstractProductB.h │ ├── ConcreteFactory1.cpp │ ├── ConcreteFactory1.h │ ├── ConcreteFactory2.cpp │ ├── ConcreteFactory2.h │ ├── ProductA1.cpp │ ├── ProductA1.h │ ├── ProductA2.cpp │ ├── ProductA2.h │ ├── ProductB1.cpp │ ├── ProductB1.h │ ├── ProductB2.cpp │ ├── ProductB2.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── AbstractFactory.o │ │ ├── AbstractProductA.o │ │ ├── AbstractProductB.o │ │ ├── ConcreteFactory1.o │ │ ├── ConcreteFactory2.o │ │ ├── ProductA1.o │ │ ├── ProductA2.o │ │ ├── ProductB1.o │ │ ├── ProductB2.o │ │ ├── main.exe │ │ └── main.o ├── Adapter │ ├── Adaptee.cpp │ ├── Adaptee.h │ ├── Adapter.cpp │ ├── Adapter.h │ ├── Client.cpp │ ├── Client.h │ ├── Target.cpp │ ├── Target.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Adaptee.o │ │ ├── Adapter.o │ │ ├── Target.o │ │ ├── main.exe │ │ └── main.o ├── Bridge │ ├── Abstraction.cpp │ ├── Abstraction.h │ ├── ConcreteImplementorA.cpp │ ├── ConcreteImplementorA.h │ ├── ConcreteImplementorB.cpp │ ├── ConcreteImplementorB.h │ ├── Implementor.cpp │ ├── Implementor.h │ ├── RefinedAbstraction.cpp │ ├── RefinedAbstraction.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Abstraction.o │ │ ├── ConcreteImplementorA.o │ │ ├── ConcreteImplementorB.o │ │ ├── Implementor.o │ │ ├── RefinedAbstraction.o │ │ ├── main.exe │ │ └── main.o ├── Builder │ ├── Builder.cpp │ ├── Builder.h │ ├── ConcreteBuilder.cpp │ ├── ConcreteBuilder.h │ ├── Director.cpp │ ├── Director.h │ ├── Product.cpp │ ├── Product.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Builder.o │ │ ├── ConcreteBuilder.o │ │ ├── Director.o │ │ ├── Product.o │ │ ├── main.exe │ │ └── main.o ├── Command │ ├── Command.cpp │ ├── Command.h │ ├── ConcreteCommand.cpp │ ├── ConcreteCommand.h │ ├── Invoker.cpp │ ├── Invoker.h │ ├── Receiver.cpp │ ├── Receiver.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Command.o │ │ ├── ConcreteCommand.o │ │ ├── Invoker.o │ │ ├── Receiver.o │ │ ├── main.exe │ │ └── main.o ├── Decorator │ ├── Component.cpp │ ├── Component.h │ ├── ConcreteComponent.cpp │ ├── ConcreteComponent.h │ ├── ConcreteDecoratorA.cpp │ ├── ConcreteDecoratorA.h │ ├── ConcreteDecoratorB.cpp │ ├── ConcreteDecoratorB.h │ ├── Decorator.cpp │ ├── Decorator.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Component.o │ │ ├── ConcreteComponent.o │ │ ├── ConcreteDecoratorA.o │ │ ├── ConcreteDecoratorB.o │ │ ├── Decorator.o │ │ ├── main.exe │ │ └── main.o ├── Facade │ ├── Client.cpp │ ├── Client.h │ ├── Facade.cpp │ ├── Facade.h │ ├── SystemA.cpp │ ├── SystemA.h │ ├── SystemB.cpp │ ├── SystemB.h │ ├── SystemC.cpp │ ├── SystemC.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Facade.o │ │ ├── SystemA.o │ │ ├── SystemB.o │ │ ├── SystemC.o │ │ ├── main.exe │ │ └── main.o ├── FactoryMethod │ ├── Client.cpp │ ├── Client.h │ ├── ConcreteFactory.cpp │ ├── ConcreteFactory.h │ ├── ConcreteProduct.cpp │ ├── ConcreteProduct.h │ ├── Factory.cpp │ ├── Factory.h │ ├── Product.cpp │ ├── Product.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Client.o │ │ ├── ConcreteFactory.o │ │ ├── ConcreteProduct.o │ │ ├── Factory.o │ │ ├── Product.o │ │ ├── main.exe │ │ └── main.o ├── Flyweight │ ├── ConcreteFlyweight.cpp │ ├── ConcreteFlyweight.h │ ├── Flyweight.cpp │ ├── Flyweight.h │ ├── FlyweightFactory.cpp │ ├── FlyweightFactory.h │ ├── UnsharedConcreteFlyweight.cpp │ ├── UnsharedConcreteFlyweight.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── ConcreteFlyweight.o │ │ ├── Flyweight.o │ │ ├── FlyweightFactory.o │ │ ├── main.exe │ │ └── main.o ├── Mediator │ ├── Colleague.cpp │ ├── Colleague.h │ ├── ConcreteColleagueA.cpp │ ├── ConcreteColleagueA.h │ ├── ConcreteColleagueB.cpp │ ├── ConcreteColleagueB.h │ ├── ConcreteMediator.cpp │ ├── ConcreteMediator.h │ ├── Mediator.cpp │ ├── Mediator.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Colleague.o │ │ ├── ConcreteColleagueA.o │ │ ├── ConcreteColleagueB.o │ │ ├── ConcreteMediator.o │ │ ├── Mediator.o │ │ ├── main.exe │ │ └── main.o ├── Obeserver │ ├── ConcreteObeserver.cpp │ ├── ConcreteObeserver.h │ ├── ConcreteSubject.cpp │ ├── ConcreteSubject.h │ ├── Obeserver.cpp │ ├── Obeserver.h │ ├── Subject.cpp │ ├── Subject.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── ConcreteObeserver.o │ │ ├── ConcreteSubject.o │ │ ├── Obeserver.o │ │ ├── Subject.o │ │ ├── main.exe │ │ └── main.o ├── Proxy │ ├── Proxy.cpp │ ├── Proxy.h │ ├── RealSubject.cpp │ ├── RealSubject.h │ ├── Subject.cpp │ ├── Subject.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Proxy.o │ │ ├── RealSubject.o │ │ ├── Subject.o │ │ ├── main.exe │ │ └── main.o ├── SimpleFactory │ ├── ConcreteProductA.cpp │ ├── ConcreteProductA.h │ ├── ConcreteProductB.cpp │ ├── ConcreteProductB.h │ ├── Factory.cpp │ ├── Factory.h │ ├── Product.cpp │ ├── Product.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── ConcreteProductA.o │ │ ├── ConcreteProductB.o │ │ ├── Factory.o │ │ ├── Product.o │ │ ├── main.exe │ │ └── main.o ├── Singleton │ ├── Singleton.cpp │ ├── Singleton.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── Singleton.o │ │ ├── main.exe │ │ └── main.o ├── State │ ├── ConcreteStateA.cpp │ ├── ConcreteStateA.h │ ├── ConcreteStateB.cpp │ ├── ConcreteStateB.h │ ├── Context.cpp │ ├── Context.h │ ├── State.cpp │ ├── State.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ └── mingw5 │ │ ├── ConcreteStateA.o │ │ ├── ConcreteStateB.o │ │ ├── Context.o │ │ ├── State.o │ │ ├── main.exe │ │ └── main.o ├── Strategy │ ├── ConcreteStrategyA.cpp │ ├── ConcreteStrategyA.h │ ├── ConcreteStrategyB.cpp │ ├── ConcreteStrategyB.h │ ├── Context.cpp │ ├── Context.h │ ├── Strategy.cpp │ ├── Strategy.h │ ├── main.cfp │ ├── main.cfpg │ ├── main.cpp │ ├── main.o │ └── mingw5 │ │ ├── ConcreteStrategyA.o │ │ ├── ConcreteStrategyB.o │ │ ├── Context.o │ │ ├── Strategy.o │ │ ├── main.exe │ │ └── main.o └── main │ ├── main.cfp │ ├── main.cfpg │ └── main.cpp ├── conf.py ├── creational_patterns ├── abstract_factory.rst ├── builder.rst ├── creational.rst ├── factory_method.rst ├── simple_factory.rst └── singleton.rst ├── design_patterns.EAP ├── dp_tmp.txt ├── index.rst ├── make.bat ├── read_uml.rst ├── readme.rst ├── sphinx_rtd_theme ├── __init__.py ├── breadcrumbs.html ├── footer.html ├── layout.html ├── layout_old.html ├── search.html ├── searchbox.html ├── static │ ├── css │ │ ├── badge_only.css │ │ ├── font_lato_googleapi.css │ │ └── theme.css │ ├── fonts │ │ ├── FontAwesome.otf │ │ ├── fontawesome-webfont.eot │ │ ├── fontawesome-webfont.svg │ │ ├── fontawesome-webfont.ttf │ │ └── fontawesome-webfont.woff │ └── js │ │ ├── modernizr.min.js │ │ └── theme.js ├── theme.conf └── versions.html ├── state_diagram.vsd └── structural_patterns ├── adapter.rst ├── bridge.rst ├── decorator.rst ├── facade.rst ├── flyweight.rst ├── proxy.rst └── structural.rst /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | _build/ 6 | 7 | *.pydevproject 8 | .project 9 | .metadata 10 | bin/ 11 | tmp/ 12 | *.tmp 13 | *.bak 14 | *.swp 15 | *~.nib 16 | local.properties 17 | .classpath 18 | .settings/ 19 | .loadpath 20 | 21 | # External tool builders 22 | .externalToolBuilders/ 23 | 24 | # Locally stored "Eclipse launch configurations" 25 | *.launch 26 | 27 | # CDT-specific 28 | .cproject 29 | 30 | # PDT-specific 31 | .buildpath 32 | 33 | 34 | ################# 35 | ## Visual Studio 36 | ################# 37 | 38 | ## Ignore Visual Studio temporary files, build results, and 39 | ## files generated by popular Visual Studio add-ons. 40 | 41 | # User-specific files 42 | *.suo 43 | *.user 44 | *.sln.docstates 45 | 46 | # Build results 47 | 48 | [Dd]ebug/ 49 | [Rr]elease/ 50 | x64/ 51 | build/ 52 | [Bb]in/ 53 | [Oo]bj/ 54 | 55 | # MSTest test Results 56 | [Tt]est[Rr]esult*/ 57 | [Bb]uild[Ll]og.* 58 | 59 | *_i.c 60 | *_p.c 61 | *.ilk 62 | *.meta 63 | *.obj 64 | *.pch 65 | *.pdb 66 | *.pgc 67 | *.pgd 68 | *.rsp 69 | *.sbr 70 | *.tlb 71 | *.tli 72 | *.tlh 73 | *.tmp 74 | *.tmp_proj 75 | *.log 76 | *.vspscc 77 | *.vssscc 78 | .builds 79 | *.pidb 80 | *.log 81 | *.scc 82 | 83 | # Visual C++ cache files 84 | ipch/ 85 | *.aps 86 | *.ncb 87 | *.opensdf 88 | *.sdf 89 | *.cachefile 90 | 91 | # Visual Studio profiler 92 | *.psess 93 | *.vsp 94 | *.vspx 95 | 96 | # Guidance Automation Toolkit 97 | *.gpState 98 | 99 | # ReSharper is a .NET coding add-in 100 | _ReSharper*/ 101 | *.[Rr]e[Ss]harper 102 | 103 | # TeamCity is a build add-in 104 | _TeamCity* 105 | 106 | # DotCover is a Code Coverage Tool 107 | *.dotCover 108 | 109 | # NCrunch 110 | *.ncrunch* 111 | .*crunch*.local.xml 112 | 113 | # Installshield output folder 114 | [Ee]xpress/ 115 | 116 | # DocProject is a documentation generator add-in 117 | DocProject/buildhelp/ 118 | DocProject/Help/*.HxT 119 | DocProject/Help/*.HxC 120 | DocProject/Help/*.hhc 121 | DocProject/Help/*.hhk 122 | DocProject/Help/*.hhp 123 | DocProject/Help/Html2 124 | DocProject/Help/html 125 | 126 | # Click-Once directory 127 | publish/ 128 | 129 | # Publish Web Output 130 | *.Publish.xml 131 | *.pubxml 132 | 133 | # NuGet Packages Directory 134 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 135 | #packages/ 136 | 137 | # Windows Azure Build Output 138 | csx 139 | *.build.csdef 140 | 141 | # Windows Store app package directory 142 | AppPackages/ 143 | 144 | # Others 145 | sql/ 146 | *.Cache 147 | ClientBin/ 148 | [Ss]tyle[Cc]op.* 149 | ~$* 150 | *~ 151 | *.dbmdl 152 | *.[Pp]ublish.xml 153 | *.pfx 154 | *.publishsettings 155 | 156 | # RIA/Silverlight projects 157 | Generated_Code/ 158 | 159 | # Backup & report files from converting an old project file to a newer 160 | # Visual Studio version. Backup files are not needed, because we have git ;-) 161 | _UpgradeReport_Files/ 162 | Backup*/ 163 | UpgradeLog*.XML 164 | UpgradeLog*.htm 165 | 166 | # SQL Server files 167 | App_Data/*.mdf 168 | App_Data/*.ldf 169 | 170 | ############# 171 | ## Windows detritus 172 | ############# 173 | 174 | # Windows image file caches 175 | Thumbs.db 176 | ehthumbs.db 177 | 178 | # Folder config file 179 | Desktop.ini 180 | 181 | # Recycle Bin used on file shares 182 | $RECYCLE.BIN/ 183 | 184 | # Mac crap 185 | .DS_Store 186 | 187 | 188 | ############# 189 | ## Python 190 | ############# 191 | 192 | *.py[co] 193 | 194 | # Packages 195 | *.egg 196 | *.egg-info 197 | dist/ 198 | build/ 199 | eggs/ 200 | parts/ 201 | var/ 202 | sdist/ 203 | develop-eggs/ 204 | .installed.cfg 205 | 206 | # Installer logs 207 | pip-log.txt 208 | 209 | # Unit test / coverage reports 210 | .coverage 211 | .tox 212 | 213 | #Translations 214 | *.mo 215 | 216 | #Mr Developer 217 | .mr.developer.cfg 218 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 版权说明 2 | ===================== 3 | 4 | 5 | 本书所有源代码及图片可自由分享,包括用于商业用途; 6 | 7 | 由于部分章节内容使用到刘伟的课件《java设计模式》(互联网分享),所以本书 8 | 文字内容不可用于商业目的(可在互联网自由分享); 9 | 10 | 本书内容转载请注明出处; 11 | 12 | 参考: 13 | -------------------- 14 | 《Java设计模式》PPT , 刘伟 15 | -------------------------------------------------------------------------------- /_static/AbatractFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/AbatractFactory.jpg -------------------------------------------------------------------------------- /_static/AbatractFactory_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/AbatractFactory_run.jpg -------------------------------------------------------------------------------- /_static/Adapter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Adapter.jpg -------------------------------------------------------------------------------- /_static/Adapter_classModel.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Adapter_classModel.jpg -------------------------------------------------------------------------------- /_static/Adapter_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Adapter_run.jpg -------------------------------------------------------------------------------- /_static/Bridge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Bridge.jpg -------------------------------------------------------------------------------- /_static/Bridge_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Bridge_eg.jpg -------------------------------------------------------------------------------- /_static/Bridge_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Bridge_run.jpg -------------------------------------------------------------------------------- /_static/Builder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Builder.jpg -------------------------------------------------------------------------------- /_static/Builder_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Builder_run.jpg -------------------------------------------------------------------------------- /_static/Command.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Command.jpg -------------------------------------------------------------------------------- /_static/Command_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Command_eg.jpg -------------------------------------------------------------------------------- /_static/Command_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Command_run.jpg -------------------------------------------------------------------------------- /_static/Decorator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Decorator.jpg -------------------------------------------------------------------------------- /_static/Decorator_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Decorator_eg.jpg -------------------------------------------------------------------------------- /_static/Decorator_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Decorator_run.jpg -------------------------------------------------------------------------------- /_static/Facade.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Facade.jpg -------------------------------------------------------------------------------- /_static/Facade_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Facade_run.jpg -------------------------------------------------------------------------------- /_static/FactoryMethod.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/FactoryMethod.jpg -------------------------------------------------------------------------------- /_static/Flyweight.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Flyweight.jpg -------------------------------------------------------------------------------- /_static/Flyweight_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Flyweight_run.jpg -------------------------------------------------------------------------------- /_static/KFCBuilder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/KFCBuilder.jpg -------------------------------------------------------------------------------- /_static/Mediator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Mediator.jpg -------------------------------------------------------------------------------- /_static/Mediator_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Mediator_eg.jpg -------------------------------------------------------------------------------- /_static/Mediator_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Mediator_run.jpg -------------------------------------------------------------------------------- /_static/Obeserver.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Obeserver.jpg -------------------------------------------------------------------------------- /_static/Obeserver_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Obeserver_run.jpg -------------------------------------------------------------------------------- /_static/Proactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Proactor.jpg -------------------------------------------------------------------------------- /_static/Proxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Proxy.jpg -------------------------------------------------------------------------------- /_static/Proxy_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Proxy_run.jpg -------------------------------------------------------------------------------- /_static/Reactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Reactor.jpg -------------------------------------------------------------------------------- /_static/SimpleFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/SimpleFactory.jpg -------------------------------------------------------------------------------- /_static/Singleton.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Singleton.jpg -------------------------------------------------------------------------------- /_static/Singleton_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Singleton_run.jpg -------------------------------------------------------------------------------- /_static/State.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/State.jpg -------------------------------------------------------------------------------- /_static/State_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/State_eg.jpg -------------------------------------------------------------------------------- /_static/State_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/State_run.jpg -------------------------------------------------------------------------------- /_static/Strategy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Strategy.jpg -------------------------------------------------------------------------------- /_static/Strategy_run.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/Strategy_run.jpg -------------------------------------------------------------------------------- /_static/SystemFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/SystemFactory.jpg -------------------------------------------------------------------------------- /_static/dp_relation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/dp_relation.jpg -------------------------------------------------------------------------------- /_static/loger.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/loger.jpg -------------------------------------------------------------------------------- /_static/seq_AbatractFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_AbatractFactory.jpg -------------------------------------------------------------------------------- /_static/seq_Adapter.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Adapter.jpg -------------------------------------------------------------------------------- /_static/seq_Bridge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Bridge.jpg -------------------------------------------------------------------------------- /_static/seq_Builder.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Builder.jpg -------------------------------------------------------------------------------- /_static/seq_Command.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Command.jpg -------------------------------------------------------------------------------- /_static/seq_Command_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Command_eg.jpg -------------------------------------------------------------------------------- /_static/seq_Decorator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Decorator.jpg -------------------------------------------------------------------------------- /_static/seq_Decorator_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Decorator_eg.jpg -------------------------------------------------------------------------------- /_static/seq_Facade.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Facade.jpg -------------------------------------------------------------------------------- /_static/seq_FactoryMethod.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_FactoryMethod.jpg -------------------------------------------------------------------------------- /_static/seq_Flyweight.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Flyweight.jpg -------------------------------------------------------------------------------- /_static/seq_Mediator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Mediator.jpg -------------------------------------------------------------------------------- /_static/seq_Mediator_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Mediator_eg.jpg -------------------------------------------------------------------------------- /_static/seq_Obeserver.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Obeserver.jpg -------------------------------------------------------------------------------- /_static/seq_Proactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Proactor.jpg -------------------------------------------------------------------------------- /_static/seq_Proxy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Proxy.jpg -------------------------------------------------------------------------------- /_static/seq_Reactor.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Reactor.jpg -------------------------------------------------------------------------------- /_static/seq_SimpleFactory.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_SimpleFactory.jpg -------------------------------------------------------------------------------- /_static/seq_Singleton.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Singleton.jpg -------------------------------------------------------------------------------- /_static/seq_State.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_State.jpg -------------------------------------------------------------------------------- /_static/seq_State_eg.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_State_eg.jpg -------------------------------------------------------------------------------- /_static/seq_Strategy.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_Strategy.jpg -------------------------------------------------------------------------------- /_static/seq_loger.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/seq_loger.jpg -------------------------------------------------------------------------------- /_static/state1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/state1.png -------------------------------------------------------------------------------- /_static/state2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/state2.png -------------------------------------------------------------------------------- /_static/uml_AbatractClass.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_AbatractClass.jpg -------------------------------------------------------------------------------- /_static/uml_aggregation.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_aggregation.jpg -------------------------------------------------------------------------------- /_static/uml_association.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_association.jpg -------------------------------------------------------------------------------- /_static/uml_class_struct.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_class_struct.jpg -------------------------------------------------------------------------------- /_static/uml_composition.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_composition.jpg -------------------------------------------------------------------------------- /_static/uml_dependency.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_dependency.jpg -------------------------------------------------------------------------------- /_static/uml_generalization.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_generalization.jpg -------------------------------------------------------------------------------- /_static/uml_generalize.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_generalize.jpg -------------------------------------------------------------------------------- /_static/uml_realize.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/_static/uml_realize.jpg -------------------------------------------------------------------------------- /behavioral_patterns/behavioral.rst: -------------------------------------------------------------------------------- 1 | .. _behavioral: 2 | 3 | 行为型模式 4 | ==================== 5 | 6 | 7 | 行为型模式(Behavioral Pattern)是对在不同的对象之间划分责任和算法的抽象化。 8 | 9 | 行为型模式不仅仅关注类和对象的结构,而且重点关注它们之间的相互作用。 10 | 11 | 通过行为型模式,可以更加清晰地划分类与对象的职责,并研究系统在运行时实例对象 12 | 之间的交互。在系统运行时,对象并不是孤立的,它们可以通过相互通信与协作完成某些复杂功能,一个对象在运行时也将影响到其他对象的运行。 13 | 14 | 行为型模式分为类行为型模式和对象行为型模式两种: 15 | 16 | - 类行为型模式:类的行为型模式使用继承关系在几个类之间分配行为,类行为型模式主要通过多态等方式来分配父类与子类的职责。 17 | - 对象行为型模式:对象的行为型模式则使用对象的聚合关联关系来分配行为,对象行为型模式主要是通过对象关联等方式来分配两个或多个类的职责。根据“合成复用原则”,系统中要尽量使用关联关系来取代继承关系,因此大部分行为型设计模式都属于对象行为型设计模式。 18 | 19 | **包含模式** 20 | 21 | - 职责链模式(Chain of Responsibility) 22 | 重要程度:3 23 | - 命令模式(Command) 24 | 重要程度:4 25 | - 解释器模式(Interpreter) 26 | 重要程度:1 27 | - 迭代器模式(Iterator) 28 | 重要程度:5 29 | - 中介者模式(Mediator) 30 | 重要程度:2 31 | - 备忘录模式(Memento) 32 | 重要程度:2 33 | - 观察者模式(Observer) 34 | 重要程度:5 35 | - 状态模式(State) 36 | 重要程度:3 37 | - 策略模式(Strategy) 38 | 重要程度:4 39 | - 模板方法模式(Template Method) 40 | 重要程度:3 41 | - 访问者模式(Visitor) 42 | 重要程度:1 43 | 44 | **目录** 45 | 46 | .. toctree:: 47 | :maxdepth: 2 48 | :numbered: 2 49 | 50 | command 51 | mediator 52 | observer 53 | state 54 | strategy 55 | 56 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractFactory.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractFactory.cpp 3 | // Implementation of the Class AbstractFactory 4 | // Created on: 02-十月-2014 15:04:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "AbstractFactory.h" 9 | 10 | 11 | AbstractFactory::AbstractFactory(){ 12 | 13 | } 14 | 15 | 16 | 17 | AbstractFactory::~AbstractFactory(){ 18 | 19 | } 20 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractFactory.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractFactory.h 3 | // Implementation of the Class AbstractFactory 4 | // Created on: 02-十月-2014 15:04:09 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_B48A03BA_2A60_4d85_B9EF_57DBDEEFE360__INCLUDED_) 9 | #define EA_B48A03BA_2A60_4d85_B9EF_57DBDEEFE360__INCLUDED_ 10 | #include "AbstractProductA.h" 11 | #include "AbstractProductB.h" 12 | 13 | class AbstractFactory 14 | { 15 | 16 | public: 17 | AbstractFactory(); 18 | virtual ~AbstractFactory(); 19 | 20 | virtual AbstractProductA * createProductA() = 0; 21 | virtual AbstractProductB * createProductB() = 0; 22 | 23 | }; 24 | #endif // !defined(EA_B48A03BA_2A60_4d85_B9EF_57DBDEEFE360__INCLUDED_) 25 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractProductA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractProductA.cpp 3 | // Implementation of the Class AbstractProductA 4 | // Created on: 02-十月-2014 15:04:14 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "AbstractProductA.h" 9 | 10 | 11 | AbstractProductA::AbstractProductA(){ 12 | 13 | } 14 | 15 | 16 | 17 | AbstractProductA::~AbstractProductA(){ 18 | 19 | } 20 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractProductA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractProductA.h 3 | // Implementation of the Class AbstractProductA 4 | // Created on: 02-十月-2014 15:04:14 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_A8C81D97_DDD6_4ee6_8D9A_35E6305ED449__INCLUDED_) 9 | #define EA_A8C81D97_DDD6_4ee6_8D9A_35E6305ED449__INCLUDED_ 10 | 11 | class AbstractProductA 12 | { 13 | 14 | public: 15 | AbstractProductA(); 16 | virtual ~AbstractProductA(); 17 | 18 | virtual void use() = 0; 19 | 20 | }; 21 | #endif // !defined(EA_A8C81D97_DDD6_4ee6_8D9A_35E6305ED449__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractProductB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractProductB.cpp 3 | // Implementation of the Class AbstractProductB 4 | // Created on: 02-十月-2014 15:04:15 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "AbstractProductB.h" 9 | 10 | 11 | AbstractProductB::AbstractProductB(){ 12 | 13 | } 14 | 15 | 16 | 17 | AbstractProductB::~AbstractProductB(){ 18 | 19 | } 20 | -------------------------------------------------------------------------------- /code/AbstractFactory/AbstractProductB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // AbstractProductB.h 3 | // Implementation of the Class AbstractProductB 4 | // Created on: 02-十月-2014 15:04:15 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_20F4F7CF_3782_441b_BB79_CC25B8481002__INCLUDED_) 9 | #define EA_20F4F7CF_3782_441b_BB79_CC25B8481002__INCLUDED_ 10 | 11 | class AbstractProductB 12 | { 13 | 14 | public: 15 | AbstractProductB(); 16 | virtual ~AbstractProductB(); 17 | 18 | virtual void eat() = 0; 19 | 20 | }; 21 | #endif // !defined(EA_20F4F7CF_3782_441b_BB79_CC25B8481002__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/AbstractFactory/ConcreteFactory1.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory1.cpp 3 | // Implementation of the Class ConcreteFactory1 4 | // Created on: 02-十月-2014 15:04:11 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteFactory1.h" 9 | #include "ProductA1.h" 10 | #include "ProductB1.h" 11 | 12 | 13 | ConcreteFactory1::ConcreteFactory1(){ 14 | 15 | } 16 | 17 | ConcreteFactory1::~ConcreteFactory1(){ 18 | 19 | } 20 | 21 | AbstractProductA * ConcreteFactory1::createProductA(){ 22 | return new ProductA1(); 23 | } 24 | 25 | 26 | AbstractProductB * ConcreteFactory1::createProductB(){ 27 | return new ProductB1(); 28 | } -------------------------------------------------------------------------------- /code/AbstractFactory/ConcreteFactory1.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory1.h 3 | // Implementation of the Class ConcreteFactory1 4 | // Created on: 02-十月-2014 15:04:11 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_A1BE35BD_23B8_4607_8EE7_7BCA32867D8E__INCLUDED_) 9 | #define EA_A1BE35BD_23B8_4607_8EE7_7BCA32867D8E__INCLUDED_ 10 | 11 | #include "AbstractFactory.h" 12 | #include "AbstractProductA.h" 13 | #include "AbstractProductB.h" 14 | 15 | 16 | class ConcreteFactory1 : public AbstractFactory 17 | { 18 | 19 | public: 20 | ConcreteFactory1(); 21 | virtual ~ConcreteFactory1(); 22 | 23 | virtual AbstractProductA * createProductA(); 24 | virtual AbstractProductB * createProductB(); 25 | 26 | }; 27 | #endif // !defined(EA_A1BE35BD_23B8_4607_8EE7_7BCA32867D8E__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/AbstractFactory/ConcreteFactory2.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory2.cpp 3 | // Implementation of the Class ConcreteFactory2 4 | // Created on: 02-十月-2014 15:04:13 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteFactory2.h" 9 | #include "ProductA2.h" 10 | #include "ProductB2.h" 11 | 12 | ConcreteFactory2::ConcreteFactory2(){ 13 | 14 | } 15 | 16 | ConcreteFactory2::~ConcreteFactory2(){ 17 | 18 | } 19 | 20 | 21 | AbstractProductA * ConcreteFactory2::createProductA(){ 22 | return new ProductA2(); 23 | } 24 | 25 | 26 | AbstractProductB * ConcreteFactory2::createProductB(){ 27 | return new ProductB2(); 28 | } 29 | 30 | -------------------------------------------------------------------------------- /code/AbstractFactory/ConcreteFactory2.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory2.h 3 | // Implementation of the Class ConcreteFactory2 4 | // Created on: 02-十月-2014 15:04:13 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_E8105A80_B2A3_4ec2_821E_1061C896A888__INCLUDED_) 9 | #define EA_E8105A80_B2A3_4ec2_821E_1061C896A888__INCLUDED_ 10 | 11 | #include "AbstractFactory.h" 12 | #include "AbstractProductA.h" 13 | #include "AbstractProductB.h" 14 | 15 | class ConcreteFactory2 : public AbstractFactory 16 | { 17 | 18 | public: 19 | ConcreteFactory2(); 20 | virtual ~ConcreteFactory2(); 21 | 22 | virtual AbstractProductA * createProductA(); 23 | virtual AbstractProductB * createProductB(); 24 | 25 | }; 26 | #endif // !defined(EA_E8105A80_B2A3_4ec2_821E_1061C896A888__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/AbstractFactory/ProductA1.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductA1.cpp 3 | // Implementation of the Class ProductA1 4 | // Created on: 02-十月-2014 15:04:17 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ProductA1.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ProductA1::ProductA1(){ 14 | 15 | } 16 | 17 | ProductA1::~ProductA1(){ 18 | 19 | } 20 | 21 | void ProductA1::use(){ 22 | cout << "use Product A1" << endl; 23 | } -------------------------------------------------------------------------------- /code/AbstractFactory/ProductA1.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductA1.h 3 | // Implementation of the Class ProductA1 4 | // Created on: 02-十月-2014 15:04:16 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_A2B201B2_C47D_41bd_9B53_C404C047CB78__INCLUDED_) 9 | #define EA_A2B201B2_C47D_41bd_9B53_C404C047CB78__INCLUDED_ 10 | 11 | #include "AbstractProductA.h" 12 | 13 | class ProductA1 : public AbstractProductA 14 | { 15 | 16 | public: 17 | ProductA1(); 18 | virtual ~ProductA1(); 19 | 20 | void use(); 21 | 22 | }; 23 | #endif // !defined(EA_A2B201B2_C47D_41bd_9B53_C404C047CB78__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/AbstractFactory/ProductA2.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductA2.cpp 3 | // Implementation of the Class ProductA2 4 | // Created on: 02-十月-2014 15:04:18 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ProductA2.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ProductA2::ProductA2(){ 14 | 15 | } 16 | 17 | 18 | 19 | ProductA2::~ProductA2(){ 20 | 21 | } 22 | 23 | 24 | void ProductA2::use(){ 25 | cout << "use Product A2" << endl; 26 | } -------------------------------------------------------------------------------- /code/AbstractFactory/ProductA2.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductA2.h 3 | // Implementation of the Class ProductA2 4 | // Created on: 02-十月-2014 15:04:18 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_FA4DE366_9B79_4c87_B2A6_197B4713DFB1__INCLUDED_) 9 | #define EA_FA4DE366_9B79_4c87_B2A6_197B4713DFB1__INCLUDED_ 10 | 11 | #include "AbstractProductA.h" 12 | 13 | class ProductA2 : public AbstractProductA 14 | { 15 | 16 | public: 17 | ProductA2(); 18 | virtual ~ProductA2(); 19 | 20 | void use(); 21 | 22 | }; 23 | #endif // !defined(EA_FA4DE366_9B79_4c87_B2A6_197B4713DFB1__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/AbstractFactory/ProductB1.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductB1.cpp 3 | // Implementation of the Class ProductB1 4 | // Created on: 02-十月-2014 15:04:19 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ProductB1.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ProductB1::ProductB1(){ 14 | 15 | } 16 | 17 | 18 | 19 | ProductB1::~ProductB1(){ 20 | 21 | } 22 | 23 | void ProductB1::eat(){ 24 | cout << "eat Product B1" << endl; 25 | } -------------------------------------------------------------------------------- /code/AbstractFactory/ProductB1.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductB1.h 3 | // Implementation of the Class ProductB1 4 | // Created on: 02-十月-2014 15:04:19 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_CAAE93FF_EC03_413c_A931_0528F68AA18A__INCLUDED_) 9 | #define EA_CAAE93FF_EC03_413c_A931_0528F68AA18A__INCLUDED_ 10 | 11 | #include "AbstractProductB.h" 12 | 13 | class ProductB1 : public AbstractProductB 14 | { 15 | 16 | public: 17 | ProductB1(); 18 | virtual ~ProductB1(); 19 | 20 | void eat(); 21 | 22 | }; 23 | #endif // !defined(EA_CAAE93FF_EC03_413c_A931_0528F68AA18A__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/AbstractFactory/ProductB2.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductB2.cpp 3 | // Implementation of the Class ProductB2 4 | // Created on: 02-十月-2014 15:04:20 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ProductB2.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | 14 | ProductB2::ProductB2(){ 15 | 16 | } 17 | 18 | 19 | 20 | ProductB2::~ProductB2(){ 21 | 22 | } 23 | 24 | void ProductB2::eat(){ 25 | cout << "eat Product B2" << endl; 26 | } -------------------------------------------------------------------------------- /code/AbstractFactory/ProductB2.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ProductB2.h 3 | // Implementation of the Class ProductB2 4 | // Created on: 02-十月-2014 15:04:20 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_D451967B_37E0_4776_9D7A_85DD6DC72821__INCLUDED_) 9 | #define EA_D451967B_37E0_4776_9D7A_85DD6DC72821__INCLUDED_ 10 | 11 | #include "AbstractProductB.h" 12 | 13 | class ProductB2 : public AbstractProductB 14 | { 15 | 16 | public: 17 | ProductB2(); 18 | virtual ~ProductB2(); 19 | 20 | void eat(); 21 | 22 | }; 23 | #endif // !defined(EA_D451967B_37E0_4776_9D7A_85DD6DC72821__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/AbstractFactory/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/AbstractFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "AbstractFactory.h" 3 | #include "AbstractProductA.h" 4 | #include "AbstractProductB.h" 5 | #include "ConcreteFactory1.h" 6 | #include "ConcreteFactory2.h" 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | AbstractFactory * fc = new ConcreteFactory1(); 12 | AbstractProductA * pa = fc->createProductA(); 13 | AbstractProductB * pb = fc->createProductB(); 14 | pa->use(); 15 | pb->eat(); 16 | 17 | AbstractFactory * fc2 = new ConcreteFactory2(); 18 | AbstractProductA * pa2 = fc2->createProductA(); 19 | AbstractProductB * pb2 = fc2->createProductB(); 20 | pa2->use(); 21 | pb2->eat(); 22 | 23 | delete fc; 24 | delete pa; 25 | delete pb; 26 | delete fc2; 27 | delete pa2; 28 | delete pb2; 29 | 30 | return 0; 31 | } 32 | -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/AbstractFactory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/AbstractFactory.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/AbstractProductA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/AbstractProductA.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/AbstractProductB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/AbstractProductB.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ConcreteFactory1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ConcreteFactory1.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ConcreteFactory2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ConcreteFactory2.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ProductA1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ProductA1.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ProductA2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ProductA2.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ProductB1.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ProductB1.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/ProductB2.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/ProductB2.o -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/main.exe -------------------------------------------------------------------------------- /code/AbstractFactory/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/AbstractFactory/mingw5/main.o -------------------------------------------------------------------------------- /code/Adapter/Adaptee.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Adaptee.cpp 3 | // Implementation of the Class Adaptee 4 | // Created on: 03-十月-2014 17:32:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Adaptee.h" 9 | #include 10 | using namespace std; 11 | 12 | Adaptee::Adaptee(){ 13 | 14 | } 15 | 16 | Adaptee::~Adaptee(){ 17 | 18 | } 19 | 20 | void Adaptee::specificRequest(){ 21 | cout << "specificRequest()|this is real Request from Adaptee!" << endl; 22 | } -------------------------------------------------------------------------------- /code/Adapter/Adaptee.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Adaptee.h 3 | // Implementation of the Class Adaptee 4 | // Created on: 03-十月-2014 17:32:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_826E6B4F_12BE_4609_A0A3_95BD5E657D36__INCLUDED_) 9 | #define EA_826E6B4F_12BE_4609_A0A3_95BD5E657D36__INCLUDED_ 10 | 11 | class Adaptee 12 | { 13 | 14 | public: 15 | Adaptee(); 16 | virtual ~Adaptee(); 17 | 18 | void specificRequest(); 19 | 20 | }; 21 | #endif // !defined(EA_826E6B4F_12BE_4609_A0A3_95BD5E657D36__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Adapter/Adapter.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Adapter.cpp 3 | // Implementation of the Class Adapter 4 | // Created on: 03-十月-2014 17:32:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Adapter.h" 9 | 10 | Adapter::Adapter(Adaptee * adaptee){ 11 | m_pAdaptee = adaptee; 12 | } 13 | 14 | Adapter::~Adapter(){ 15 | 16 | } 17 | 18 | void Adapter::request(){ 19 | m_pAdaptee->specificRequest(); 20 | } -------------------------------------------------------------------------------- /code/Adapter/Adapter.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Adapter.h 3 | // Implementation of the Class Adapter 4 | // Created on: 03-十月-2014 17:32:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_BD766D47_0C69_4131_B7B9_21DF78B1E80D__INCLUDED_) 9 | #define EA_BD766D47_0C69_4131_B7B9_21DF78B1E80D__INCLUDED_ 10 | 11 | #include "Target.h" 12 | #include "Adaptee.h" 13 | 14 | class Adapter : public Target 15 | { 16 | 17 | public: 18 | Adapter(Adaptee *adaptee); 19 | virtual ~Adapter(); 20 | 21 | virtual void request(); 22 | 23 | private: 24 | Adaptee* m_pAdaptee; 25 | 26 | }; 27 | #endif // !defined(EA_BD766D47_0C69_4131_B7B9_21DF78B1E80D__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/Adapter/Client.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.cpp 3 | // Implementation of the Class Client 4 | // Created on: 03-十月-2014 17:32:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Client.h" 9 | 10 | 11 | Client::Client(){ 12 | 13 | } 14 | 15 | 16 | 17 | Client::~Client(){ 18 | 19 | } -------------------------------------------------------------------------------- /code/Adapter/Client.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.h 3 | // Implementation of the Class Client 4 | // Created on: 03-十月-2014 17:32:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_E8E0A9E0_55D9_4f95_81DE_7D51BC6C4087__INCLUDED_) 9 | #define EA_E8E0A9E0_55D9_4f95_81DE_7D51BC6C4087__INCLUDED_ 10 | 11 | class Client 12 | { 13 | 14 | public: 15 | Client(); 16 | virtual ~Client(); 17 | 18 | }; 19 | #endif // !defined(EA_E8E0A9E0_55D9_4f95_81DE_7D51BC6C4087__INCLUDED_) 20 | -------------------------------------------------------------------------------- /code/Adapter/Target.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Target.cpp 3 | // Implementation of the Class Target 4 | // Created on: 03-十月-2014 17:32:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Target.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | Target::Target(){ 14 | 15 | } 16 | 17 | 18 | 19 | Target::~Target(){ 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | void Target::request(){ 28 | cout << "this is original request " << endl; 29 | } -------------------------------------------------------------------------------- /code/Adapter/Target.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Target.h 3 | // Implementation of the Class Target 4 | // Created on: 03-十月-2014 17:32:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_F694149D_AE3C_4295_962B_85E6916C402F__INCLUDED_) 9 | #define EA_F694149D_AE3C_4295_962B_85E6916C402F__INCLUDED_ 10 | 11 | class Target 12 | { 13 | 14 | public: 15 | Target(); 16 | virtual ~Target(); 17 | 18 | virtual void request(); 19 | 20 | }; 21 | #endif // !defined(EA_F694149D_AE3C_4295_962B_85E6916C402F__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Adapter/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Adapter/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Adapter.h" 3 | #include "Adaptee.h" 4 | #include "Target.h" 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | Adaptee * adaptee = new Adaptee(); 11 | Target * tar = new Adapter(adaptee); 12 | tar->request(); 13 | 14 | return 0; 15 | } 16 | -------------------------------------------------------------------------------- /code/Adapter/mingw5/Adaptee.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Adapter/mingw5/Adaptee.o -------------------------------------------------------------------------------- /code/Adapter/mingw5/Adapter.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Adapter/mingw5/Adapter.o -------------------------------------------------------------------------------- /code/Adapter/mingw5/Target.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Adapter/mingw5/Target.o -------------------------------------------------------------------------------- /code/Adapter/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Adapter/mingw5/main.exe -------------------------------------------------------------------------------- /code/Adapter/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Adapter/mingw5/main.o -------------------------------------------------------------------------------- /code/Bridge/Abstraction.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Abstraction.cpp 3 | // Implementation of the Class Abstraction 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Abstraction.h" 9 | 10 | 11 | Abstraction::Abstraction(){ 12 | 13 | } 14 | 15 | 16 | 17 | Abstraction::~Abstraction(){ 18 | delete m_pImp; 19 | } 20 | 21 | 22 | 23 | 24 | 25 | Abstraction::Abstraction(Implementor* imp){ 26 | m_pImp = imp; 27 | } 28 | 29 | 30 | void Abstraction::operation(){ 31 | 32 | } -------------------------------------------------------------------------------- /code/Bridge/Abstraction.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Abstraction.h 3 | // Implementation of the Class Abstraction 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_E8BB74E1_58E2_463e_B8EE_5EC7E8E218B0__INCLUDED_) 9 | #define EA_E8BB74E1_58E2_463e_B8EE_5EC7E8E218B0__INCLUDED_ 10 | 11 | #include "Implementor.h" 12 | 13 | class Abstraction 14 | { 15 | 16 | public: 17 | Abstraction(); 18 | virtual ~Abstraction(); 19 | 20 | Abstraction(Implementor* imp); 21 | virtual void operation(); 22 | 23 | protected: 24 | Implementor* m_pImp; 25 | 26 | }; 27 | #endif // !defined(EA_E8BB74E1_58E2_463e_B8EE_5EC7E8E218B0__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/Bridge/ConcreteImplementorA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteImplementorA.cpp 3 | // Implementation of the Class ConcreteImplementorA 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteImplementorA.h" 9 | #include 10 | using namespace std; 11 | 12 | ConcreteImplementorA::ConcreteImplementorA(){ 13 | 14 | } 15 | 16 | 17 | 18 | ConcreteImplementorA::~ConcreteImplementorA(){ 19 | 20 | } 21 | 22 | 23 | 24 | 25 | 26 | void ConcreteImplementorA::operationImp(){ 27 | cout << "imp in ConcreteImplementorA style." << endl; 28 | } -------------------------------------------------------------------------------- /code/Bridge/ConcreteImplementorA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteImplementorA.h 3 | // Implementation of the Class ConcreteImplementorA 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_77F5D11B_99CE_4c63_9971_9226E73FCBD1__INCLUDED_) 9 | #define EA_77F5D11B_99CE_4c63_9971_9226E73FCBD1__INCLUDED_ 10 | 11 | #include "Implementor.h" 12 | 13 | class ConcreteImplementorA : public Implementor 14 | { 15 | 16 | public: 17 | ConcreteImplementorA(); 18 | virtual ~ConcreteImplementorA(); 19 | 20 | virtual void operationImp(); 21 | 22 | }; 23 | #endif // !defined(EA_77F5D11B_99CE_4c63_9971_9226E73FCBD1__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Bridge/ConcreteImplementorB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteImplementorB.cpp 3 | // Implementation of the Class ConcreteImplementorB 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteImplementorB.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteImplementorB::ConcreteImplementorB(){ 14 | 15 | } 16 | 17 | 18 | 19 | ConcreteImplementorB::~ConcreteImplementorB(){ 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | void ConcreteImplementorB::operationImp(){ 28 | cout << "imp in ConcreteImplementorB style." << endl; 29 | } -------------------------------------------------------------------------------- /code/Bridge/ConcreteImplementorB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteImplementorB.h 3 | // Implementation of the Class ConcreteImplementorB 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_ED81E431_10B1_41f3_A4D6_037143E5773B__INCLUDED_) 9 | #define EA_ED81E431_10B1_41f3_A4D6_037143E5773B__INCLUDED_ 10 | 11 | #include "Implementor.h" 12 | 13 | class ConcreteImplementorB : public Implementor 14 | { 15 | 16 | public: 17 | ConcreteImplementorB(); 18 | virtual ~ConcreteImplementorB(); 19 | 20 | virtual void operationImp(); 21 | 22 | }; 23 | #endif // !defined(EA_ED81E431_10B1_41f3_A4D6_037143E5773B__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Bridge/Implementor.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Implementor.cpp 3 | // Implementation of the Class Implementor 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Implementor.h" 9 | 10 | 11 | Implementor::Implementor(){ 12 | 13 | } 14 | 15 | 16 | 17 | Implementor::~Implementor(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Implementor::operationImp(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Bridge/Implementor.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Implementor.h 3 | // Implementation of the Class Implementor 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_DBC56CB0_0310_476d_A44C_C159323E3181__INCLUDED_) 9 | #define EA_DBC56CB0_0310_476d_A44C_C159323E3181__INCLUDED_ 10 | 11 | class Implementor 12 | { 13 | 14 | public: 15 | Implementor(); 16 | virtual ~Implementor(); 17 | 18 | virtual void operationImp(); 19 | 20 | }; 21 | #endif // !defined(EA_DBC56CB0_0310_476d_A44C_C159323E3181__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Bridge/RefinedAbstraction.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // RefinedAbstraction.cpp 3 | // Implementation of the Class RefinedAbstraction 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "RefinedAbstraction.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | RefinedAbstraction::RefinedAbstraction(){ 14 | 15 | } 16 | 17 | RefinedAbstraction::RefinedAbstraction(Implementor* imp) 18 | :Abstraction(imp) 19 | { 20 | } 21 | 22 | RefinedAbstraction::~RefinedAbstraction(){ 23 | 24 | } 25 | 26 | void RefinedAbstraction::operation(){ 27 | cout << "do something else ,and then " << endl; 28 | m_pImp->operationImp(); 29 | } 30 | 31 | -------------------------------------------------------------------------------- /code/Bridge/RefinedAbstraction.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // RefinedAbstraction.h 3 | // Implementation of the Class RefinedAbstraction 4 | // Created on: 03-十月-2014 18:12:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_4BA5BE7C_DED5_4236_8362_F2988921CFA7__INCLUDED_) 9 | #define EA_4BA5BE7C_DED5_4236_8362_F2988921CFA7__INCLUDED_ 10 | 11 | #include "Abstraction.h" 12 | 13 | class RefinedAbstraction : public Abstraction 14 | { 15 | 16 | public: 17 | RefinedAbstraction(); 18 | RefinedAbstraction(Implementor* imp); 19 | virtual ~RefinedAbstraction(); 20 | 21 | virtual void operation(); 22 | 23 | }; 24 | #endif // !defined(EA_4BA5BE7C_DED5_4236_8362_F2988921CFA7__INCLUDED_) 25 | -------------------------------------------------------------------------------- /code/Bridge/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Bridge/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteImplementorA.h" 3 | #include "ConcreteImplementorB.h" 4 | #include "RefinedAbstraction.h" 5 | #include "Abstraction.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | 12 | Implementor * pImp = new ConcreteImplementorA(); 13 | Abstraction * pa = new RefinedAbstraction(pImp); 14 | pa->operation(); 15 | 16 | Abstraction * pb = new RefinedAbstraction(new ConcreteImplementorB()); 17 | pb->operation(); 18 | 19 | delete pa; 20 | delete pb; 21 | 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /code/Bridge/mingw5/Abstraction.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/Abstraction.o -------------------------------------------------------------------------------- /code/Bridge/mingw5/ConcreteImplementorA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/ConcreteImplementorA.o -------------------------------------------------------------------------------- /code/Bridge/mingw5/ConcreteImplementorB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/ConcreteImplementorB.o -------------------------------------------------------------------------------- /code/Bridge/mingw5/Implementor.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/Implementor.o -------------------------------------------------------------------------------- /code/Bridge/mingw5/RefinedAbstraction.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/RefinedAbstraction.o -------------------------------------------------------------------------------- /code/Bridge/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/main.exe -------------------------------------------------------------------------------- /code/Bridge/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Bridge/mingw5/main.o -------------------------------------------------------------------------------- /code/Builder/Builder.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Builder.cpp 3 | // Implementation of the Class Builder 4 | // Created on: 02-十月-2014 15:57:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Builder.h" 9 | 10 | 11 | Builder::Builder(){ 12 | m_prod = new Product(); 13 | } 14 | 15 | 16 | 17 | Builder::~Builder(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Builder::buildPartA(){ 26 | 27 | } 28 | 29 | 30 | void Builder::buildPartB(){ 31 | 32 | } 33 | 34 | 35 | void Builder::buildPartC(){ 36 | 37 | } 38 | 39 | 40 | Product* Builder::getResult(){ 41 | return m_prod; 42 | } -------------------------------------------------------------------------------- /code/Builder/Builder.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Builder.h 3 | // Implementation of the Class Builder 4 | // Created on: 02-十月-2014 15:57:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_938F1725_29F0_4174_93A3_D49DAB5D16A0__INCLUDED_) 9 | #define EA_938F1725_29F0_4174_93A3_D49DAB5D16A0__INCLUDED_ 10 | #include "Product.h" 11 | 12 | class Builder 13 | { 14 | 15 | public: 16 | Builder(); 17 | virtual ~Builder(); 18 | 19 | virtual void buildPartA(); 20 | virtual void buildPartB(); 21 | virtual void buildPartC(); 22 | virtual Product * getResult(); 23 | protected : 24 | Product * m_prod; 25 | }; 26 | #endif // !defined(EA_938F1725_29F0_4174_93A3_D49DAB5D16A0__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/Builder/ConcreteBuilder.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteBuilder.cpp 3 | // Implementation of the Class ConcreteBuilder 4 | // Created on: 02-十月-2014 15:57:03 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteBuilder.h" 9 | 10 | 11 | ConcreteBuilder::ConcreteBuilder(){ 12 | 13 | } 14 | 15 | 16 | 17 | ConcreteBuilder::~ConcreteBuilder(){ 18 | 19 | } 20 | 21 | void ConcreteBuilder::buildPartA(){ 22 | m_prod->setA("A Style "); //不同的建造者,可以实现不同产品的建造 23 | } 24 | 25 | 26 | void ConcreteBuilder::buildPartB(){ 27 | m_prod->setB("B Style "); 28 | } 29 | 30 | 31 | void ConcreteBuilder::buildPartC(){ 32 | m_prod->setC("C style "); 33 | } -------------------------------------------------------------------------------- /code/Builder/ConcreteBuilder.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteBuilder.h 3 | // Implementation of the Class ConcreteBuilder 4 | // Created on: 02-十月-2014 15:57:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_8FD38521_8504_468b_A6E5_D23DE3F1E1F1__INCLUDED_) 9 | #define EA_8FD38521_8504_468b_A6E5_D23DE3F1E1F1__INCLUDED_ 10 | 11 | #include "Builder.h" 12 | 13 | class ConcreteBuilder : public Builder 14 | { 15 | 16 | public: 17 | ConcreteBuilder(); 18 | virtual ~ConcreteBuilder(); 19 | 20 | virtual void buildPartA(); 21 | virtual void buildPartB(); 22 | virtual void buildPartC(); 23 | 24 | }; 25 | #endif // !defined(EA_8FD38521_8504_468b_A6E5_D23DE3F1E1F1__INCLUDED_) 26 | -------------------------------------------------------------------------------- /code/Builder/Director.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Director.cpp 3 | // Implementation of the Class Director 4 | // Created on: 02-十月-2014 15:57:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Director.h" 9 | 10 | Director::Director(){ 11 | } 12 | 13 | Director::~Director(){ 14 | } 15 | 16 | Product* Director::constuct(){ 17 | m_pbuilder->buildPartA(); 18 | m_pbuilder->buildPartB(); 19 | m_pbuilder->buildPartC(); 20 | 21 | return m_pbuilder->getResult(); 22 | } 23 | 24 | 25 | void Director::setBuilder(Builder* buider){ 26 | m_pbuilder = buider; 27 | } -------------------------------------------------------------------------------- /code/Builder/Director.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Director.h 3 | // Implementation of the Class Director 4 | // Created on: 02-十月-2014 15:57:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_211CC7ED_6EAC_41b3_9A9B_BB97BCCF0D14__INCLUDED_) 9 | #define EA_211CC7ED_6EAC_41b3_9A9B_BB97BCCF0D14__INCLUDED_ 10 | 11 | #include "Builder.h" 12 | 13 | class Director 14 | { 15 | 16 | public: 17 | Director(); 18 | virtual ~Director(); 19 | Builder *m_Builder; 20 | 21 | Product* constuct(); 22 | void setBuilder(Builder* buider); 23 | 24 | private: 25 | Builder * m_pbuilder; 26 | 27 | }; 28 | #endif // !defined(EA_211CC7ED_6EAC_41b3_9A9B_BB97BCCF0D14__INCLUDED_) 29 | -------------------------------------------------------------------------------- /code/Builder/Product.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.cpp 3 | // Implementation of the Class Product 4 | // Created on: 02-十月-2014 15:57:04 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Product.h" 9 | #include 10 | using namespace std; 11 | 12 | Product::Product(){ 13 | 14 | } 15 | 16 | 17 | 18 | Product::~Product(){ 19 | 20 | } 21 | 22 | void Product::setA(string str) 23 | { 24 | m_a = str; 25 | } 26 | 27 | void Product::setB(string str) 28 | { 29 | m_b = str; 30 | } 31 | 32 | void Product::setC(string str) 33 | { 34 | m_c = str; 35 | } 36 | 37 | void Product::show() 38 | { 39 | cout << "product has" << m_a << m_b << m_c << endl; 40 | } -------------------------------------------------------------------------------- /code/Builder/Product.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.h 3 | // Implementation of the Class Product 4 | // Created on: 02-十月-2014 15:57:04 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_8B945DE2_C5A7_4959_A62A_0055346B957E__INCLUDED_) 9 | #define EA_8B945DE2_C5A7_4959_A62A_0055346B957E__INCLUDED_ 10 | #include 11 | using namespace std; 12 | 13 | class Product 14 | { 15 | 16 | public: 17 | Product(); 18 | virtual ~Product(); 19 | 20 | void setA(string str); 21 | void setB(string str); 22 | void setC(string str); 23 | void show(); 24 | private: 25 | string m_a; 26 | string m_b; 27 | string m_c; 28 | }; 29 | #endif // !defined(EA_8B945DE2_C5A7_4959_A62A_0055346B957E__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Builder/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Builder/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteBuilder.h" 3 | #include "Director.h" 4 | #include "Builder.h" 5 | #include "Product.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | ConcreteBuilder * builder = new ConcreteBuilder(); 12 | Director director; 13 | director.setBuilder(builder); 14 | Product * pd = director.constuct(); 15 | pd->show(); 16 | 17 | delete builder; 18 | delete pd; 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /code/Builder/mingw5/Builder.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/Builder.o -------------------------------------------------------------------------------- /code/Builder/mingw5/ConcreteBuilder.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/ConcreteBuilder.o -------------------------------------------------------------------------------- /code/Builder/mingw5/Director.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/Director.o -------------------------------------------------------------------------------- /code/Builder/mingw5/Product.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/Product.o -------------------------------------------------------------------------------- /code/Builder/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/main.exe -------------------------------------------------------------------------------- /code/Builder/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Builder/mingw5/main.o -------------------------------------------------------------------------------- /code/Command/Command.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Command.cpp 3 | // Implementation of the Class Command 4 | // Created on: 07-十月-2014 17:44:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Command.h" 9 | 10 | 11 | Command::Command(){ 12 | 13 | } 14 | 15 | 16 | 17 | Command::~Command(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Command::execute(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Command/Command.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Command.h 3 | // Implementation of the Class Command 4 | // Created on: 07-十月-2014 17:44:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_DC004DA4_1FE9_4d5b_828F_6163F90CBEB9__INCLUDED_) 9 | #define EA_DC004DA4_1FE9_4d5b_828F_6163F90CBEB9__INCLUDED_ 10 | 11 | class Command 12 | { 13 | 14 | public: 15 | Command(); 16 | virtual ~Command(); 17 | 18 | virtual void execute(); 19 | 20 | }; 21 | #endif // !defined(EA_DC004DA4_1FE9_4d5b_828F_6163F90CBEB9__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Command/ConcreteCommand.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteCommand.cpp 3 | // Implementation of the Class ConcreteCommand 4 | // Created on: 07-十月-2014 17:44:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteCommand.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteCommand::ConcreteCommand(Receiver *pReceiver){ 14 | m_pReceiver = pReceiver; 15 | } 16 | 17 | 18 | 19 | ConcreteCommand::~ConcreteCommand(){ 20 | 21 | } 22 | 23 | void ConcreteCommand::execute(){ 24 | cout << "ConcreteCommand::execute" << endl; 25 | m_pReceiver->action(); 26 | } -------------------------------------------------------------------------------- /code/Command/ConcreteCommand.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteCommand.h 3 | // Implementation of the Class ConcreteCommand 4 | // Created on: 07-十月-2014 17:44:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_) 9 | #define EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_ 10 | 11 | #include "Command.h" 12 | #include "Receiver.h" 13 | 14 | class ConcreteCommand : public Command 15 | { 16 | 17 | public: 18 | ConcreteCommand(Receiver * pReceiver); 19 | virtual ~ConcreteCommand(); 20 | virtual void execute(); 21 | private: 22 | Receiver *m_pReceiver; 23 | 24 | 25 | 26 | }; 27 | #endif // !defined(EA_1AE70D53_4868_4e81_A1B8_1088DA355C23__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/Command/Invoker.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Invoker.cpp 3 | // Implementation of the Class Invoker 4 | // Created on: 07-十月-2014 17:44:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Invoker.h" 9 | #include 10 | using namespace std; 11 | 12 | Invoker::Invoker(Command * pCommand){ 13 | m_pCommand = pCommand; 14 | } 15 | 16 | Invoker::~Invoker(){ 17 | 18 | } 19 | 20 | void Invoker::call(){ 21 | cout << "invoker calling" << endl; 22 | m_pCommand->execute(); 23 | } -------------------------------------------------------------------------------- /code/Command/Invoker.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Invoker.h 3 | // Implementation of the Class Invoker 4 | // Created on: 07-十月-2014 17:44:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_) 9 | #define EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_ 10 | 11 | #include "Command.h" 12 | 13 | class Invoker 14 | { 15 | 16 | public: 17 | Invoker(Command * pCommand); 18 | virtual ~Invoker(); 19 | void call(); 20 | 21 | private: 22 | Command *m_pCommand; 23 | 24 | 25 | }; 26 | #endif // !defined(EA_3DACB62A_0813_4d11_8A82_10BF1FB00D9A__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/Command/Receiver.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Receiver.cpp 3 | // Implementation of the Class Receiver 4 | // Created on: 07-十月-2014 17:44:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Receiver.h" 9 | #include 10 | using namespace std; 11 | 12 | Receiver::Receiver(){ 13 | 14 | } 15 | 16 | Receiver::~Receiver(){ 17 | 18 | } 19 | 20 | void Receiver::action(){ 21 | cout << "receiver action." << endl; 22 | } -------------------------------------------------------------------------------- /code/Command/Receiver.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Receiver.h 3 | // Implementation of the Class Receiver 4 | // Created on: 07-十月-2014 17:44:02 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_) 9 | #define EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_ 10 | 11 | class Receiver 12 | { 13 | 14 | public: 15 | Receiver(); 16 | virtual ~Receiver(); 17 | 18 | void action(); 19 | 20 | }; 21 | #endif // !defined(EA_8E5430BB_0904_4a7d_9A3B_7169586237C8__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Command/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Command/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteCommand.h" 3 | #include "Invoker.h" 4 | #include "Receiver.h" 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | Receiver * pReceiver = new Receiver(); 11 | ConcreteCommand * pCommand = new ConcreteCommand(pReceiver); 12 | Invoker * pInvoker = new Invoker(pCommand); 13 | pInvoker->call(); 14 | 15 | delete pReceiver; 16 | delete pCommand; 17 | delete pInvoker; 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /code/Command/mingw5/Command.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/Command.o -------------------------------------------------------------------------------- /code/Command/mingw5/ConcreteCommand.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/ConcreteCommand.o -------------------------------------------------------------------------------- /code/Command/mingw5/Invoker.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/Invoker.o -------------------------------------------------------------------------------- /code/Command/mingw5/Receiver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/Receiver.o -------------------------------------------------------------------------------- /code/Command/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/main.exe -------------------------------------------------------------------------------- /code/Command/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Command/mingw5/main.o -------------------------------------------------------------------------------- /code/Decorator/Component.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Component.cpp 3 | // Implementation of the Class Component 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Component.h" 9 | 10 | 11 | Component::Component(){ 12 | 13 | } 14 | 15 | 16 | 17 | Component::~Component(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Component::operation(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Decorator/Component.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Component.h 3 | // Implementation of the Class Component 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_1FB0D8F7_EC40_4a8c_A5BC_ED236BBDC33E__INCLUDED_) 9 | #define EA_1FB0D8F7_EC40_4a8c_A5BC_ED236BBDC33E__INCLUDED_ 10 | 11 | class Component 12 | { 13 | 14 | public: 15 | Component(); 16 | virtual ~Component(); 17 | 18 | virtual void operation(); 19 | 20 | }; 21 | #endif // !defined(EA_1FB0D8F7_EC40_4a8c_A5BC_ED236BBDC33E__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Decorator/ConcreteComponent.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteComponent.cpp 3 | // Implementation of the Class ConcreteComponent 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteComponent.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteComponent::ConcreteComponent(){ 14 | 15 | } 16 | 17 | ConcreteComponent::~ConcreteComponent(){ 18 | 19 | } 20 | 21 | void ConcreteComponent::operation(){ 22 | cout << "ConcreteComponent's normal operation!" << endl; 23 | } -------------------------------------------------------------------------------- /code/Decorator/ConcreteComponent.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteComponent.h 3 | // Implementation of the Class ConcreteComponent 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_A20435BD_1DDE_439d_83AB_73AA7DAD469E__INCLUDED_) 9 | #define EA_A20435BD_1DDE_439d_83AB_73AA7DAD469E__INCLUDED_ 10 | 11 | #include "Component.h" 12 | 13 | class ConcreteComponent : public Component 14 | { 15 | 16 | public: 17 | ConcreteComponent(); 18 | virtual ~ConcreteComponent(); 19 | 20 | void operation(); 21 | 22 | }; 23 | #endif // !defined(EA_A20435BD_1DDE_439d_83AB_73AA7DAD469E__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Decorator/ConcreteDecoratorA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteDecoratorA.cpp 3 | // Implementation of the Class ConcreteDecoratorA 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteDecoratorA.h" 9 | #include 10 | using namespace std; 11 | 12 | ConcreteDecoratorA::ConcreteDecoratorA(Component* pcmp) 13 | :Decorator(pcmp) 14 | { 15 | 16 | } 17 | 18 | ConcreteDecoratorA::~ConcreteDecoratorA(){ 19 | 20 | } 21 | 22 | void ConcreteDecoratorA::addBehavior(){ 23 | cout << "addBehavior AAAA" << endl; 24 | } 25 | 26 | 27 | void ConcreteDecoratorA::operation(){ 28 | Decorator::operation(); 29 | addBehavior(); 30 | } -------------------------------------------------------------------------------- /code/Decorator/ConcreteDecoratorA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteDecoratorA.h 3 | // Implementation of the Class ConcreteDecoratorA 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_6786B68E_DCE4_44c4_B26D_812F0B3C0382__INCLUDED_) 9 | #define EA_6786B68E_DCE4_44c4_B26D_812F0B3C0382__INCLUDED_ 10 | 11 | #include "Decorator.h" 12 | #include "Component.h" 13 | 14 | class ConcreteDecoratorA : public Decorator 15 | { 16 | 17 | public: 18 | ConcreteDecoratorA(Component* pcmp); 19 | virtual ~ConcreteDecoratorA(); 20 | 21 | void addBehavior(); 22 | virtual void operation(); 23 | 24 | }; 25 | #endif // !defined(EA_6786B68E_DCE4_44c4_B26D_812F0B3C0382__INCLUDED_) 26 | -------------------------------------------------------------------------------- /code/Decorator/ConcreteDecoratorB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteDecoratorB.cpp 3 | // Implementation of the Class ConcreteDecoratorB 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteDecoratorB.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteDecoratorB::ConcreteDecoratorB(Component* pcmp) 14 | :Decorator(pcmp) 15 | { 16 | 17 | } 18 | 19 | 20 | 21 | ConcreteDecoratorB::~ConcreteDecoratorB(){ 22 | 23 | } 24 | 25 | 26 | 27 | 28 | 29 | void ConcreteDecoratorB::addBehavior(){ 30 | cout << "addBehavior BBBB" << endl; 31 | } 32 | 33 | 34 | void ConcreteDecoratorB::operation(){ 35 | Decorator::operation(); 36 | addBehavior(); 37 | } -------------------------------------------------------------------------------- /code/Decorator/ConcreteDecoratorB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteDecoratorB.h 3 | // Implementation of the Class ConcreteDecoratorB 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_D3774874_9201_4a73_956B_78973EC4E890__INCLUDED_) 9 | #define EA_D3774874_9201_4a73_956B_78973EC4E890__INCLUDED_ 10 | 11 | #include "Decorator.h" 12 | #include "Component.h" 13 | 14 | class ConcreteDecoratorB : public Decorator 15 | { 16 | 17 | public: 18 | ConcreteDecoratorB(Component* pcmp); 19 | virtual ~ConcreteDecoratorB(); 20 | 21 | void addBehavior(); 22 | virtual void operation(); 23 | 24 | }; 25 | #endif // !defined(EA_D3774874_9201_4a73_956B_78973EC4E890__INCLUDED_) 26 | -------------------------------------------------------------------------------- /code/Decorator/Decorator.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Decorator.cpp 3 | // Implementation of the Class Decorator 4 | // Created on: 03-十月-2014 18:53:01 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Decorator.h" 9 | 10 | 11 | Decorator::Decorator(Component* pcmp){ 12 | m_pComponent = pcmp; 13 | } 14 | 15 | Decorator::~Decorator(){ 16 | 17 | } 18 | 19 | void Decorator::operation(){ 20 | m_pComponent->operation(); 21 | } -------------------------------------------------------------------------------- /code/Decorator/Decorator.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Decorator.h 3 | // Implementation of the Class Decorator 4 | // Created on: 03-十月-2014 18:53:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_1B4529AE_AF10_4b83_B1D5_40C798AFFAAA__INCLUDED_) 9 | #define EA_1B4529AE_AF10_4b83_B1D5_40C798AFFAAA__INCLUDED_ 10 | 11 | #include "Component.h" 12 | 13 | class Decorator : public Component 14 | { 15 | 16 | public: 17 | Decorator(Component* pcmp); 18 | virtual ~Decorator(); 19 | 20 | void operation(); 21 | 22 | private: 23 | Component * m_pComponent; 24 | 25 | }; 26 | #endif // !defined(EA_1B4529AE_AF10_4b83_B1D5_40C798AFFAAA__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/Decorator/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Decorator/main.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/main.cpp -------------------------------------------------------------------------------- /code/Decorator/mingw5/Component.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/Component.o -------------------------------------------------------------------------------- /code/Decorator/mingw5/ConcreteComponent.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/ConcreteComponent.o -------------------------------------------------------------------------------- /code/Decorator/mingw5/ConcreteDecoratorA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/ConcreteDecoratorA.o -------------------------------------------------------------------------------- /code/Decorator/mingw5/ConcreteDecoratorB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/ConcreteDecoratorB.o -------------------------------------------------------------------------------- /code/Decorator/mingw5/Decorator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/Decorator.o -------------------------------------------------------------------------------- /code/Decorator/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/main.exe -------------------------------------------------------------------------------- /code/Decorator/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Decorator/mingw5/main.o -------------------------------------------------------------------------------- /code/Facade/Client.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.cpp 3 | // Implementation of the Class Client 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Client.h" 9 | 10 | 11 | Client::Client(){ 12 | 13 | } 14 | 15 | 16 | 17 | Client::~Client(){ 18 | 19 | } -------------------------------------------------------------------------------- /code/Facade/Client.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.h 3 | // Implementation of the Class Client 4 | // Created on: 06-十月-2014 19:10:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_FB09DAF1_FBE1_4f6e_BD52_72D69A9FCA56__INCLUDED_) 9 | #define EA_FB09DAF1_FBE1_4f6e_BD52_72D69A9FCA56__INCLUDED_ 10 | 11 | class Client 12 | { 13 | 14 | public: 15 | Client(); 16 | virtual ~Client(); 17 | 18 | }; 19 | #endif // !defined(EA_FB09DAF1_FBE1_4f6e_BD52_72D69A9FCA56__INCLUDED_) 20 | -------------------------------------------------------------------------------- /code/Facade/Facade.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Facade.cpp 3 | // Implementation of the Class Facade 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Facade.h" 9 | 10 | 11 | Facade::Facade(){ 12 | m_SystemA = new SystemA(); 13 | m_SystemB = new SystemB(); 14 | m_SystemC = new SystemC(); 15 | } 16 | 17 | 18 | 19 | Facade::~Facade(){ 20 | delete m_SystemA; 21 | delete m_SystemB; 22 | delete m_SystemC; 23 | } 24 | 25 | void Facade::wrapOpration(){ 26 | m_SystemA->operationA(); 27 | m_SystemB->operationB(); 28 | m_SystemC->opeartionC(); 29 | } -------------------------------------------------------------------------------- /code/Facade/Facade.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Facade.h 3 | // Implementation of the Class Facade 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_FD130A87_92A9_4168_9B33_7A925C47AFD5__INCLUDED_) 9 | #define EA_FD130A87_92A9_4168_9B33_7A925C47AFD5__INCLUDED_ 10 | 11 | #include "SystemC.h" 12 | #include "SystemA.h" 13 | #include "SystemB.h" 14 | 15 | class Facade 16 | { 17 | 18 | public: 19 | Facade(); 20 | virtual ~Facade(); 21 | 22 | void wrapOpration(); 23 | 24 | private: 25 | SystemC *m_SystemC; 26 | SystemA *m_SystemA; 27 | SystemB *m_SystemB; 28 | }; 29 | #endif // !defined(EA_FD130A87_92A9_4168_9B33_7A925C47AFD5__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Facade/SystemA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemA.cpp 3 | // Implementation of the Class SystemA 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "SystemA.h" 9 | #include 10 | using namespace std; 11 | 12 | SystemA::SystemA(){ 13 | 14 | } 15 | 16 | 17 | 18 | SystemA::~SystemA(){ 19 | 20 | } 21 | 22 | 23 | 24 | 25 | 26 | void SystemA::operationA(){ 27 | cout << "operationA" << endl; 28 | } -------------------------------------------------------------------------------- /code/Facade/SystemA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemA.h 3 | // Implementation of the Class SystemA 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_4EADC06E_6529_4fd1_B3B2_4C2A130E1229__INCLUDED_) 9 | #define EA_4EADC06E_6529_4fd1_B3B2_4C2A130E1229__INCLUDED_ 10 | 11 | class SystemA 12 | { 13 | 14 | public: 15 | SystemA(); 16 | virtual ~SystemA(); 17 | 18 | void operationA(); 19 | 20 | }; 21 | #endif // !defined(EA_4EADC06E_6529_4fd1_B3B2_4C2A130E1229__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Facade/SystemB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemB.cpp 3 | // Implementation of the Class SystemB 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "SystemB.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | SystemB::SystemB(){ 14 | 15 | } 16 | 17 | 18 | 19 | SystemB::~SystemB(){ 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | void SystemB::operationB(){ 28 | cout << "operationB" << endl; 29 | } -------------------------------------------------------------------------------- /code/Facade/SystemB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemB.h 3 | // Implementation of the Class SystemB 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_ED8A018A_4813_445c_BEB5_3C1A9C2D458E__INCLUDED_) 9 | #define EA_ED8A018A_4813_445c_BEB5_3C1A9C2D458E__INCLUDED_ 10 | 11 | class SystemB 12 | { 13 | 14 | public: 15 | SystemB(); 16 | virtual ~SystemB(); 17 | 18 | void operationB(); 19 | 20 | }; 21 | #endif // !defined(EA_ED8A018A_4813_445c_BEB5_3C1A9C2D458E__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Facade/SystemC.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemC.cpp 3 | // Implementation of the Class SystemC 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "SystemC.h" 9 | #include 10 | using namespace std; 11 | 12 | SystemC::SystemC(){ 13 | 14 | } 15 | 16 | 17 | 18 | SystemC::~SystemC(){ 19 | 20 | } 21 | 22 | 23 | 24 | 25 | 26 | void SystemC::opeartionC(){ 27 | cout << "operationC" << endl; 28 | } -------------------------------------------------------------------------------- /code/Facade/SystemC.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // SystemC.h 3 | // Implementation of the Class SystemC 4 | // Created on: 06-十月-2014 19:10:44 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_CC90A19E_B6A2_4247_ACCD_F39F749C19D0__INCLUDED_) 9 | #define EA_CC90A19E_B6A2_4247_ACCD_F39F749C19D0__INCLUDED_ 10 | 11 | class SystemC 12 | { 13 | 14 | public: 15 | SystemC(); 16 | virtual ~SystemC(); 17 | 18 | void opeartionC(); 19 | 20 | }; 21 | #endif // !defined(EA_CC90A19E_B6A2_4247_ACCD_F39F749C19D0__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Facade/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Facade/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Facade.h" 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | Facade fa; 8 | fa.wrapOpration(); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /code/Facade/mingw5/Facade.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/Facade.o -------------------------------------------------------------------------------- /code/Facade/mingw5/SystemA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/SystemA.o -------------------------------------------------------------------------------- /code/Facade/mingw5/SystemB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/SystemB.o -------------------------------------------------------------------------------- /code/Facade/mingw5/SystemC.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/SystemC.o -------------------------------------------------------------------------------- /code/Facade/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/main.exe -------------------------------------------------------------------------------- /code/Facade/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Facade/mingw5/main.o -------------------------------------------------------------------------------- /code/FactoryMethod/Client.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.cpp 3 | // Implementation of the Class Client 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Client.h" 9 | 10 | 11 | Client::Client(){ 12 | 13 | } 14 | 15 | 16 | 17 | Client::~Client(){ 18 | 19 | } -------------------------------------------------------------------------------- /code/FactoryMethod/Client.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Client.h 3 | // Implementation of the Class Client 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_18EFCEE9_550F_4132_986C_CB1894F63B40__INCLUDED_) 9 | #define EA_18EFCEE9_550F_4132_986C_CB1894F63B40__INCLUDED_ 10 | 11 | class Client 12 | { 13 | 14 | public: 15 | Client(); 16 | virtual ~Client(); 17 | 18 | }; 19 | #endif // !defined(EA_18EFCEE9_550F_4132_986C_CB1894F63B40__INCLUDED_) 20 | -------------------------------------------------------------------------------- /code/FactoryMethod/ConcreteFactory.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory.cpp 3 | // Implementation of the Class ConcreteFactory 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteFactory.h" 9 | #include "ConcreteProduct.h" 10 | 11 | ConcreteFactory::ConcreteFactory(){ 12 | 13 | } 14 | 15 | 16 | ConcreteFactory::~ConcreteFactory(){ 17 | 18 | } 19 | 20 | Product* ConcreteFactory::factoryMethod(){ 21 | 22 | return new ConcreteProduct(); 23 | } -------------------------------------------------------------------------------- /code/FactoryMethod/ConcreteFactory.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFactory.h 3 | // Implementation of the Class ConcreteFactory 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_99AEC7F3_304D_41c6_A35C_A81D7E5B280F__INCLUDED_) 9 | #define EA_99AEC7F3_304D_41c6_A35C_A81D7E5B280F__INCLUDED_ 10 | 11 | #include "Product.h" 12 | #include "Factory.h" 13 | 14 | class ConcreteFactory : public Factory 15 | { 16 | 17 | public: 18 | ConcreteFactory(); 19 | virtual ~ConcreteFactory(); 20 | 21 | virtual Product* factoryMethod(); 22 | 23 | }; 24 | #endif // !defined(EA_99AEC7F3_304D_41c6_A35C_A81D7E5B280F__INCLUDED_) 25 | -------------------------------------------------------------------------------- /code/FactoryMethod/ConcreteProduct.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProduct.cpp 3 | // Implementation of the Class ConcreteProduct 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteProduct.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteProduct::ConcreteProduct(){ 14 | 15 | } 16 | 17 | 18 | 19 | ConcreteProduct::~ConcreteProduct(){ 20 | 21 | } 22 | 23 | 24 | 25 | 26 | 27 | void ConcreteProduct::use(){ 28 | cout << "use prodect A" << endl; 29 | } -------------------------------------------------------------------------------- /code/FactoryMethod/ConcreteProduct.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProduct.h 3 | // Implementation of the Class ConcreteProduct 4 | // Created on: 02-十月-2014 10:18:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_EF9F81B2_79BC_4b45_BB45_60E30EE545C4__INCLUDED_) 9 | #define EA_EF9F81B2_79BC_4b45_BB45_60E30EE545C4__INCLUDED_ 10 | 11 | #include "Product.h" 12 | 13 | class ConcreteProduct : public Product 14 | { 15 | 16 | public: 17 | ConcreteProduct(); 18 | virtual ~ConcreteProduct(); 19 | 20 | virtual void use(); 21 | 22 | }; 23 | #endif // !defined(EA_EF9F81B2_79BC_4b45_BB45_60E30EE545C4__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/FactoryMethod/Factory.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Factory.cpp 3 | // Implementation of the Class Factory 4 | // Created on: 02-十月-2014 10:18:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Factory.h" 9 | #include 10 | 11 | 12 | Factory::Factory(){ 13 | 14 | } 15 | 16 | 17 | 18 | Factory::~Factory(){ 19 | 20 | } 21 | 22 | Product* Factory::factoryMethod(){ 23 | return NULL; 24 | } 25 | 26 | -------------------------------------------------------------------------------- /code/FactoryMethod/Factory.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Factory.h 3 | // Implementation of the Class Factory 4 | // Created on: 02-十月-2014 10:18:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_BB1E9945_CCF9_4d5c_8B7D_C3D86C0C2856__INCLUDED_) 9 | #define EA_BB1E9945_CCF9_4d5c_8B7D_C3D86C0C2856__INCLUDED_ 10 | 11 | #include "Product.h" 12 | 13 | class Factory 14 | { 15 | 16 | public: 17 | Factory(); 18 | virtual ~Factory(); 19 | 20 | virtual Product* factoryMethod(); 21 | 22 | }; 23 | #endif // !defined(EA_BB1E9945_CCF9_4d5c_8B7D_C3D86C0C2856__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/FactoryMethod/Product.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.cpp 3 | // Implementation of the Class Product 4 | // Created on: 02-十月-2014 10:19:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Product.h" 9 | 10 | 11 | Product::Product(){ 12 | 13 | } 14 | 15 | 16 | 17 | Product::~Product(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Product::use(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/FactoryMethod/Product.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.h 3 | // Implementation of the Class Product 4 | // Created on: 02-十月-2014 10:19:00 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_071D24B8_F0A3_4f19_955A_8F5511036EF0__INCLUDED_) 9 | #define EA_071D24B8_F0A3_4f19_955A_8F5511036EF0__INCLUDED_ 10 | 11 | class Product 12 | { 13 | 14 | public: 15 | Product(); 16 | virtual ~Product(); 17 | 18 | virtual void use(); 19 | 20 | }; 21 | #endif // !defined(EA_071D24B8_F0A3_4f19_955A_8F5511036EF0__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/FactoryMethod/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/FactoryMethod/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Factory.h" 2 | #include "ConcreteFactory.h" 3 | #include "Product.h" 4 | #include 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | Factory * fc = new ConcreteFactory(); 10 | Product * prod = fc->factoryMethod(); 11 | prod->use(); 12 | 13 | delete fc; 14 | delete prod; 15 | 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/Client.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/Client.o -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/ConcreteFactory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/ConcreteFactory.o -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/ConcreteProduct.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/ConcreteProduct.o -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/Factory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/Factory.o -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/Product.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/Product.o -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/main.exe -------------------------------------------------------------------------------- /code/FactoryMethod/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/FactoryMethod/mingw5/main.o -------------------------------------------------------------------------------- /code/Flyweight/ConcreteFlyweight.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFlyweight.cpp 3 | // Implementation of the Class ConcreteFlyweight 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteFlyweight.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | ConcreteFlyweight::ConcreteFlyweight(string str){ 14 | intrinsicState = str; 15 | } 16 | 17 | ConcreteFlyweight::~ConcreteFlyweight(){ 18 | 19 | } 20 | 21 | void ConcreteFlyweight::operation(){ 22 | cout << "Flyweight[" << intrinsicState << "] do operation." << endl; 23 | } -------------------------------------------------------------------------------- /code/Flyweight/ConcreteFlyweight.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteFlyweight.h 3 | // Implementation of the Class ConcreteFlyweight 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_) 9 | #define EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_ 10 | 11 | #include "Flyweight.h" 12 | #include 13 | using namespace std; 14 | 15 | class ConcreteFlyweight : public Flyweight 16 | { 17 | 18 | public: 19 | ConcreteFlyweight(string str); 20 | virtual ~ConcreteFlyweight(); 21 | 22 | virtual void operation(); 23 | 24 | private: 25 | string intrinsicState; 26 | 27 | }; 28 | #endif // !defined(EA_C0AF438E_96E4_46f1_ADEC_308EF16E11D1__INCLUDED_) 29 | -------------------------------------------------------------------------------- /code/Flyweight/Flyweight.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Flyweight.cpp 3 | // Implementation of the Class Flyweight 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Flyweight.h" 9 | 10 | 11 | Flyweight::Flyweight(){ 12 | 13 | } 14 | 15 | 16 | 17 | Flyweight::~Flyweight(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Flyweight::operation(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Flyweight/Flyweight.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Flyweight.h 3 | // Implementation of the Class Flyweight 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_33A7FB32_88ED_4585_83D3_188CF17052A8__INCLUDED_) 9 | #define EA_33A7FB32_88ED_4585_83D3_188CF17052A8__INCLUDED_ 10 | 11 | class Flyweight 12 | { 13 | 14 | public: 15 | Flyweight(); 16 | virtual ~Flyweight(); 17 | 18 | virtual void operation(); 19 | 20 | }; 21 | #endif // !defined(EA_33A7FB32_88ED_4585_83D3_188CF17052A8__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Flyweight/FlyweightFactory.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // FlyweightFactory.cpp 3 | // Implementation of the Class FlyweightFactory 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "FlyweightFactory.h" 9 | #include "ConcreteFlyweight.h" 10 | #include 11 | using namespace std; 12 | 13 | FlyweightFactory::FlyweightFactory(){ 14 | 15 | } 16 | 17 | 18 | 19 | FlyweightFactory::~FlyweightFactory(){ 20 | 21 | } 22 | 23 | Flyweight* FlyweightFactory::getFlyweight(string str){ 24 | map::iterator itr = m_mpFlyweight.find(str); 25 | if(itr == m_mpFlyweight.end()) 26 | { 27 | Flyweight * fw = new ConcreteFlyweight(str); 28 | m_mpFlyweight.insert(make_pair(str,fw)); 29 | return fw; 30 | } 31 | else 32 | { 33 | cout << "aready in the pool,use the exist one:" << endl; 34 | return itr->second; 35 | } 36 | } -------------------------------------------------------------------------------- /code/Flyweight/FlyweightFactory.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // FlyweightFactory.h 3 | // Implementation of the Class FlyweightFactory 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_C0370E5F_AC7A_4f98_8E8B_CAA37A1EE7EA__INCLUDED_) 9 | #define EA_C0370E5F_AC7A_4f98_8E8B_CAA37A1EE7EA__INCLUDED_ 10 | 11 | #include "Flyweight.h" 12 | #include 13 | #include 14 | using namespace std; 15 | 16 | class FlyweightFactory 17 | { 18 | 19 | public: 20 | FlyweightFactory(); 21 | virtual ~FlyweightFactory(); 22 | 23 | Flyweight* getFlyweight(string str); 24 | 25 | private: 26 | map m_mpFlyweight; 27 | 28 | }; 29 | #endif // !defined(EA_C0370E5F_AC7A_4f98_8E8B_CAA37A1EE7EA__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Flyweight/UnsharedConcreteFlyweight.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // UnsharedConcreteFlyweight.cpp 3 | // Implementation of the Class UnsharedConcreteFlyweight 4 | // Created on: 06-十月-2014 20:10:43 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "UnsharedConcreteFlyweight.h" 9 | 10 | 11 | UnsharedConcreteFlyweight::UnsharedConcreteFlyweight(){ 12 | 13 | } 14 | 15 | 16 | 17 | UnsharedConcreteFlyweight::~UnsharedConcreteFlyweight(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void UnsharedConcreteFlyweight::operation(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Flyweight/UnsharedConcreteFlyweight.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // UnsharedConcreteFlyweight.h 3 | // Implementation of the Class UnsharedConcreteFlyweight 4 | // Created on: 06-十月-2014 20:10:42 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_63FB0A19_070D_4a21_BCE7_CB5834C0C14D__INCLUDED_) 9 | #define EA_63FB0A19_070D_4a21_BCE7_CB5834C0C14D__INCLUDED_ 10 | 11 | #include "Flyweight.h" 12 | 13 | class UnsharedConcreteFlyweight : public Flyweight 14 | { 15 | 16 | public: 17 | UnsharedConcreteFlyweight(); 18 | virtual ~UnsharedConcreteFlyweight(); 19 | 20 | void operation(); 21 | 22 | private: 23 | int allState; 24 | 25 | }; 26 | #endif // !defined(EA_63FB0A19_070D_4a21_BCE7_CB5834C0C14D__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/Flyweight/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Flyweight/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteFlyweight.h" 3 | #include "FlyweightFactory.h" 4 | #include "Flyweight.h" 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | FlyweightFactory factory; 10 | Flyweight * fw = factory.getFlyweight("one"); 11 | fw->operation(); 12 | 13 | Flyweight * fw2 = factory.getFlyweight("two"); 14 | fw2->operation(); 15 | //aready exist in pool 16 | Flyweight * fw3 = factory.getFlyweight("one"); 17 | fw3->operation(); 18 | return 0; 19 | } 20 | -------------------------------------------------------------------------------- /code/Flyweight/mingw5/ConcreteFlyweight.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Flyweight/mingw5/ConcreteFlyweight.o -------------------------------------------------------------------------------- /code/Flyweight/mingw5/Flyweight.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Flyweight/mingw5/Flyweight.o -------------------------------------------------------------------------------- /code/Flyweight/mingw5/FlyweightFactory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Flyweight/mingw5/FlyweightFactory.o -------------------------------------------------------------------------------- /code/Flyweight/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Flyweight/mingw5/main.exe -------------------------------------------------------------------------------- /code/Flyweight/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Flyweight/mingw5/main.o -------------------------------------------------------------------------------- /code/Mediator/Colleague.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Colleague.cpp 3 | // Implementation of the Class Colleague 4 | // Created on: 07-十月-2014 21:30:47 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Colleague.h" 9 | #include "Mediator.h" 10 | #include 11 | using namespace std; 12 | 13 | Colleague::Colleague(){ 14 | 15 | } 16 | 17 | 18 | 19 | Colleague::~Colleague(){ 20 | 21 | } 22 | 23 | void Colleague::receivemsg(string str){ 24 | cout << "reveivemsg:" << str < 13 | using namespace std; 14 | 15 | class Colleague 16 | { 17 | 18 | public: 19 | Colleague(); 20 | virtual ~Colleague(); 21 | 22 | virtual void receivemsg(string str); 23 | virtual void sendmsg(int toWho,string str); 24 | void setMediator(Mediator * aMediator); 25 | protected: 26 | Mediator * m_pMediator; 27 | 28 | }; 29 | #endif // !defined(EA_216CC2D4_9FEB_466b_B7F7_B7F5199798A8__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Mediator/ConcreteColleagueA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteColleagueA.cpp 3 | // Implementation of the Class ConcreteColleagueA 4 | // Created on: 07-十月-2014 21:30:47 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteColleagueA.h" 9 | #include 10 | using namespace std; 11 | 12 | ConcreteColleagueA::ConcreteColleagueA(){ 13 | } 14 | 15 | ConcreteColleagueA::~ConcreteColleagueA(){ 16 | } 17 | 18 | void ConcreteColleagueA::sendmsg(int toWho,string str){ 19 | cout << "send msg from colleagueA,to:" << toWho << endl; 20 | m_pMediator->operation(toWho,str); 21 | } 22 | 23 | void ConcreteColleagueA::receivemsg(string str){ 24 | cout << "ConcreteColleagueA reveivemsg:" << str < 10 | using namespace std; 11 | 12 | 13 | ConcreteColleagueB::ConcreteColleagueB(){ 14 | 15 | } 16 | 17 | 18 | 19 | ConcreteColleagueB::~ConcreteColleagueB(){ 20 | 21 | } 22 | 23 | void ConcreteColleagueB::sendmsg(int toWho,string str){ 24 | cout << "send msg from colleagueB,to:" << toWho << endl; 25 | m_pMediator->operation(toWho,str); 26 | } 27 | 28 | void ConcreteColleagueB::receivemsg(string str){ 29 | cout << "ConcreteColleagueB reveivemsg:" << str < 10 | #include 11 | using namespace std; 12 | 13 | ConcreteMediator::ConcreteMediator(){ 14 | 15 | } 16 | 17 | ConcreteMediator::~ConcreteMediator(){ 18 | 19 | } 20 | 21 | void ConcreteMediator::operation(int nWho,string str){ 22 | map::const_iterator itr = m_mpColleague.find(nWho); 23 | if(itr == m_mpColleague.end()) 24 | { 25 | cout << "not found this colleague!" << endl; 26 | return; 27 | } 28 | 29 | Colleague* pc = itr->second; 30 | pc->receivemsg(str); 31 | } 32 | 33 | 34 | void ConcreteMediator::registered(int nWho,Colleague * aColleague){ 35 | map::const_iterator itr = m_mpColleague.find(nWho); 36 | if(itr == m_mpColleague.end()) 37 | { 38 | m_mpColleague.insert(make_pair(nWho,aColleague)); 39 | //同时将中介类暴露给colleague 40 | aColleague->setMediator(this); 41 | } 42 | } -------------------------------------------------------------------------------- /code/Mediator/ConcreteMediator.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteMediator.h 3 | // Implementation of the Class ConcreteMediator 4 | // Created on: 07-十月-2014 21:30:47 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_8CECE546_61DD_456f_A3E7_D98BC078D8E8__INCLUDED_) 9 | #define EA_8CECE546_61DD_456f_A3E7_D98BC078D8E8__INCLUDED_ 10 | 11 | #include "ConcreteColleagueB.h" 12 | #include "Mediator.h" 13 | #include "ConcreteColleagueA.h" 14 | #include 15 | using namespace std; 16 | class ConcreteMediator : public Mediator 17 | { 18 | 19 | public: 20 | ConcreteMediator(); 21 | virtual ~ConcreteMediator(); 22 | 23 | virtual void operation(int nWho,string str); 24 | virtual void registered(int nWho, Colleague * aColleague); 25 | private: 26 | map m_mpColleague; 27 | }; 28 | #endif // !defined(EA_8CECE546_61DD_456f_A3E7_D98BC078D8E8__INCLUDED_) 29 | -------------------------------------------------------------------------------- /code/Mediator/Mediator.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Mediator.cpp 3 | // Implementation of the Class Mediator 4 | // Created on: 07-十月-2014 21:30:48 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Mediator.h" 9 | #include "Colleague.h" 10 | 11 | Mediator::Mediator(){ 12 | 13 | } 14 | 15 | Mediator::~Mediator(){ 16 | 17 | } 18 | 19 | void Mediator::operation(int nWho,string str){ 20 | } 21 | 22 | 23 | void Mediator::registered(int nWho,Colleague * aColleague){ 24 | } -------------------------------------------------------------------------------- /code/Mediator/Mediator.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Mediator.h 3 | // Implementation of the Class Mediator 4 | // Created on: 07-十月-2014 21:30:48 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_6E33A7CE_E410_460e_884D_607B81669C44__INCLUDED_) 9 | #define EA_6E33A7CE_E410_460e_884D_607B81669C44__INCLUDED_ 10 | 11 | #include 12 | 13 | using namespace std; 14 | 15 | // 注:由于两个类相互引用, 这里不能直接#include Colleague头文件,而使用声明; 16 | class Colleague; 17 | 18 | class Mediator 19 | { 20 | 21 | public: 22 | Mediator(); 23 | virtual ~Mediator(); 24 | 25 | virtual void operation(int nWho,string str); 26 | virtual void registered(int nWho, Colleague * aColleague); 27 | 28 | }; 29 | #endif // !defined(EA_6E33A7CE_E410_460e_884D_607B81669C44__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Mediator/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Mediator/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ConcreteColleagueA.h" 3 | #include "ConcreteMediator.h" 4 | #include "ConcreteColleagueB.h" 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | ConcreteColleagueA * pa = new ConcreteColleagueA(); 11 | ConcreteColleagueB * pb = new ConcreteColleagueB(); 12 | ConcreteMediator * pm = new ConcreteMediator(); 13 | pm->registered(1,pa); 14 | pm->registered(2,pb); 15 | 16 | // sendmsg from a to b 17 | pa->sendmsg(2,"hello,i am a"); 18 | // sendmsg from b to a 19 | pb->sendmsg(1,"hello,i am b"); 20 | 21 | delete pa,pb,pm; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /code/Mediator/mingw5/Colleague.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/Colleague.o -------------------------------------------------------------------------------- /code/Mediator/mingw5/ConcreteColleagueA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/ConcreteColleagueA.o -------------------------------------------------------------------------------- /code/Mediator/mingw5/ConcreteColleagueB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/ConcreteColleagueB.o -------------------------------------------------------------------------------- /code/Mediator/mingw5/ConcreteMediator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/ConcreteMediator.o -------------------------------------------------------------------------------- /code/Mediator/mingw5/Mediator.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/Mediator.o -------------------------------------------------------------------------------- /code/Mediator/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/main.exe -------------------------------------------------------------------------------- /code/Mediator/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Mediator/mingw5/main.o -------------------------------------------------------------------------------- /code/Obeserver/ConcreteObeserver.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteObeserver.cpp 3 | // Implementation of the Class ConcreteObeserver 4 | // Created on: 07-十月-2014 23:00:09 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteObeserver.h" 9 | #include 10 | #include 11 | #include "Subject.h" 12 | using namespace std; 13 | 14 | ConcreteObeserver::ConcreteObeserver(string name){ 15 | m_objName = name; 16 | } 17 | 18 | ConcreteObeserver::~ConcreteObeserver(){ 19 | 20 | } 21 | 22 | void ConcreteObeserver::update(Subject * sub){ 23 | m_obeserverState = sub->getState(); 24 | cout << "update oberserver[" << m_objName << "] state:" << m_obeserverState << endl; 25 | } 26 | -------------------------------------------------------------------------------- /code/Obeserver/ConcreteObeserver.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteObeserver.h 3 | // Implementation of the Class ConcreteObeserver 4 | // Created on: 07-十月-2014 23:00:09 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_7B020534_BFEA_4c9e_8E4C_34DCE001E9B1__INCLUDED_) 9 | #define EA_7B020534_BFEA_4c9e_8E4C_34DCE001E9B1__INCLUDED_ 10 | #include "Obeserver.h" 11 | #include 12 | using namespace std; 13 | 14 | class ConcreteObeserver : public Obeserver 15 | { 16 | 17 | public: 18 | ConcreteObeserver(string name); 19 | virtual ~ConcreteObeserver(); 20 | virtual void update(Subject * sub); 21 | 22 | private: 23 | string m_objName; 24 | int m_obeserverState; 25 | }; 26 | #endif // !defined(EA_7B020534_BFEA_4c9e_8E4C_34DCE001E9B1__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/Obeserver/ConcreteSubject.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteSubject.cpp 3 | // Implementation of the Class ConcreteSubject 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteSubject.h" 9 | 10 | 11 | ConcreteSubject::ConcreteSubject(){ 12 | 13 | } 14 | 15 | ConcreteSubject::~ConcreteSubject(){ 16 | 17 | } 18 | 19 | 20 | int ConcreteSubject::getState(){ 21 | return m_nState; 22 | } 23 | 24 | 25 | void ConcreteSubject::setState(int i){ 26 | m_nState = i; 27 | } 28 | -------------------------------------------------------------------------------- /code/Obeserver/ConcreteSubject.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteSubject.h 3 | // Implementation of the Class ConcreteSubject 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_13122F7D_1635_4e20_ADC6_87933BB1B603__INCLUDED_) 9 | #define EA_13122F7D_1635_4e20_ADC6_87933BB1B603__INCLUDED_ 10 | 11 | #include "Subject.h" 12 | 13 | class ConcreteSubject : public Subject 14 | { 15 | 16 | public: 17 | ConcreteSubject(); 18 | virtual ~ConcreteSubject(); 19 | 20 | virtual int getState(); 21 | virtual void setState(int i); 22 | 23 | private: 24 | int m_nState; 25 | 26 | }; 27 | #endif // !defined(EA_13122F7D_1635_4e20_ADC6_87933BB1B603__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/Obeserver/Obeserver.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Obeserver.cpp 3 | // Implementation of the Class Obeserver 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Obeserver.h" 9 | 10 | Obeserver::Obeserver(){ 11 | } 12 | 13 | Obeserver::~Obeserver(){ 14 | } -------------------------------------------------------------------------------- /code/Obeserver/Obeserver.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Obeserver.h 3 | // Implementation of the Class Obeserver 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_2C7362B2_0B22_4168_8690_F9C7B76C343F__INCLUDED_) 9 | #define EA_2C7362B2_0B22_4168_8690_F9C7B76C343F__INCLUDED_ 10 | 11 | class Subject; 12 | 13 | class Obeserver 14 | { 15 | 16 | public: 17 | Obeserver(); 18 | virtual ~Obeserver(); 19 | virtual void update(Subject * sub) = 0; 20 | }; 21 | #endif // !defined(EA_2C7362B2_0B22_4168_8690_F9C7B76C343F__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Obeserver/Subject.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Subject.cpp 3 | // Implementation of the Class Subject 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Subject.h" 9 | 10 | Subject::Subject(){ 11 | 12 | } 13 | 14 | Subject::~Subject(){ 15 | 16 | } 17 | 18 | void Subject::attach(Obeserver * pObeserver){ 19 | m_vtObj.push_back(pObeserver); 20 | } 21 | 22 | void Subject::detach(Obeserver * pObeserver){ 23 | for(vector::iterator itr = m_vtObj.begin(); 24 | itr != m_vtObj.end(); itr++) 25 | { 26 | if(*itr == pObeserver) 27 | { 28 | m_vtObj.erase(itr); 29 | return; 30 | } 31 | } 32 | } 33 | 34 | void Subject::notify(){ 35 | for(vector::iterator itr = m_vtObj.begin(); 36 | itr != m_vtObj.end(); 37 | itr++) 38 | { 39 | (*itr)->update(this); 40 | } 41 | } -------------------------------------------------------------------------------- /code/Obeserver/Subject.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Subject.h 3 | // Implementation of the Class Subject 4 | // Created on: 07-十月-2014 23:00:10 5 | // Original author: cl 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_61998456_1B61_49f4_B3EA_9D28EEBC9649__INCLUDED_) 9 | #define EA_61998456_1B61_49f4_B3EA_9D28EEBC9649__INCLUDED_ 10 | 11 | #include "Obeserver.h" 12 | #include 13 | using namespace std; 14 | 15 | class Subject 16 | { 17 | 18 | public: 19 | Subject(); 20 | virtual ~Subject(); 21 | Obeserver *m_Obeserver; 22 | 23 | void attach(Obeserver * pObeserver); 24 | void detach(Obeserver * pObeserver); 25 | void notify(); 26 | 27 | virtual int getState() = 0; 28 | virtual void setState(int i)= 0; 29 | 30 | private: 31 | vector m_vtObj; 32 | 33 | }; 34 | #endif // !defined(EA_61998456_1B61_49f4_B3EA_9D28EEBC9649__INCLUDED_) 35 | -------------------------------------------------------------------------------- /code/Obeserver/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Obeserver/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Subject.h" 3 | #include "Obeserver.h" 4 | #include "ConcreteObeserver.h" 5 | #include "ConcreteSubject.h" 6 | 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | Subject * subject = new ConcreteSubject(); 12 | Obeserver * objA = new ConcreteObeserver("A"); 13 | Obeserver * objB = new ConcreteObeserver("B"); 14 | subject->attach(objA); 15 | subject->attach(objB); 16 | 17 | subject->setState(1); 18 | subject->notify(); 19 | 20 | cout << "--------------------" << endl; 21 | subject->detach(objB); 22 | subject->setState(2); 23 | subject->notify(); 24 | 25 | delete subject; 26 | delete objA; 27 | delete objB; 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /code/Obeserver/mingw5/ConcreteObeserver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/ConcreteObeserver.o -------------------------------------------------------------------------------- /code/Obeserver/mingw5/ConcreteSubject.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/ConcreteSubject.o -------------------------------------------------------------------------------- /code/Obeserver/mingw5/Obeserver.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/Obeserver.o -------------------------------------------------------------------------------- /code/Obeserver/mingw5/Subject.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/Subject.o -------------------------------------------------------------------------------- /code/Obeserver/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/main.exe -------------------------------------------------------------------------------- /code/Obeserver/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Obeserver/mingw5/main.o -------------------------------------------------------------------------------- /code/Proxy/Proxy.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Proxy.cpp 3 | // Implementation of the Class Proxy 4 | // Created on: 07-十月-2014 16:57:54 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Proxy.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | Proxy::Proxy(){ 14 | //有人觉得 RealSubject对象的创建应该是在main中实现;我认为RealSubject应该 15 | //对用户是透明的,用户所面对的接口都是通过代理的;这样才是真正的代理; 16 | m_pRealSubject = new RealSubject(); 17 | } 18 | 19 | Proxy::~Proxy(){ 20 | delete m_pRealSubject; 21 | } 22 | 23 | void Proxy::afterRequest(){ 24 | cout << "Proxy::afterRequest" << endl; 25 | } 26 | 27 | 28 | void Proxy::preRequest(){ 29 | cout << "Proxy::preRequest" << endl; 30 | } 31 | 32 | 33 | void Proxy::request(){ 34 | preRequest(); 35 | m_pRealSubject->request(); 36 | afterRequest(); 37 | } -------------------------------------------------------------------------------- /code/Proxy/Proxy.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Proxy.h 3 | // Implementation of the Class Proxy 4 | // Created on: 07-十月-2014 16:57:54 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_56011290_0413_40c6_9132_63EE89B023FD__INCLUDED_) 9 | #define EA_56011290_0413_40c6_9132_63EE89B023FD__INCLUDED_ 10 | 11 | #include "RealSubject.h" 12 | #include "Subject.h" 13 | 14 | class Proxy : public Subject 15 | { 16 | 17 | public: 18 | Proxy(); 19 | virtual ~Proxy(); 20 | 21 | void request(); 22 | 23 | private: 24 | void afterRequest(); 25 | void preRequest(); 26 | RealSubject *m_pRealSubject; 27 | 28 | }; 29 | #endif // !defined(EA_56011290_0413_40c6_9132_63EE89B023FD__INCLUDED_) 30 | -------------------------------------------------------------------------------- /code/Proxy/RealSubject.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // RealSubject.cpp 3 | // Implementation of the Class RealSubject 4 | // Created on: 07-十月-2014 16:57:54 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "RealSubject.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | RealSubject::RealSubject(){ 14 | 15 | } 16 | 17 | 18 | 19 | RealSubject::~RealSubject(){ 20 | 21 | } 22 | 23 | void RealSubject::request(){ 24 | cout << "RealSubject::request" << endl; 25 | } -------------------------------------------------------------------------------- /code/Proxy/RealSubject.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // RealSubject.h 3 | // Implementation of the Class RealSubject 4 | // Created on: 07-十月-2014 16:57:54 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_5D2DD659_53C1_4a89_AFE3_762303A2AADD__INCLUDED_) 9 | #define EA_5D2DD659_53C1_4a89_AFE3_762303A2AADD__INCLUDED_ 10 | 11 | #include "Subject.h" 12 | 13 | class RealSubject : public Subject 14 | { 15 | 16 | public: 17 | RealSubject(); 18 | virtual ~RealSubject(); 19 | 20 | void request(); 21 | 22 | }; 23 | #endif // !defined(EA_5D2DD659_53C1_4a89_AFE3_762303A2AADD__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Proxy/Subject.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Subject.cpp 3 | // Implementation of the Class Subject 4 | // Created on: 07-十月-2014 16:57:55 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Subject.h" 9 | 10 | 11 | Subject::Subject(){ 12 | 13 | } 14 | 15 | 16 | 17 | Subject::~Subject(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Subject::request(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Proxy/Subject.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Subject.h 3 | // Implementation of the Class Subject 4 | // Created on: 07-十月-2014 16:57:55 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_5A04CB12_F247_4fe6_9A0C_64A15E180E2B__INCLUDED_) 9 | #define EA_5A04CB12_F247_4fe6_9A0C_64A15E180E2B__INCLUDED_ 10 | 11 | class Subject 12 | { 13 | 14 | public: 15 | Subject(); 16 | virtual ~Subject(); 17 | 18 | virtual void request(); 19 | 20 | }; 21 | #endif // !defined(EA_5A04CB12_F247_4fe6_9A0C_64A15E180E2B__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Proxy/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Proxy/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "RealSubject.h" 3 | #include "Proxy.h" 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | Proxy proxy; 10 | proxy.request(); 11 | 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /code/Proxy/mingw5/Proxy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Proxy/mingw5/Proxy.o -------------------------------------------------------------------------------- /code/Proxy/mingw5/RealSubject.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Proxy/mingw5/RealSubject.o -------------------------------------------------------------------------------- /code/Proxy/mingw5/Subject.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Proxy/mingw5/Subject.o -------------------------------------------------------------------------------- /code/Proxy/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Proxy/mingw5/main.exe -------------------------------------------------------------------------------- /code/Proxy/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Proxy/mingw5/main.o -------------------------------------------------------------------------------- /code/SimpleFactory/ConcreteProductA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProductA.cpp 3 | // Implementation of the Class ConcreteProductA 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteProductA.h" 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | ConcreteProductA::ConcreteProductA(){ 15 | 16 | } 17 | 18 | 19 | 20 | ConcreteProductA::~ConcreteProductA(){ 21 | 22 | } 23 | 24 | void ConcreteProductA::Use() 25 | { 26 | cout << "use productB" << endl; 27 | } -------------------------------------------------------------------------------- /code/SimpleFactory/ConcreteProductA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProductA.h 3 | // Implementation of the Class ConcreteProductA 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_7A08FFBB_5AD4_4f9c_AE53_998AC5E88F34__INCLUDED_) 9 | #define EA_7A08FFBB_5AD4_4f9c_AE53_998AC5E88F34__INCLUDED_ 10 | 11 | #include "Product.h" 12 | 13 | class ConcreteProductA : public Product 14 | { 15 | 16 | public: 17 | ConcreteProductA(); 18 | virtual ~ConcreteProductA(); 19 | 20 | virtual void Use(); 21 | 22 | }; 23 | #endif // !defined(EA_7A08FFBB_5AD4_4f9c_AE53_998AC5E88F34__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/SimpleFactory/ConcreteProductB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProductB.cpp 3 | // Implementation of the Class ConcreteProductB 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteProductB.h" 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | 14 | ConcreteProductB::ConcreteProductB(){ 15 | 16 | } 17 | 18 | 19 | 20 | ConcreteProductB::~ConcreteProductB(){ 21 | 22 | } 23 | 24 | void ConcreteProductB::Use() 25 | { 26 | cout << "use productB" << endl; 27 | } -------------------------------------------------------------------------------- /code/SimpleFactory/ConcreteProductB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteProductB.h 3 | // Implementation of the Class ConcreteProductB 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_52558B6D_9609_4377_944C_C57B380F7229__INCLUDED_) 9 | #define EA_52558B6D_9609_4377_944C_C57B380F7229__INCLUDED_ 10 | 11 | #include "Product.h" 12 | 13 | class ConcreteProductB : public Product 14 | { 15 | 16 | public: 17 | ConcreteProductB(); 18 | virtual ~ConcreteProductB(); 19 | 20 | virtual void Use(); 21 | 22 | }; 23 | #endif // !defined(EA_52558B6D_9609_4377_944C_C57B380F7229__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/SimpleFactory/Factory.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Factory.cpp 3 | // Implementation of the Class Factory 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Factory.h" 9 | #include "ConcreteProductA.h" 10 | #include "ConcreteProductB.h" 11 | 12 | 13 | Factory::Factory(){ 14 | 15 | } 16 | 17 | 18 | 19 | Factory::~Factory(){ 20 | 21 | } 22 | 23 | 24 | Product* Factory::createProduct(string proname){ 25 | if ( "A" == proname ) 26 | { 27 | return new ConcreteProductA(); 28 | } 29 | else if("B" == proname) 30 | { 31 | return new ConcreteProductB(); 32 | } 33 | return NULL; 34 | } -------------------------------------------------------------------------------- /code/SimpleFactory/Factory.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Factory.h 3 | // Implementation of the Class Factory 4 | // Created on: 01-十月-2014 18:41:33 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_4C08AF19_2960_47a1_B769_9B60CFA50FE0__INCLUDED_) 9 | #define EA_4C08AF19_2960_47a1_B769_9B60CFA50FE0__INCLUDED_ 10 | 11 | #include "Product.h" 12 | #include 13 | using namespace std; 14 | 15 | class Factory 16 | { 17 | 18 | public: 19 | Factory(); 20 | virtual ~Factory(); 21 | 22 | static Product * createProduct(string proname); 23 | 24 | }; 25 | #endif // !defined(EA_4C08AF19_2960_47a1_B769_9B60CFA50FE0__INCLUDED_) 26 | -------------------------------------------------------------------------------- /code/SimpleFactory/Product.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.cpp 3 | // Implementation of the Class Product 4 | // Created on: 01-十月-2014 18:41:34 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Product.h" 9 | 10 | 11 | Product::Product(){ 12 | 13 | } 14 | 15 | 16 | 17 | Product::~Product(){ 18 | 19 | } -------------------------------------------------------------------------------- /code/SimpleFactory/Product.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Product.h 3 | // Implementation of the Class Product 4 | // Created on: 01-十月-2014 18:41:34 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_9126430A_5CDD_4424_AA90_549F255E0D2D__INCLUDED_) 9 | #define EA_9126430A_5CDD_4424_AA90_549F255E0D2D__INCLUDED_ 10 | 11 | class Product 12 | { 13 | 14 | public: 15 | Product(); 16 | virtual ~Product(); 17 | 18 | virtual void Use() = 0; 19 | 20 | }; 21 | #endif // !defined(EA_9126430A_5CDD_4424_AA90_549F255E0D2D__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/SimpleFactory/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/SimpleFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Factory.h" 3 | #include "Product.h" 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char *argv[]) 8 | { 9 | Product * prod = Factory::createProduct("A"); 10 | 11 | prod->Use(); 12 | 13 | delete prod; 14 | 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/ConcreteProductA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/ConcreteProductA.o -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/ConcreteProductB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/ConcreteProductB.o -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/Factory.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/Factory.o -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/Product.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/Product.o -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/main.exe -------------------------------------------------------------------------------- /code/SimpleFactory/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/SimpleFactory/mingw5/main.o -------------------------------------------------------------------------------- /code/Singleton/Singleton.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Singleton.cpp 3 | // Implementation of the Class Singleton 4 | // Created on: 02-十月-2014 17:24:46 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Singleton.h" 9 | #include 10 | using namespace std; 11 | 12 | Singleton * Singleton::instance = NULL; 13 | Singleton::Singleton(){ 14 | 15 | } 16 | 17 | Singleton::~Singleton(){ 18 | delete instance; 19 | } 20 | 21 | Singleton* Singleton::getInstance(){ 22 | if (instance == NULL) 23 | { 24 | instance = new Singleton(); 25 | } 26 | 27 | return instance; 28 | } 29 | 30 | 31 | void Singleton::singletonOperation(){ 32 | cout << "singletonOperation" << endl; 33 | } 34 | 35 | -------------------------------------------------------------------------------- /code/Singleton/Singleton.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Singleton.h 3 | // Implementation of the Class Singleton 4 | // Created on: 02-十月-2014 17:24:46 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_5A12F734_0177_4e67_9117_77C147875E5A__INCLUDED_) 9 | #define EA_5A12F734_0177_4e67_9117_77C147875E5A__INCLUDED_ 10 | 11 | class Singleton 12 | { 13 | 14 | public: 15 | virtual ~Singleton(); 16 | Singleton *m_Singleton; 17 | 18 | static Singleton* getInstance(); 19 | void singletonOperation(); 20 | 21 | private: 22 | static Singleton * instance; 23 | 24 | Singleton(); 25 | 26 | }; 27 | #endif // !defined(EA_5A12F734_0177_4e67_9117_77C147875E5A__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/Singleton/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Singleton/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Singleton.h" 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | Singleton * sg = Singleton::getInstance(); 8 | sg->singletonOperation(); 9 | 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /code/Singleton/mingw5/Singleton.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Singleton/mingw5/Singleton.o -------------------------------------------------------------------------------- /code/Singleton/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Singleton/mingw5/main.exe -------------------------------------------------------------------------------- /code/Singleton/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Singleton/mingw5/main.o -------------------------------------------------------------------------------- /code/State/ConcreteStateA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStateA.cpp 3 | // Implementation of the Class ConcreteStateA 4 | // Created on: 09-十月-2014 17:20:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteStateA.h" 9 | #include "ConcreteStateB.h" 10 | #include "Context.h" 11 | #include 12 | using namespace std; 13 | 14 | State * ConcreteStateA::m_pState = NULL; 15 | ConcreteStateA::ConcreteStateA(){ 16 | } 17 | 18 | ConcreteStateA::~ConcreteStateA(){ 19 | } 20 | 21 | State * ConcreteStateA::Instance() 22 | { 23 | if ( NULL == m_pState) 24 | { 25 | m_pState = new ConcreteStateA(); 26 | } 27 | return m_pState; 28 | } 29 | 30 | void ConcreteStateA::handle(Context * c){ 31 | cout << "doing something in State A.\n done,change state to B" << endl; 32 | c->changeState(ConcreteStateB::Instance()); 33 | } -------------------------------------------------------------------------------- /code/State/ConcreteStateA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStateA.h 3 | // Implementation of the Class ConcreteStateA 4 | // Created on: 09-十月-2014 17:20:58 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_) 9 | #define EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_ 10 | 11 | #include "State.h" 12 | 13 | class ConcreteStateA : public State 14 | { 15 | 16 | public: 17 | virtual ~ConcreteStateA(); 18 | 19 | static State * Instance(); 20 | 21 | virtual void handle(Context * c); 22 | 23 | private: 24 | ConcreteStateA(); 25 | static State * m_pState; 26 | }; 27 | #endif // !defined(EA_84158F08_E96A_4bdb_89A1_4BE2E633C3EE__INCLUDED_) 28 | -------------------------------------------------------------------------------- /code/State/ConcreteStateB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStateB.cpp 3 | // Implementation of the Class ConcreteStateB 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteStateB.h" 9 | #include "ConcreteStateA.h" 10 | #include "Context.h" 11 | #include 12 | using namespace std; 13 | 14 | State * ConcreteStateB::m_pState = NULL; 15 | ConcreteStateB::ConcreteStateB(){ 16 | 17 | } 18 | 19 | State * ConcreteStateB::Instance() 20 | { 21 | if ( NULL == m_pState) 22 | { 23 | m_pState = new ConcreteStateB(); 24 | } 25 | return m_pState; 26 | } 27 | 28 | ConcreteStateB::~ConcreteStateB(){ 29 | 30 | } 31 | 32 | void ConcreteStateB::handle(Context * c){ 33 | cout << "doing something in State B.\n done,change state to A" << endl; 34 | c->changeState(ConcreteStateA::Instance()); 35 | } 36 | 37 | -------------------------------------------------------------------------------- /code/State/ConcreteStateB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStateB.h 3 | // Implementation of the Class ConcreteStateB 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_) 9 | #define EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_ 10 | 11 | #include "State.h" 12 | 13 | class ConcreteStateB : public State 14 | { 15 | 16 | public: 17 | static State * Instance(); 18 | 19 | virtual ~ConcreteStateB(); 20 | 21 | virtual void handle(Context * c); 22 | private: 23 | ConcreteStateB(); 24 | static State * m_pState; 25 | }; 26 | #endif // !defined(EA_A6960661_6D76_42cc_98F8_30BF46FBA4CC__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/State/Context.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Context.cpp 3 | // Implementation of the Class Context 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Context.h" 9 | #include "ConcreteStateA.h" 10 | 11 | Context::Context(){ 12 | //default is a 13 | m_pState = ConcreteStateA::Instance(); 14 | } 15 | 16 | Context::~Context(){ 17 | } 18 | 19 | void Context::changeState(State * st){ 20 | m_pState = st; 21 | } 22 | 23 | void Context::request(){ 24 | m_pState->handle(this); 25 | } -------------------------------------------------------------------------------- /code/State/Context.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Context.h 3 | // Implementation of the Class Context 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_) 9 | #define EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_ 10 | 11 | #include "State.h" 12 | 13 | class Context 14 | { 15 | 16 | public: 17 | Context(); 18 | virtual ~Context(); 19 | 20 | void changeState(State * st); 21 | void request(); 22 | 23 | private: 24 | State *m_pState; 25 | }; 26 | #endif // !defined(EA_F245CF81_2A68_4461_B039_2B901BD5A126__INCLUDED_) 27 | -------------------------------------------------------------------------------- /code/State/State.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // State.cpp 3 | // Implementation of the Class State 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "State.h" 9 | 10 | 11 | State::State(){ 12 | 13 | } 14 | 15 | 16 | 17 | State::~State(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void State::handle(Context * c){ 26 | 27 | } -------------------------------------------------------------------------------- /code/State/State.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // State.h 3 | // Implementation of the Class State 4 | // Created on: 09-十月-2014 17:20:59 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_) 9 | #define EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_ 10 | 11 | class Context; 12 | 13 | class State 14 | { 15 | 16 | public: 17 | State(); 18 | virtual ~State(); 19 | 20 | virtual void handle(Context * c); 21 | 22 | }; 23 | #endif // !defined(EA_8AB26F2E_677E_42cb_98AF_64B7018246A2__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/State/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/State/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Context.h" 3 | #include "ConcreteStateA.h" 4 | #include "ConcreteStateB.h" 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char *argv[]) 9 | { 10 | char a = '0'; 11 | if('0' == a) 12 | cout << "yes" << endl; 13 | else 14 | cout << "no" << endl; 15 | 16 | Context * c = new Context(); 17 | c->request(); 18 | c->request(); 19 | c->request(); 20 | 21 | delete c; 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /code/State/mingw5/ConcreteStateA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/ConcreteStateA.o -------------------------------------------------------------------------------- /code/State/mingw5/ConcreteStateB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/ConcreteStateB.o -------------------------------------------------------------------------------- /code/State/mingw5/Context.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/Context.o -------------------------------------------------------------------------------- /code/State/mingw5/State.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/State.o -------------------------------------------------------------------------------- /code/State/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/main.exe -------------------------------------------------------------------------------- /code/State/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/State/mingw5/main.o -------------------------------------------------------------------------------- /code/Strategy/ConcreteStrategyA.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStrategyA.cpp 3 | // Implementation of the Class ConcreteStrategyA 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteStrategyA.h" 9 | #include 10 | using namespace std; 11 | 12 | ConcreteStrategyA::ConcreteStrategyA(){ 13 | 14 | } 15 | 16 | ConcreteStrategyA::~ConcreteStrategyA(){ 17 | 18 | } 19 | 20 | void ConcreteStrategyA::algorithm(){ 21 | cout << "use algorithm A" << endl; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /code/Strategy/ConcreteStrategyA.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStrategyA.h 3 | // Implementation of the Class ConcreteStrategyA 4 | // Created on: 09-十月-2014 22:21:06 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_9B180F12_677B_4e9b_A243_1F5DAD93FE1D__INCLUDED_) 9 | #define EA_9B180F12_677B_4e9b_A243_1F5DAD93FE1D__INCLUDED_ 10 | 11 | #include "Strategy.h" 12 | 13 | class ConcreteStrategyA : public Strategy 14 | { 15 | 16 | public: 17 | ConcreteStrategyA(); 18 | virtual ~ConcreteStrategyA(); 19 | 20 | virtual void algorithm(); 21 | 22 | }; 23 | #endif // !defined(EA_9B180F12_677B_4e9b_A243_1F5DAD93FE1D__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Strategy/ConcreteStrategyB.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStrategyB.cpp 3 | // Implementation of the Class ConcreteStrategyB 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "ConcreteStrategyB.h" 9 | #include 10 | using namespace std; 11 | 12 | 13 | 14 | ConcreteStrategyB::ConcreteStrategyB(){ 15 | 16 | } 17 | 18 | 19 | 20 | ConcreteStrategyB::~ConcreteStrategyB(){ 21 | 22 | } 23 | 24 | void ConcreteStrategyB::algorithm(){ 25 | cout << "use algorithm B" << endl; 26 | } 27 | 28 | -------------------------------------------------------------------------------- /code/Strategy/ConcreteStrategyB.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // ConcreteStrategyB.h 3 | // Implementation of the Class ConcreteStrategyB 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_10F709BE_1F23_40c5_8872_397ACB1923A4__INCLUDED_) 9 | #define EA_10F709BE_1F23_40c5_8872_397ACB1923A4__INCLUDED_ 10 | 11 | #include "Strategy.h" 12 | 13 | class ConcreteStrategyB : public Strategy 14 | { 15 | 16 | public: 17 | ConcreteStrategyB(); 18 | virtual ~ConcreteStrategyB(); 19 | 20 | virtual void algorithm(); 21 | 22 | }; 23 | #endif // !defined(EA_10F709BE_1F23_40c5_8872_397ACB1923A4__INCLUDED_) 24 | -------------------------------------------------------------------------------- /code/Strategy/Context.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Context.cpp 3 | // Implementation of the Class Context 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Context.h" 9 | 10 | Context::Context(){ 11 | } 12 | 13 | Context::~Context(){ 14 | } 15 | 16 | void Context::algorithm(){ 17 | m_pStrategy->algorithm(); 18 | } 19 | 20 | void Context::setStrategy(Strategy* st){ 21 | m_pStrategy = st; 22 | } 23 | 24 | -------------------------------------------------------------------------------- /code/Strategy/Context.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Context.h 3 | // Implementation of the Class Context 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_0DA87730_4DEE_4392_9BAF_4AC64A8A07A4__INCLUDED_) 9 | #define EA_0DA87730_4DEE_4392_9BAF_4AC64A8A07A4__INCLUDED_ 10 | 11 | #include "Strategy.h" 12 | 13 | class Context 14 | { 15 | 16 | public: 17 | Context(); 18 | virtual ~Context(); 19 | 20 | 21 | void algorithm(); 22 | void setStrategy(Strategy* st); 23 | 24 | private: 25 | Strategy *m_pStrategy; 26 | 27 | }; 28 | #endif // !defined(EA_0DA87730_4DEE_4392_9BAF_4AC64A8A07A4__INCLUDED_) 29 | -------------------------------------------------------------------------------- /code/Strategy/Strategy.cpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Strategy.cpp 3 | // Implementation of the Class Strategy 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #include "Strategy.h" 9 | 10 | 11 | Strategy::Strategy(){ 12 | 13 | } 14 | 15 | 16 | 17 | Strategy::~Strategy(){ 18 | 19 | } 20 | 21 | 22 | 23 | 24 | 25 | void Strategy::algorithm(){ 26 | 27 | } -------------------------------------------------------------------------------- /code/Strategy/Strategy.h: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////// 2 | // Strategy.h 3 | // Implementation of the Class Strategy 4 | // Created on: 09-十月-2014 22:21:07 5 | // Original author: colin 6 | /////////////////////////////////////////////////////////// 7 | 8 | #if !defined(EA_ADAF8456_60CF_4627_A2BE_C1A13D046954__INCLUDED_) 9 | #define EA_ADAF8456_60CF_4627_A2BE_C1A13D046954__INCLUDED_ 10 | 11 | class Strategy 12 | { 13 | 14 | public: 15 | Strategy(); 16 | virtual ~Strategy(); 17 | 18 | virtual void algorithm(); 19 | 20 | }; 21 | #endif // !defined(EA_ADAF8456_60CF_4627_A2BE_C1A13D046954__INCLUDED_) 22 | -------------------------------------------------------------------------------- /code/Strategy/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/Strategy/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "Context.h" 3 | #include "ConcreteStrategyA.h" 4 | #include "ConcreteStrategyB.h" 5 | #include "Strategy.h" 6 | #include 7 | using namespace std; 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | Strategy * s1 = new ConcreteStrategyA(); 12 | Context * cxt = new Context(); 13 | cxt->setStrategy(s1); 14 | cxt->algorithm(); 15 | 16 | Strategy *s2 = new ConcreteStrategyB(); 17 | cxt->setStrategy(s2); 18 | cxt->algorithm(); 19 | 20 | delete s1; 21 | delete s2; 22 | 23 | int rac1 = 0x1; 24 | int rac2 = 0x2; 25 | int rac3 = 0x4; 26 | int rac4 = 0x8; 27 | 28 | int i = 0xe; 29 | int j = 0x5; 30 | 31 | int r1 = i & rac1; 32 | int r2 = i & rac2; 33 | int r3 = i & rac3; 34 | int r4 = i & rac4; 35 | 36 | cout <<"res:" << r1 << "/" << r2 << "/" << r3 << "/" << r4 << endl; 37 | 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /code/Strategy/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/main.o -------------------------------------------------------------------------------- /code/Strategy/mingw5/ConcreteStrategyA.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/ConcreteStrategyA.o -------------------------------------------------------------------------------- /code/Strategy/mingw5/ConcreteStrategyB.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/ConcreteStrategyB.o -------------------------------------------------------------------------------- /code/Strategy/mingw5/Context.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/Context.o -------------------------------------------------------------------------------- /code/Strategy/mingw5/Strategy.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/Strategy.o -------------------------------------------------------------------------------- /code/Strategy/mingw5/main.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/main.exe -------------------------------------------------------------------------------- /code/Strategy/mingw5/main.o: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/code/Strategy/mingw5/main.o -------------------------------------------------------------------------------- /code/main/main.cfpg: -------------------------------------------------------------------------------- 1 | 2 | main.cfp 3 | main.cpp 4 | 5 | -------------------------------------------------------------------------------- /code/main/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main(int argc, char *argv[]) 6 | { 7 | return 0; 8 | } 9 | -------------------------------------------------------------------------------- /creational_patterns/creational.rst: -------------------------------------------------------------------------------- 1 | .. _creational: 2 | 3 | 创建型模式 4 | ==================== 5 | 6 | 创建型模式(Creational Pattern)对类的实例化过程进行了抽象,能够将软件模块中对象的创建和对象的使用分离。为了使软件的结构更加清晰,外界对于这些对象只需要知道它们共同的接口,而不清楚其具体的实现细节,使整个系统的设计更加符合单一职责原则。 7 | 8 | 创建型模式在创建什么(What),由谁创建(Who),何时创建(When)等方面都为软件设计者提供了尽可能大的灵活性。创建型模式隐藏了类的实例的创建细节,通过隐藏对象如何被创建和组合在一起达到使整个系统独立的目的。 9 | 10 | **包含模式** 11 | 12 | - 简单工厂模式(Simple Factory) 13 | 重要程度:4 (5为满分) 14 | - 工厂方法模式(Factory Method) 15 | 重要程度:5 16 | - 抽象工厂模式(Abstract Factory) 17 | 重要程度:5 18 | - 建造者模式(Builder) 19 | 重要程度:2 20 | - 原型模式(Prototype) 21 | 重要程度:3 22 | - 单例模式(Singleton) 23 | 重要程度:4 24 | 25 | 26 | **目录** 27 | 28 | .. toctree:: 29 | :maxdepth: 2 30 | :numbered: 2 31 | 32 | simple_factory 33 | factory_method 34 | abstract_factory 35 | builder 36 | singleton 37 | 38 | 39 | -------------------------------------------------------------------------------- /creational_patterns/simple_factory.rst: -------------------------------------------------------------------------------- 1 | .. _simple_factory: 2 | 3 | 简单工厂模式( Simple Factory Pattern ) 4 | ======================================== 5 | 6 | .. contents:: 目录 7 | 8 | 模式动机 9 | -------------------- 10 | 考虑一个简单的软件应用场景,一个软件系统可以提供多个外观不同的按钮(如圆形按钮、矩形按钮、菱形按钮等), 11 | 这些按钮都源自同一个基类,不过在继承基类后不同的子类修改了部分属性从而使得它们可以呈现不同的外观,如果我们希望在使用这些按钮时,不需要知道这些具体按钮类的名字,只需要知道表示该按钮类的一个参数,并提供一个调用方便的方法,把该参数传入方法即可返回一个相应的按钮对象,此时,就可以使用简单工厂模式。 12 | 13 | 模式定义 14 | -------------------- 15 | 简单工厂模式(Simple Factory Pattern):又称为静态工厂方法(Static Factory Method)模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。 16 | 17 | 18 | 模式结构 19 | -------------------- 20 | 简单工厂模式包含如下角色: 21 | 22 | - Factory:工厂角色 23 | 工厂角色负责实现创建所有实例的内部逻辑 24 | - Product:抽象产品角色 25 | 抽象产品角色是所创建的所有对象的父类,负责描述所有实例所共有的公共接口 26 | - ConcreteProduct:具体产品角色 27 | 具体产品角色是创建目标,所有创建的对象都充当这个角色的某个具体类的实例。 28 | 29 | .. image:: /_static/SimpleFactory.jpg 30 | 31 | 时序图 32 | -------------------- 33 | 34 | .. image:: /_static/seq_SimpleFactory.jpg 35 | 36 | 代码分析 37 | -------------------- 38 | 39 | .. literalinclude:: /code/SimpleFactory/Factory.cpp 40 | :language: cpp 41 | :lines: 1-10,24- 42 | :emphasize-lines: 12-19 43 | :linenos: 44 | 45 | 46 | 模式分析 47 | -------------------- 48 | 49 | - 将对象的创建和对象本身业务处理分离可以降低系统的耦合度,使得两者修改起来都相对容易。 50 | - 在调用工厂类的工厂方法时,由于工厂方法是静态方法,使用起来很方便,可通过类名直接调用,而且只需要传入一个简单的参数即可,在实际开发中,还可以在调用时将所传入的参数保存在XML等格式的配置文件中,修改参数时无须修改任何源代码。 51 | - 简单工厂模式最大的问题在于工厂类的职责相对过重,增加新的产品需要修改工厂类的判断逻辑,这一点与开闭原则是相违背的。 52 | - 简单工厂模式的要点在于:当你需要什么,只需要传入一个正确的参数,就可以获取你所需要的对象,而无须知道其创建细节。 53 | 54 | 实例 55 | -------------------- 56 | (略) 57 | 58 | 59 | 简单工厂模式的优点 60 | -------------------- 61 | 62 | - 工厂类含有必要的判断逻辑,可以决定在什么时候创建哪一个产品类的实例,客户端可以免除直接创建产品对象的责任,而仅仅“消费”产品;简单工厂模式通过这种做法实现了对责任的分割,它提供了专门的工厂类用于创建对象。 63 | - 客户端无须知道所创建的具体产品类的类名,只需要知道具体产品类所对应的参数即可,对于一些复杂的类名,通过简单工厂模式可以减少使用者的记忆量。 64 | - 通过引入配置文件,可以在不修改任何客户端代码的情况下更换和增加新的具体产品类,在一定程度上提高了系统的灵活性。 65 | 66 | 简单工厂模式的缺点 67 | -------------------- 68 | 69 | - 由于工厂类集中了所有产品创建逻辑,一旦不能正常工作,整个系统都要受到影响。 70 | - 使用简单工厂模式将会增加系统中类的个数,在一定程序上增加了系统的复杂度和理解难度。 71 | - 系统扩展困难,一旦添加新产品就不得不修改工厂逻辑,在产品类型较多时,有可能造成工厂逻辑过于复杂,不利于系统的扩展和维护。 72 | - 简单工厂模式由于使用了静态工厂方法,造成工厂角色无法形成基于继承的等级结构。 73 | 74 | 适用环境 75 | -------------------- 76 | 在以下情况下可以使用简单工厂模式: 77 | 78 | - 工厂类负责创建的对象比较少:由于创建的对象较少,不会造成工厂方法中的业务逻辑太过复杂。 79 | - 客户端只知道传入工厂类的参数,对于如何创建对象不关心:客户端既不需要关心创建细节,甚至连类名都不需要记住,只需要知道类型所对应的参数。 80 | 81 | 模式应用 82 | -------------------- 83 | 1. JDK类库中广泛使用了简单工厂模式,如工具类java.text.DateFormat,它用于格式化一个本地日期或者时间。 84 | 85 | :: 86 | 87 | public final static DateFormat getDateInstance(); 88 | public final static DateFormat getDateInstance(int style); 89 | public final static DateFormat getDateInstance(int style,Locale 90 | locale); 91 | 92 | 2. Java加密技术 93 | 94 | 获取不同加密算法的密钥生成器:: 95 | 96 | KeyGenerator keyGen=KeyGenerator.getInstance("DESede"); 97 | 98 | 创建密码器:: 99 | 100 | Cipher cp=Cipher.getInstance("DESede"); 101 | 102 | 总结 103 | -------------------- 104 | 105 | - 创建型模式对类的实例化过程进行了抽象,能够将对象的创建与对象的使用过程分离。 106 | - 简单工厂模式又称为静态工厂方法模式,它属于类创建型模式。在简单工厂模式中,可以根据参数的不同返回不同类的实例。简单工厂模式专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。 107 | - 简单工厂模式包含三个角色:工厂角色负责实现创建所有实例的内部逻辑;抽象产品角色是所创建的所有对象的父类,负责描述所有实例所共有的公共接口;具体产品角色是创建目标,所有创建的对象都充当这个角色的某个具体类的实例。 108 | - 简单工厂模式的要点在于:当你需要什么,只需要传入一个正确的参数,就可以获取你所需要的对象,而无须知道其创建细节。 109 | - 简单工厂模式最大的优点在于实现对象的创建和对象的使用分离,将对象的创建交给专门的工厂类负责,但是其最大的缺点在于工厂类不够灵活,增加新的具体产品需要修改工厂类的判断逻辑代码,而且产品较多时,工厂方法代码将会非常复杂。 110 | - 简单工厂模式适用情况包括:工厂类负责创建的对象比较少;客户端只知道传入工厂类的参数,对于如何创建对象不关心。 111 | 112 | 113 | -------------------------------------------------------------------------------- /creational_patterns/singleton.rst: -------------------------------------------------------------------------------- 1 | .. _singleton: 2 | 3 | 单例模式 4 | ==================== 5 | 6 | .. contents:: 目录 7 | 8 | 模式动机 9 | -------------------- 10 | 对于系统中的某些类来说,只有一个实例很重要,例如,一个系统中可以存在多个打印任务,但是只能有一个正在工作的任务;一个系统只能有一个窗口管理器或文件系统;一个系统只能有一个计时工具或ID(序号)生成器。 11 | 12 | 如何保证一个类只有一个实例并且这个实例易于被访问呢?定义一个全局变量可以确保对象随时都可以被访问,但不能防止我们实例化多个对象。 13 | 14 | 一个更好的解决办法是让类自身负责保存它的唯一实例。这个类可以保证没有其他实例被创建,并且它可以提供一个访问该实例的方法。这就是单例模式的模式动机。 15 | 16 | 17 | 模式定义 18 | -------------------- 19 | 单例模式(Singleton Pattern):单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法。 20 | 21 | 单例模式的要点有三个:一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。单例模式是一种对象创建型模式。单例模式又名单件模式或单态模式。 22 | 23 | 24 | 模式结构 25 | -------------------- 26 | 单例模式包含如下角色: 27 | 28 | - Singleton:单例 29 | 30 | .. image:: /_static/Singleton.jpg 31 | 32 | 33 | 时序图 34 | -------------------- 35 | .. image:: /_static/seq_Singleton.jpg 36 | 37 | 代码分析 38 | -------------------- 39 | .. literalinclude:: /code/Singleton/main.cpp 40 | :language: cpp 41 | :linenos: 42 | :emphasize-lines: 7-8 43 | 44 | .. literalinclude:: /code/Singleton/Singleton.cpp 45 | :language: cpp 46 | :linenos: 47 | :emphasize-lines: 21-28 48 | 49 | 运行结果: 50 | 51 | .. image:: /_static/Singleton_run.jpg 52 | 53 | 模式分析 54 | -------------------- 55 | 单例模式的目的是保证一个类仅有一个实例,并提供一个访问它的全局访问点。单例模式包含的角色只有一个,就是单例类——Singleton。单例类拥有一个私有构造函数,确保用户无法通过new关键字直接实例化它。除此之外,该模式中包含一个静态私有成员变量与静态公有的工厂方法,该工厂方法负责检验实例的存在性并实例化自己,然后存储在静态成员变量中,以确保只有一个实例被创建。 56 | 57 | 在单例模式的实现过程中,需要注意如下三点: 58 | 59 | - 单例类的构造函数为私有; 60 | - 提供一个自身的静态私有成员变量; 61 | - 提供一个公有的静态工厂方法。 62 | 63 | 64 | 实例 65 | -------------------- 66 | 在操作系统中,打印池(Print Spooler)是一个用于管理打印任务的应用程序,通过打印池用户可以删除、中止或者改变打印任务的优先级,在一个系统中只允许运行一个打印池对象,如果重复创建打印池则抛出异常。现使用单例模式来模拟实现打印池的设计。 67 | 68 | 69 | 优点 70 | -------------------- 71 | - 提供了对唯一实例的受控访问。因为单例类封装了它的唯一实例,所以它可以严格控制客户怎样以及何时访问它,并为设计及开发团队提供了共享的概念。 72 | - 由于在系统内存中只存在一个对象,因此可以节约系统资源,对于一些需要频繁创建和销毁的对象,单例模式无疑可以提高系统的性能。 73 | - 允许可变数目的实例。我们可以基于单例模式进行扩展,使用与单例控制相似的方法来获得指定个数的对象实例。 74 | 75 | 76 | 缺点 77 | -------------------- 78 | - 由于单例模式中没有抽象层,因此单例类的扩展有很大的困难。 79 | - 单例类的职责过重,在一定程度上违背了“单一职责原则”。因为单例类既充当了工厂角色,提供了工厂方法,同时又充当了产品角色,包含一些业务方法,将产品的创建和产品的本身的功能融合到一起。 80 | - 滥用单例将带来一些负面问题,如为了节省资源将数据库连接池对象设计为单例类,可能会导致共享连接池对象的程序过多而出现连接池溢出;现在很多面向对象语言(如Java、C#)的运行环境都提供了自动垃圾回收的技术,因此,如果实例化的对象长时间不被利用,系统会认为它是垃圾,会自动销毁并回收资源,下次利用时又将重新实例化,这将导致对象状态的丢失。 81 | 82 | 83 | 适用环境 84 | -------------------- 85 | 在以下情况下可以使用单例模式: 86 | 87 | - 系统只需要一个实例对象,如系统要求提供一个唯一的序列号生成器,或者需要考虑资源消耗太大而只允许创建一个对象。 88 | - 客户调用类的单个实例只允许使用一个公共访问点,除了该公共访问点,不能通过其他途径访问该实例。 89 | - 在一个系统中要求一个类只有一个实例时才应当使用单例模式。反过来,如果一个类可以有几个实例共存,就需要对单例模式进行改进,使之成为多例模式 90 | 91 | 92 | 模式应用 93 | -------------------- 94 | 一个具有自动编号主键的表可以有多个用户同时使用,但数据库中只能有一个地方分配下一个主键编号,否则会出现主键重复,因此该主键编号生成器必须具备唯一性,可以通过单例模式来实现。 95 | 96 | 97 | 模式扩展 98 | -------------------- 99 | 100 | 总结 101 | -------------------- 102 | - 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法。单例模式的要点有三个:一是某个类只能有一个实例;二是它必须自行创建这个实例;三是它必须自行向整个系统提供这个实例。单例模式是一种对象创建型模式。 103 | - 单例模式只包含一个单例角色:在单例类的内部实现只生成一个实例,同时它提供一个静态的工厂方法,让客户可以使用它的唯一实例;为了防止在外部对其实例化,将其构造函数设计为私有。 104 | - 单例模式的目的是保证一个类仅有一个实例,并提供一个访问它的全局访问点。单例类拥有一个私有构造函数,确保用户无法通过new关键字直接实例化它。除此之外,该模式中包含一个静态私有成员变量与静态公有的工厂方法。该工厂方法负责检验实例的存在性并实例化自己,然后存储在静态成员变量中,以确保只有一个实例被创建。 105 | - 单例模式的主要优点在于提供了对唯一实例的受控访问并可以节约系统资源;其主要缺点在于因为缺少抽象层而难以扩展,且单例类职责过重。 106 | - 单例模式适用情况包括:系统只需要一个实例对象;客户调用类的单个实例只允许使用一个公共访问点。 107 | 108 | 109 | 110 | -------------------------------------------------------------------------------- /design_patterns.EAP: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/design_patterns.EAP -------------------------------------------------------------------------------- /dp_tmp.txt: -------------------------------------------------------------------------------- 1 | 模式动机 2 | -------------------- 3 | 4 | 模式定义 5 | -------------------- 6 | 7 | 模式结构 8 | -------------------- 9 | 包含如下角色: 10 | 11 | .. image:: /_static/SimpleFactory.jpg 12 | 13 | 14 | 时序图 15 | -------------------- 16 | .. image:: /_static/seq_SimpleFactory.jpg 17 | 18 | 代码分析 19 | -------------------- 20 | .. literalinclude:: /code/SimpleFactory/Factory.cpp 21 | :language: cpp 22 | :linenos: 23 | :lines: 1-10,24- 24 | :emphasize-lines: 12-19 25 | 26 | 27 | 28 | 模式分析 29 | -------------------- 30 | 31 | 实例 32 | -------------------- 33 | 34 | 优点 35 | -------------------- 36 | 37 | 缺点 38 | -------------------- 39 | 40 | 适用环境 41 | -------------------- 42 | 43 | 模式应用 44 | -------------------- 45 | 46 | 模式扩展 47 | -------------------- 48 | 49 | 总结 50 | -------------------- -------------------------------------------------------------------------------- /index.rst: -------------------------------------------------------------------------------- 1 | .. _index: 2 | 3 | 图说设计模式 4 | ============== 5 | 6 | 软件模式是将模式的一般概念应用于软件开发领域,即软件开发的 7 | 总体指导思路或参照样板。软件模式并非仅限于设计模式,还包括 8 | 架构模式、分析模式和过程模式等,实际上,在软件生存期的每一 9 | 个阶段都存在着一些被认同的模式。 10 | 11 | 本书使用图形和代码结合的方式来解析设计模式; 12 | 13 | 每个模式都有相应的对象结构图,同时为了展示对象间的交互细节, 我会用到时序图来介绍其如何运行;(在状态模式中, 还会用到状态图,这种图的使用对于理解状态的转换非常直观) 14 | 15 | 为了让大家能读懂UML图,在最前面会有一篇文章来介绍UML图形符号; 16 | 17 | 18 | 在系统的学习设计模式之后,我们需要达到3个层次: 19 | 20 | 1. 能在白纸上画出所有的模式结构和时序图; 21 | 22 | 2. 能用代码实现;如果模式的代码都没有实现过,是用不出来的;即所谓,看得懂,不会用; 23 | 24 | 3. 灵活应用到工作中的项目中; 25 | 26 | 27 | 28 | .. toctree:: 29 | :maxdepth: 2 30 | 31 | read_uml 32 | creational_patterns/creational 33 | structural_patterns/structural 34 | behavioral_patterns/behavioral 35 | 36 | -------------------- 37 | 38 | 39 | Indices and tables 40 | ================== 41 | 42 | * :ref:`genindex` 43 | * :ref:`search` 44 | 45 | -------------------------------------------------------------------------------- /read_uml.rst: -------------------------------------------------------------------------------- 1 | .. _read_uml: 2 | 3 | 看懂UML类图和时序图 4 | ==================== 5 | 这里不会将UML的各种元素都提到,我只想讲讲类图中各个类之间的关系; 6 | 能看懂类图中各个类之间的线条、箭头代表什么意思后,也就足够应对 7 | 日常的工作和交流; 8 | 同时,我们应该能将类图所表达的含义和最终的代码对应起来; 9 | 有了这些知识,看后面章节的设计模式结构图就没有什么问题了; 10 | 11 | 本章所有图形使用Enterprise Architect 9.2来画,所有示例详见根目录下的design_patterns.EAP 12 | 13 | 从一个示例开始 14 | -------------------- 15 | 16 | 请看以下这个类图,类之间的关系是我们需要关注的: 17 | 18 | .. image:: /_static/uml_class_struct.jpg 19 | 20 | - 车的类图结构为<>,表示车是一个抽象类; 21 | - 它有两个继承类:小汽车和自行车;它们之间的关系为实现关系,使用带空心箭头的虚线表示; 22 | - 小汽车为与SUV之间也是继承关系,它们之间的关系为泛化关系,使用带空心箭头的实线表示; 23 | - 小汽车与发动机之间是组合关系,使用带实心箭头的实线表示; 24 | - 学生与班级之间是聚合关系,使用带空心箭头的实线表示; 25 | - 学生与身份证之间为关联关系,使用一根实线表示; 26 | - 学生上学需要用到自行车,与自行车是一种依赖关系,使用带箭头的虚线表示; 27 | 28 | 下面我们将介绍这六种关系; 29 | 30 | ---------------------------------------- 31 | 32 | 33 | 34 | 类之间的关系 35 | -------------------- 36 | 泛化关系(generalization) 37 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 38 | 类的继承结构表现在UML中为:泛化(generalize)与实现(realize): 39 | 40 | 继承关系为 is-a的关系;两个对象之间如果可以用 is-a 来表示,就是继承关系:(..是..) 41 | 42 | eg:自行车是车、猫是动物 43 | 44 | 泛化关系用一条带空心箭头的直接表示;如下图表示(A继承自B); 45 | 46 | .. image:: /_static/uml_generalization.jpg 47 | 48 | eg:汽车在现实中有实现,可用汽车定义具体的对象;汽车与SUV之间为泛化关系; 49 | 50 | .. image:: /_static/uml_generalize.jpg 51 | 52 | 注:最终代码中,泛化关系表现为继承非抽象类; 53 | 54 | 实现关系(realize) 55 | ^^^^^^^^^^^^^^^^^^^^ 56 | 实现关系用一条带空心箭头的虚线表示; 57 | 58 | eg:"车"为一个抽象概念,在现实中并无法直接用来定义对象;只有指明具体的子类(汽车还是自行车),才 59 | 可以用来定义对象("车"这个类在C++中用抽象类表示,在JAVA中有接口这个概念,更容易理解) 60 | 61 | .. image:: /_static/uml_realize.jpg 62 | 63 | 注:最终代码中,实现关系表现为继承抽象类; 64 | 65 | 聚合关系(aggregation) 66 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 67 | 聚合关系用一条带空心菱形箭头的直线表示,如下图表示A聚合到B上,或者说B由A组成; 68 | 69 | .. image:: /_static/uml_aggregation.jpg 70 | 71 | 聚合关系用于表示实体对象之间的关系,表示整体由部分构成的语义;例如一个部门由多个员工组成; 72 | 73 | 与组合关系不同的是,整体和部分不是强依赖的,即使整体不存在了,部分仍然存在;例如, 74 | 部门撤销了,人员不会消失,他们依然存在; 75 | 76 | 组合关系(composition) 77 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 78 | 组合关系用一条带实心菱形箭头直线表示,如下图表示A组成B,或者B由A组成; 79 | 80 | .. image:: /_static/uml_composition.jpg 81 | 82 | 与聚合关系一样,组合关系同样表示整体由部分构成的语义;比如公司由多个部门组成; 83 | 84 | 但组合关系是一种强依赖的特殊聚合关系,如果整体不存在了,则部分也不存在了;例如, 85 | 公司不存在了,部门也将不存在了; 86 | 87 | 关联关系(association) 88 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 89 | 关联关系是用一条直线表示的;它描述不同类的对象之间的结构关系;它是一种静态关系, 90 | 通常与运行状态无关,一般由常识等因素决定的;它一般用来定义对象之间静态的、天然的结构; 91 | 所以,关联关系是一种“强关联”的关系; 92 | 93 | 比如,乘车人和车票之间就是一种关联关系;学生和学校就是一种关联关系; 94 | 95 | 关联关系默认不强调方向,表示对象间相互知道;如果特别强调方向,如下图,表示A知道B,但 96 | B不知道A; 97 | 98 | .. image:: /_static/uml_association.jpg 99 | 100 | 注:在最终代码中,关联对象通常是以成员变量的形式实现的; 101 | 102 | 依赖关系(dependency) 103 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 104 | 依赖关系是用一套带箭头的虚线表示的;如下图表示A依赖于B;他描述一个对象在运行期间会用到另一个对象的关系; 105 | 106 | .. image:: /_static/uml_dependency.jpg 107 | 108 | 与关联关系不同的是,它是一种临时性的关系,通常在运行期间产生,并且随着运行时的变化; 109 | 依赖关系也可能发生变化; 110 | 111 | 显然,依赖也有方向,双向依赖是一种非常糟糕的结构,我们总是应该保持单向依赖,杜绝双向依赖的产生; 112 | 113 | 注:在最终代码中,依赖关系体现为类构造方法及类方法的传入参数,箭头的指向为调用关系;依赖关系除了临时知道对方外,还是“使用”对方的方法和属性; 114 | 115 | 时序图 116 | -------------------- 117 | 为了展示对象之间的交互细节,后续对设计模式解析的章节,都会用到时序图; 118 | 119 | 时序图(Sequence Diagram)是显示对象之间交互的图,这些对象是按时间顺序排列的。时序图中显示的是参与交互的对象及其对象之间消息交互的顺序。 120 | 121 | 时序图包括的建模元素主要有:对象(Actor)、生命线(Lifeline)、控制焦点(Focus of control)、消息(Message)等等。 122 | 123 | 关于时序图,以下这篇文章将概念介绍的比较详细;更多实例应用,参见后续章节模式中的时序图; 124 | 125 | http://smartlife.blog.51cto.com/1146871/284874 126 | 127 | 128 | 附录 129 | -------------------- 130 | 在EA中定义一个抽象类(其版型为《abstract》) 131 | 132 | .. image:: /_static/uml_AbatractClass.jpg 133 | 134 | -------------------------------------------------------------------------------- /readme.rst: -------------------------------------------------------------------------------- 1 | .. _readme: 2 | 3 | 图说设计模式 4 | =============== 5 | 6 | 本书使用图形和代码结合的方式来解析设计模式; 7 | 8 | 每个模式都有相应的对象结构图,同时为了展示对象间的交互细节, 9 | 我会用到时序图来介绍其如何运行;(在状态模式中, 10 | 还会用到状态图,这种图的使用对于理解状态的转换非常直观) 11 | 12 | 为了让大家能读懂UML图,在最前面会有一篇文章来介绍UML图形符号(看到UML类图和时序图); 13 | 14 | 15 | 16 | 在系统的学习设计模式之后,我们需要达到3个层次: 17 | 18 | 1. 能在白纸上画出所有的模式结构和时序图; 19 | 20 | 2. 能用代码实现;如果模式的代码都没有实现过,是用不出来的;即所谓,看得懂,不会用; 21 | 22 | 3. 灵活应用到工作中的项目中; 23 | 24 | 25 | 目录结构说明 26 | -------------------- 27 | 28 | 本书使用reStructuredText编写,你应该从index.rst开始; 29 | 30 | 根目录下的design_patterns.EAP为 EA的工程文件,里面包含里书中所有的类图和时序图示例; 31 | 32 | code/:书中所有模式实现的示例源代码(工程使用C-Free组织,在gcc 4.6.2下编译通过); 33 | 34 | 以下三个目录为具体的模式讲解文章; 35 | 36 | creational_patterns/ structural_patterns/ behavioral_patterns/ 37 | 38 | _static/:包含书中用到的所有图片; 39 | 40 | _build/: 使用sphnix生成的html文档目录; 41 | 42 | sphinx_rtd_theme/:为生成html文档所使用的主题; 43 | 44 | 45 | 在线浏览 46 | ==================== 47 | 48 | http://design-patterns.readthedocs.org/zh_CN/latest/index.html 49 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/__init__.py: -------------------------------------------------------------------------------- 1 | """Sphinx ReadTheDocs theme. 2 | 3 | From https://github.com/ryan-roemer/sphinx-bootstrap-theme. 4 | 5 | """ 6 | import os 7 | 8 | VERSION = (0, 1, 5) 9 | 10 | __version__ = ".".join(str(v) for v in VERSION) 11 | __version_full__ = __version__ 12 | 13 | 14 | def get_html_theme_path(): 15 | """Return list of HTML theme paths.""" 16 | cur_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) 17 | return cur_dir 18 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/breadcrumbs.html: -------------------------------------------------------------------------------- 1 |
2 | 18 |
19 |
20 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/footer.html: -------------------------------------------------------------------------------- 1 |
2 | {% if next or prev %} 3 | 11 | {% endif %} 12 | 13 |
14 | 15 |
16 |

17 | {%- if show_copyright %} 18 | {%- if hasdoc('copyright') %} 19 | {% trans path=pathto('copyright'), copyright=copyright|e %}© Copyright {{ copyright }}.{% endtrans %} 20 | {%- else %} 21 | {% trans copyright=copyright|e %}© Copyright {{ copyright }}.{% endtrans %} 22 | {%- endif %} 23 | {%- endif %} 24 | 25 | {%- if last_updated %} 26 | {% trans last_updated=last_updated|e %}Last updated on {{ last_updated }}.{% endtrans %} 27 | {%- endif %} 28 |

29 |
30 | 31 | {% trans %}Sphinx theme provided by Read the Docs{% endtrans %} 32 |
33 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/search.html: -------------------------------------------------------------------------------- 1 | {# 2 | basic/search.html 3 | ~~~~~~~~~~~~~~~~~ 4 | 5 | Template for the search page. 6 | 7 | :copyright: Copyright 2007-2013 by the Sphinx team, see AUTHORS. 8 | :license: BSD, see LICENSE for details. 9 | #} 10 | {%- extends "layout.html" %} 11 | {% set title = _('Search') %} 12 | {% set script_files = script_files + ['_static/searchtools.js'] %} 13 | {% block footer %} 14 | 17 | {# this is used when loading the search index using $.ajax fails, 18 | such as on Chrome for documents on localhost #} 19 | 20 | {{ super() }} 21 | {% endblock %} 22 | {% block body %} 23 | 31 | 32 | {% if search_performed %} 33 |

{{ _('Search Results') }}

34 | {% if not search_results %} 35 |

{{ _('Your search did not match any documents. Please make sure that all words are spelled correctly and that you\'ve selected enough categories.') }}

36 | {% endif %} 37 | {% endif %} 38 |
39 | {% if search_results %} 40 |
    41 | {% for href, caption, context in search_results %} 42 |
  • 43 | {{ caption }} 44 |

    {{ context|e }}

    45 |
  • 46 | {% endfor %} 47 |
48 | {% endif %} 49 |
50 | {% endblock %} 51 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/searchbox.html: -------------------------------------------------------------------------------- 1 |
2 |
3 | 4 | 5 | 6 |
7 |
8 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/css/badge_only.css: -------------------------------------------------------------------------------- 1 | .fa:before{-webkit-font-smoothing:antialiased}.clearfix{*zoom:1}.clearfix:before,.clearfix:after{display:table;content:""}.clearfix:after{clear:both}@font-face{font-family:FontAwesome;font-weight:normal;font-style:normal;src:url("../font/fontawesome_webfont.eot");src:url("../font/fontawesome_webfont.eot?#iefix") format("embedded-opentype"),url("../font/fontawesome_webfont.woff") format("woff"),url("../font/fontawesome_webfont.ttf") format("truetype"),url("../font/fontawesome_webfont.svg#FontAwesome") format("svg")}.fa:before{display:inline-block;font-family:FontAwesome;font-style:normal;font-weight:normal;line-height:1;text-decoration:inherit}a .fa{display:inline-block;text-decoration:inherit}li .fa{display:inline-block}li .fa-large:before,li .fa-large:before{width:1.875em}ul.fas{list-style-type:none;margin-left:2em;text-indent:-0.8em}ul.fas li .fa{width:0.8em}ul.fas li .fa-large:before,ul.fas li .fa-large:before{vertical-align:baseline}.fa-book:before{content:""}.icon-book:before{content:""}.fa-caret-down:before{content:""}.icon-caret-down:before{content:""}.fa-caret-up:before{content:""}.icon-caret-up:before{content:""}.fa-caret-left:before{content:""}.icon-caret-left:before{content:""}.fa-caret-right:before{content:""}.icon-caret-right:before{content:""}.rst-versions{position:fixed;bottom:0;left:0;width:300px;color:#fcfcfc;background:#1f1d1d;border-top:solid 10px #343131;font-family:"Lato","proxima-nova","Helvetica Neue",Arial,sans-serif;z-index:400}.rst-versions a{color:#2980B9;text-decoration:none}.rst-versions .rst-badge-small{display:none}.rst-versions .rst-current-version{padding:12px;background-color:#272525;display:block;text-align:right;font-size:90%;cursor:pointer;color:#27AE60;*zoom:1}.rst-versions .rst-current-version:before,.rst-versions .rst-current-version:after{display:table;content:""}.rst-versions .rst-current-version:after{clear:both}.rst-versions .rst-current-version .fa{color:#fcfcfc}.rst-versions .rst-current-version .fa-book{float:left}.rst-versions .rst-current-version .icon-book{float:left}.rst-versions .rst-current-version.rst-out-of-date{background-color:#E74C3C;color:#fff}.rst-versions .rst-current-version.rst-active-old-version{background-color:#F1C40F;color:#000}.rst-versions.shift-up .rst-other-versions{display:block}.rst-versions .rst-other-versions{font-size:90%;padding:12px;color:gray;display:none}.rst-versions .rst-other-versions hr{display:block;height:1px;border:0;margin:20px 0;padding:0;border-top:solid 1px #413d3d}.rst-versions .rst-other-versions dd{display:inline-block;margin:0}.rst-versions .rst-other-versions dd a{display:inline-block;padding:6px;color:#fcfcfc}.rst-versions.rst-badge{width:auto;bottom:20px;right:20px;left:auto;border:none;max-width:300px}.rst-versions.rst-badge .icon-book{float:none}.rst-versions.rst-badge .fa-book{float:none}.rst-versions.rst-badge.shift-up .rst-current-version{text-align:right}.rst-versions.rst-badge.shift-up .rst-current-version .fa-book{float:left}.rst-versions.rst-badge.shift-up .rst-current-version .icon-book{float:left}.rst-versions.rst-badge .rst-current-version{width:auto;height:30px;line-height:30px;padding:0 6px;display:block;text-align:center}@media screen and (max-width: 768px){.rst-versions{width:85%;display:none}.rst-versions.shift{display:block}img{width:100%;height:auto}} 2 | /*# sourceMappingURL=badge_only.css.map */ 3 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/css/font_lato_googleapi.css: -------------------------------------------------------------------------------- 1 | @font-face { 2 | font-family: 'Inconsolata'; 3 | font-style: normal; 4 | font-weight: 400; 5 | src: local('Inconsolata'), url(https://fonts.gstatic.com/s/inconsolata/v9/BjAYBlHtW3CJxDcjzrnZCIbN6UDyHWBl620a-IRfuBk.woff) format('woff'); 6 | } 7 | @font-face { 8 | font-family: 'Inconsolata'; 9 | font-style: normal; 10 | font-weight: 700; 11 | src: local('Inconsolata Bold'), local('Inconsolata-Bold'), url(https://fonts.gstatic.com/s/inconsolata/v9/AIed271kqQlcIRSOnQH0yTqR_3kx9_hJXbbyU8S6IN0.woff) format('woff'); 12 | } 13 | @font-face { 14 | font-family: 'Lato'; 15 | font-style: normal; 16 | font-weight: 400; 17 | src: local('Lato Regular'), local('Lato-Regular'), url(https://fonts.gstatic.com/s/lato/v10/9k-RPmcnxYEPm8CNFsH2gg.woff) format('woff'); 18 | } 19 | @font-face { 20 | font-family: 'Lato'; 21 | font-style: normal; 22 | font-weight: 700; 23 | src: local('Lato Bold'), local('Lato-Bold'), url(https://fonts.gstatic.com/s/lato/v10/wkfQbvfT_02e2IWO3yYueQ.woff) format('woff'); 24 | } 25 | @font-face { 26 | font-family: 'Lato'; 27 | font-style: italic; 28 | font-weight: 400; 29 | src: local('Lato Italic'), local('Lato-Italic'), url(https://fonts.gstatic.com/s/lato/v10/oUan5VrEkpzIazlUe5ieaA.woff) format('woff'); 30 | } 31 | @font-face { 32 | font-family: 'Lato'; 33 | font-style: italic; 34 | font-weight: 700; 35 | src: local('Lato Bold Italic'), local('Lato-BoldItalic'), url(https://fonts.gstatic.com/s/lato/v10/HkF_qI1x_noxlxhrhMQYED8E0i7KZn-EPnyo3HZu7kw.woff) format('woff'); 36 | } 37 | @font-face { 38 | font-family: 'Roboto Slab'; 39 | font-style: normal; 40 | font-weight: 400; 41 | src: local('Roboto Slab Regular'), local('RobotoSlab-Regular'), url(https://fonts.gstatic.com/s/robotoslab/v6/y7lebkjgREBJK96VQi37ZobN6UDyHWBl620a-IRfuBk.woff) format('woff'); 42 | } 43 | @font-face { 44 | font-family: 'Roboto Slab'; 45 | font-style: normal; 46 | font-weight: 700; 47 | src: local('Roboto Slab Bold'), local('RobotoSlab-Bold'), url(https://fonts.gstatic.com/s/robotoslab/v6/dazS1PrQQuCxC3iOAJFEJTqR_3kx9_hJXbbyU8S6IN0.woff) format('woff'); 48 | } 49 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/fonts/FontAwesome.otf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/sphinx_rtd_theme/static/fonts/FontAwesome.otf -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/fonts/fontawesome-webfont.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/sphinx_rtd_theme/static/fonts/fontawesome-webfont.eot -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/fonts/fontawesome-webfont.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/sphinx_rtd_theme/static/fonts/fontawesome-webfont.ttf -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/fonts/fontawesome-webfont.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/sphinx_rtd_theme/static/fonts/fontawesome-webfont.woff -------------------------------------------------------------------------------- /sphinx_rtd_theme/static/js/theme.js: -------------------------------------------------------------------------------- 1 | $( document ).ready(function() { 2 | // Shift nav in mobile when clicking the menu. 3 | $(document).on('click', "[data-toggle='wy-nav-top']", function() { 4 | $("[data-toggle='wy-nav-shift']").toggleClass("shift"); 5 | $("[data-toggle='rst-versions']").toggleClass("shift"); 6 | }); 7 | // Close menu when you click a link. 8 | $(document).on('click', ".wy-menu-vertical .current ul li a", function() { 9 | $("[data-toggle='wy-nav-shift']").removeClass("shift"); 10 | $("[data-toggle='rst-versions']").toggleClass("shift"); 11 | }); 12 | $(document).on('click', "[data-toggle='rst-current-version']", function() { 13 | $("[data-toggle='rst-versions']").toggleClass("shift-up"); 14 | }); 15 | // Make tables responsive 16 | $("table.docutils:not(.field-list)").wrap("
"); 17 | }); 18 | 19 | window.SphinxRtdTheme = (function (jquery) { 20 | var stickyNav = (function () { 21 | var navBar, 22 | win, 23 | stickyNavCssClass = 'stickynav', 24 | applyStickNav = function () { 25 | if (navBar.height() <= win.height()) { 26 | navBar.addClass(stickyNavCssClass); 27 | } else { 28 | navBar.removeClass(stickyNavCssClass); 29 | } 30 | }, 31 | enable = function () { 32 | applyStickNav(); 33 | win.on('resize', applyStickNav); 34 | }, 35 | init = function () { 36 | navBar = jquery('nav.wy-nav-side:first'); 37 | win = jquery(window); 38 | }; 39 | jquery(init); 40 | return { 41 | enable : enable 42 | }; 43 | }()); 44 | return { 45 | StickyNav : stickyNav 46 | }; 47 | }($)); 48 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/theme.conf: -------------------------------------------------------------------------------- 1 | [theme] 2 | inherit = basic 3 | stylesheet = css/theme.css 4 | 5 | [options] 6 | typekit_id = hiw1hhg 7 | analytics_id = 8 | sticky_navigation = False 9 | -------------------------------------------------------------------------------- /sphinx_rtd_theme/versions.html: -------------------------------------------------------------------------------- 1 | {% if READTHEDOCS %} 2 | {# Add rst-badge after rst-versions for small badge style. #} 3 |
4 | 5 | Read the Docs 6 | v: {{ current_version }} 7 | 8 | 9 |
10 |
11 |
Versions
12 | {% for slug, url in versions %} 13 |
{{ slug }}
14 | {% endfor %} 15 |
16 |
17 |
Downloads
18 | {% for type, url in downloads %} 19 |
{{ type }}
20 | {% endfor %} 21 |
22 |
23 |
On Read the Docs
24 |
25 | Project Home 26 |
27 |
28 | Builds 29 |
30 |
31 |
32 | Free document hosting provided by Read the Docs. 33 | 34 |
35 |
36 | {% endif %} 37 | 38 | -------------------------------------------------------------------------------- /state_diagram.vsd: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/me115/design_patterns/0cbd7856a83d3ae98f549fc1da715cc1b7ec1bbe/state_diagram.vsd -------------------------------------------------------------------------------- /structural_patterns/facade.rst: -------------------------------------------------------------------------------- 1 | .. _facade: 2 | 3 | 外观模式 4 | ==================== 5 | 6 | .. contents:: 目录 7 | 8 | 模式动机 9 | -------------------- 10 | 11 | 模式定义 12 | -------------------- 13 | 外观模式(Facade Pattern):外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式又称为门面模式,它是一种对象结构型模式。 14 | 15 | 16 | 模式结构 17 | -------------------- 18 | 外观模式包含如下角色: 19 | 20 | - Facade: 外观角色 21 | - SubSystem:子系统角色 22 | 23 | 24 | .. image:: /_static/Facade.jpg 25 | 26 | 27 | 时序图 28 | -------------------- 29 | .. image:: /_static/seq_Facade.jpg 30 | 31 | 代码分析 32 | -------------------- 33 | .. literalinclude:: /code/Facade/main.cpp 34 | :language: cpp 35 | :linenos: 36 | :lines: 1- 37 | :emphasize-lines: 7-8 38 | 39 | .. literalinclude:: /code/Facade/Facade.h 40 | :language: cpp 41 | :linenos: 42 | :lines: 1- 43 | 44 | .. literalinclude:: /code/Facade/Facade.cpp 45 | :language: cpp 46 | :linenos: 47 | :lines: 1- 48 | :emphasize-lines: 25-29 49 | 50 | 运行结果: 51 | 52 | .. image:: /_static/Facade_run.jpg 53 | 54 | 55 | 56 | 模式分析 57 | -------------------- 58 | 根据“单一职责原则”,在软件中将一个系统划分为若干个子系统有利于降低整个系统的复杂性,一个常见的设计目标是使子系统间的通信和相互依赖关系达到最小,而达到该目标的途径之一就是引入一个外观对象,它为子系统的访问提供了一个简单而单一的入口。 59 | -外观模式也是“迪米特法则”的体现,通过引入一个新的外观类可以降低原有系统的复杂度,同时降低客户类与子系统类的耦合度。 60 | - 外观模式要求一个子系统的外部与其内部的通信通过一个统一的外观对象进行,外观类将客户端与子系统的内部复杂性分隔开,使得客户端只需要与外观对象打交道,而不需要与子系统内部的很多对象打交道。 61 | -外观模式的目的在于降低系统的复杂程度。 62 | -外观模式从很大程度上提高了客户端使用的便捷性,使得客户端无须关心子系统的工作细节,通过外观角色即可调用相关功能。 63 | 64 | 实例 65 | -------------------- 66 | 67 | 优点 68 | -------------------- 69 | 外观模式的优点 70 | 71 | - 对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易。通过引入外观模式,客户代码将变得很简单,与之关联的对象也很少。 72 | - 实现了子系统与客户之间的松耦合关系,这使得子系统的组件变化不会影响到调用它的客户类,只需要调整外观类即可。 73 | - 降低了大型软件系统中的编译依赖性,并简化了系统在不同平台之间的移植过程,因为编译一个子系统一般不需要编译所有其他的子系统。一个子系统的修改对其他子系统没有任何影响,而且子系统内部变化也不会影响到外观对象。 74 | - 只是提供了一个访问子系统的统一入口,并不影响用户直接使用子系统类。 75 | 76 | 77 | 缺点 78 | -------------------- 79 | 外观模式的缺点 80 | 81 | - 不能很好地限制客户使用子系统类,如果对客户访问子系统类做太多的限制则减少了可变性和灵活性。 82 | - 在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。 83 | 84 | 85 | 适用环境 86 | -------------------- 87 | 在以下情况下可以使用外观模式: 88 | 89 | - 当要为一个复杂子系统提供一个简单接口时可以使用外观模式。该接口可以满足大多数用户的需求,而且用户也可以越过外观类直接访问子系统。 90 | - 客户程序与多个子系统之间存在很大的依赖性。引入外观类将子系统与客户以及其他子系统解耦,可以提高子系统的独立性和可移植性。 91 | - 在层次化结构中,可以使用外观模式定义系统中每一层的入口,层与层之间不直接产生联系,而通过外观类建立联系,降低层之间的耦合度。 92 | 93 | 94 | 模式应用 95 | -------------------- 96 | 97 | 模式扩展 98 | -------------------- 99 | 一个系统有多个外观类 100 | 在外观模式中,通常只需要一个外观类,并且此外观类只有一个实例,换言之它是一个单例类。在很多情况下为了节约系统资源,一般将外观类设计为单例类。当然这并不意味着在整个系统里只能有一个外观类,在一个系统中可以设计多个外观类,每个外观类都负责和一些特定的子系统交互,向用户提供相应的业务功能。 101 | 102 | 不要试图通过外观类为子系统增加新行为 103 | 不要通过继承一个外观类在子系统中加入新的行为,这种做法是错误的。外观模式的用意是为子系统提供一个集中化和简化的沟通渠道,而不是向子系统加入新的行为,新的行为的增加应该通过修改原有子系统类或增加新的子系统类来实现,不能通过外观类来实现。 104 | 105 | 外观模式与迪米特法则 106 | 外观模式创造出一个外观对象,将客户端所涉及的属于一个子系统的协作伙伴的数量减到最少,使得客户端与子系统内部的对象的相互作用被外观对象所取代。外观类充当了客户类与子系统类之间的“第三者”,降低了客户类与子系统类之间的耦合度,外观模式就是实现代码重构以便达到“迪米特法则”要求的一个强有力的武器。 107 | 108 | 抽象外观类的引入 109 | 外观模式最大的缺点在于违背了“开闭原则”,当增加新的子系统或者移除子系统时需要修改外观类,可以通过引入抽象外观类在一定程度上解决该问题,客户端针对抽象外观类进行编程。对于新的业务需求,不修改原有外观类,而对应增加一个新的具体外观类,由新的具体外观类来关联新的子系统对象,同时通过修改配置文件来达到不修改源代码并更换外观类的目的。 110 | 111 | 总结 112 | -------------------- 113 | - 在外观模式中,外部与一个子系统的通信必须通过一个统一的外观对象进行,为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。外观模式又称为门面模式,它是一种对象结构型模式。 114 | - 外观模式包含两个角色:外观角色是在客户端直接调用的角色,在外观角色中可以知道相关的(一个或者多个)子系统的功能和责任,它将所有从客户端发来的请求委派到相应的子系统去,传递给相应的子系统对象处理;在软件系统中可以同时有一个或者多个子系统角色,每一个子系统可以不是一个单独的类,而是一个类的集合,它实现子系统的功能。 115 | - 外观模式要求一个子系统的外部与其内部的通信通过一个统一的外观对象进行,外观类将客户端与子系统的内部复杂性分隔开,使得客户端只需要与外观对象打交道,而不需要与子系统内部的很多对象打交道。 116 | - 外观模式主要优点在于对客户屏蔽子系统组件,减少了客户处理的对象数目并使得子系统使用起来更加容易,它实现了子系统与客户之间的松耦合关系,并降低了大型软件系统中的编译依赖性,简化了系统在不同平台之间的移植过程;其缺点在于不能很好地限制客户使用子系统类,而且在不引入抽象外观类的情况下,增加新的子系统可能需要修改外观类或客户端的源代码,违背了“开闭原则”。 117 | - 外观模式适用情况包括:要为一个复杂子系统提供一个简单接口;客户程序与多个子系统之间存在很大的依赖性;在层次化结构中,需要定义系统中每一层的入口,使得层与层之间不直接产生联系。 118 | -------------------------------------------------------------------------------- /structural_patterns/flyweight.rst: -------------------------------------------------------------------------------- 1 | .. _flyweight: 2 | 3 | 享元模式 4 | ==================== 5 | 6 | .. contents:: 目录 7 | 8 | 模式动机 9 | -------------------- 10 | 面向对象技术可以很好地解决一些灵活性或可扩展性问题,但在很多情况下需要在系统中增加类和对象的个数。当对象数量太多时,将导致运行代价过高,带来性能下降等问题。 11 | 12 | - 享元模式正是为解决这一类问题而诞生的。享元模式通过共享技术实现相同或相似对象的重用。 13 | - 在享元模式中可以共享的相同内容称为内部状态(IntrinsicState),而那些需要外部环境来设置的不能共享的内容称为外部状态(Extrinsic State),由于区分了内部状态和外部状态,因此可以通过设置不同的外部状态使得相同的对象可以具有一些不同的特征,而相同的内部状态是可以共享的。 14 | - 在享元模式中通常会出现工厂模式,需要创建一个享元工厂来负责维护一个享元池(Flyweight Pool)用于存储具有相同内部状态的享元对象。 15 | - 在享元模式中共享的是享元对象的内部状态,外部状态需要通过环境来设置。在实际使用中,能够共享的内部状态是有限的,因此享元对象一般都设计为较小的对象,它所包含的内部状态较少,这种对象也称为细粒度对象。享元模式的目的就是使用共享技术来实现大量细粒度对象的复用。 16 | 17 | 模式定义 18 | -------------------- 19 | 享元模式(Flyweight Pattern):运用共享技术有效地支持大量细粒度对象的复用。系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用。由于享元模式要求能够共享的对象必须是细粒度对象,因此它又称为轻量级模式,它是一种对象结构型模式。 20 | 21 | 模式结构 22 | -------------------- 23 | 享元模式包含如下角色: 24 | 25 | - Flyweight: 抽象享元类 26 | - ConcreteFlyweight: 具体享元类 27 | - UnsharedConcreteFlyweight: 非共享具体享元类 28 | - FlyweightFactory: 享元工厂类 29 | 30 | 31 | .. image:: /_static/Flyweight.jpg 32 | 33 | 34 | 时序图 35 | -------------------- 36 | .. image:: /_static/seq_Flyweight.jpg 37 | 38 | 代码分析 39 | -------------------- 40 | .. literalinclude:: /code/Flyweight/main.cpp 41 | :language: cpp 42 | :linenos: 43 | :lines: 1- 44 | :emphasize-lines: 9-17 45 | 46 | .. literalinclude:: /code/Flyweight/FlyweightFactory.cpp 47 | :language: cpp 48 | :linenos: 49 | :lines: 1- 50 | :emphasize-lines: 23-35 51 | 52 | .. literalinclude:: /code/Flyweight/ConcreteFlyweight.h 53 | :language: cpp 54 | :linenos: 55 | :lines: 1- 56 | 57 | .. literalinclude:: /code/Flyweight/ConcreteFlyweight.cpp 58 | :language: cpp 59 | :linenos: 60 | :lines: 1- 61 | 62 | 运行结果: 63 | 64 | .. image:: /_static/Flyweight_run.jpg 65 | 66 | 模式分析 67 | -------------------- 68 | 享元模式是一个考虑系统性能的设计模式,通过使用享元模式可以节约内存空间,提高系统的性能。 69 | 70 | 享元模式的核心在于享元工厂类,享元工厂类的作用在于提供一个用于存储享元对象的享元池,用户需要对象时,首先从享元池中获取,如果享元池中不存在,则创建一个新的享元对象返回给用户,并在享元池中保存该新增对象。 71 | 72 | 享元模式以共享的方式高效地支持大量的细粒度对象,享元对象能做到共享的关键是区分内部状态(Internal State)和外部状态(External State)。 73 | 74 | - 内部状态是存储在享元对象内部并且不会随环境改变而改变的状态,因此内部状态可以共享。 75 | - 外部状态是随环境改变而改变的、不可以共享的状态。享元对象的外部状态必须由客户端保存,并在享元对象被创建之后,在需要使用的时候再传入到享元对象内部。一个外部状态与另一个外部状态之间是相互独立的。 76 | 77 | 实例 78 | -------------------- 79 | 80 | 优点 81 | -------------------- 82 | 享元模式的优点 83 | 84 | - 享元模式的优点在于它可以极大减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份。 85 | - 享元模式的外部状态相对独立,而且不会影响其内部状态,从而使得享元对象可以在不同的环境中被共享。 86 | 87 | 缺点 88 | -------------------- 89 | 享元模式的缺点 90 | 91 | - 享元模式使得系统更加复杂,需要分离出内部状态和外部状态,这使得程序的逻辑复杂化。 92 | - 为了使对象可以共享,享元模式需要将享元对象的状态外部化,而读取外部状态使得运行时间变长。 93 | 94 | 适用环境 95 | -------------------- 96 | 在以下情况下可以使用享元模式: 97 | 98 | - 一个系统有大量相同或者相似的对象,由于这类对象的大量使用,造成内存的大量耗费。 99 | - 对象的大部分状态都可以外部化,可以将这些外部状态传入对象中。 100 | - 使用享元模式需要维护一个存储享元对象的享元池,而这需要耗费资源,因此,应当在多次重复使用享元对象时才值得使用享元模式。 101 | 102 | 103 | 模式应用 104 | -------------------- 105 | 享元模式在编辑器软件中大量使用,如在一个文档中多次出现相同的图片,则只需要创建一个图片对象,通过在应用程序中设置该图片出现的位置,可以实现该图片在不同地方多次重复显示。 106 | 107 | 模式扩展 108 | -------------------- 109 | 单纯享元模式和复合享元模式 110 | 111 | - 单纯享元模式:在单纯享元模式中,所有的享元对象都是可以共享的,即所有抽象享元类的子类都可共享,不存在非共享具体享元类。 112 | - 复合享元模式:将一些单纯享元使用组合模式加以组合,可以形成复合享元对象,这样的复合享元对象本身不能共享,但是它们可以分解成单纯享元对象,而后者则可以共享。 113 | 114 | 享元模式与其他模式的联用 115 | 116 | - 在享元模式的享元工厂类中通常提供一个静态的工厂方法用于返回享元对象,使用简单工厂模式来生成享元对象。 117 | - 在一个系统中,通常只有唯一一个享元工厂,因此享元工厂类可以使用单例模式进行设计。 118 | - 享元模式可以结合组合模式形成复合享元模式,统一对享元对象设置外部状态。 119 | 120 | 121 | 总结 122 | -------------------- 123 | - 享元模式运用共享技术有效地支持大量细粒度对象的复用。系统只使用少量的对象,而这些对象都很相似,状态变化很小,可以实现对象的多次复用,它是一种对象结构型模式。 124 | - 享元模式包含四个角色:抽象享元类声明一个接口,通过它可以接受并作用于外部状态;具体享元类实现了抽象享元接口,其实例称为享元对象;非共享具体享元是不能被共享的抽象享元类的子类;享元工厂类用于创建并管理享元对象,它针对抽象享元类编程,将各种类型的具体享元对象存储在一个享元池中。 125 | - 享元模式以共享的方式高效地支持大量的细粒度对象,享元对象能做到共享的关键是区分内部状态和外部状态。其中内部状态是存储在享元对象内部并且不会随环境改变而改变的状态,因此内部状态可以共享;外部状态是随环境改变而改变的、不可以共享的状态。 126 | - 享元模式主要优点在于它可以极大减少内存中对象的数量,使得相同对象或相似对象在内存中只保存一份;其缺点是使得系统更加复杂,并且需要将享元对象的状态外部化,而读取外部状态使得运行时间变长。 127 | - 享元模式适用情况包括:一个系统有大量相同或者相似的对象,由于这类对象的大量使用,造成内存的大量耗费;对象的大部分状态都可以外部化,可以将这些外部状态传入对象中;多次重复使用享元对象。 128 | -------------------------------------------------------------------------------- /structural_patterns/structural.rst: -------------------------------------------------------------------------------- 1 | .. _structural: 2 | 3 | 结构型模式 4 | ==================== 5 | 6 | 结构型模式(Structural Pattern)描述如何将类或者对 7 | 象结合在一起形成更大的结构,就像搭积木,可以通过 8 | 简单积木的组合形成复杂的、功能更为强大的结构。 9 | 10 | 结构型模式可以分为类结构型模式和对象结构型模式: 11 | 12 | - 类结构型模式关心类的组合,由多个类可以组合成一个更大的 13 | 系统,在类结构型模式中一般只存在继承关系和实现关系。 14 | - 对象结构型模式关心类与对象的组合,通过关联关系使得在一 15 | 个类中定义另一个类的实例对象,然后通过该对象调用其方法。 16 | 根据“合成复用原则”,在系统中尽量使用关联关系来替代继 17 | 承关系,因此大部分结构型模式都是对象结构型模式。 18 | 19 | **包含模式** 20 | 21 | - 适配器模式(Adapter) 22 | 重要程度:4 23 | - 桥接模式(Bridge) 24 | 重要程度:3 25 | - 组合模式(Composite) 26 | 重要程度:4 27 | - 装饰模式(Decorator) 28 | 重要程度:3 29 | - 外观模式(Facade) 30 | 重要程度:5 31 | - 享元模式(Flyweight) 32 | 重要程度:1 33 | - 代理模式(Proxy) 34 | 重要程度:4 35 | 36 | 37 | **目录** 38 | 39 | .. toctree:: 40 | :maxdepth: 2 41 | :numbered: 2 42 | 43 | adapter 44 | bridge 45 | decorator 46 | facade 47 | flyweight 48 | proxy 49 | --------------------------------------------------------------------------------