├── CMakeLists.txt ├── LICENSE ├── README.adoc ├── 创建型模式 ├── AbstractFactory │ ├── AbstractFactory.cpp │ ├── AbstractFactory.h │ ├── CMakeLists.txt │ ├── Product.cpp │ ├── Product.h │ ├── README │ └── main.cpp ├── Builder │ ├── Builder.cpp │ ├── Builder.h │ ├── CMakeLists.txt │ ├── Director.cpp │ ├── Director.h │ ├── Product.cpp │ ├── Product.h │ ├── README │ └── main.cpp ├── Factory │ ├── CMakeLists.txt │ ├── Factory.cpp │ ├── Factory.h │ ├── Product.cpp │ ├── Product.h │ ├── README │ └── main.cpp ├── Prototype │ ├── CMakeLists.txt │ ├── Prototype.cpp │ ├── Prototype.h │ ├── README │ └── main.cpp └── Singleton │ ├── CMakeLists.txt │ ├── README │ ├── Singleton.cpp │ ├── Singleton.h │ └── main.cpp ├── 结构型模式 ├── Adapter │ ├── CMakeLists.txt │ ├── 对象模式 │ │ ├── Adapter.cpp │ │ ├── Adapter.h │ │ ├── CMakeLists.txt │ │ ├── README │ │ └── main.cpp │ └── 类模式 │ │ ├── Adapter.cpp │ │ ├── Adapter.h │ │ ├── CMakeLists.txt │ │ ├── README │ │ └── main.cpp ├── Bridge │ ├── Abstraction.cpp │ ├── Abstraction.h │ ├── AbstractionImp.cpp │ ├── AbstractionImp.h │ ├── CMakeLists.txt │ ├── README │ └── main.cpp ├── Composite │ ├── CMakeLists.txt │ ├── Component.cpp │ ├── Component.h │ ├── Composite.cpp │ ├── Composite.h │ ├── Leaf.cpp │ ├── Leaf.h │ ├── README │ └── main.cpp ├── Decorator │ ├── CMakeLists.txt │ ├── Decorator.cpp │ ├── Decorator.h │ ├── README │ └── main.cpp ├── Facade │ ├── CMakeLists.txt │ ├── Facade.cpp │ ├── Facade.h │ ├── README │ └── main.cpp ├── Flyweight │ ├── CMakeLists.txt │ ├── Flyweight.cpp │ ├── Flyweight.h │ ├── FlyweightFactory.cpp │ ├── FlyweightFactory.h │ ├── README │ └── main.cpp └── Proxy │ ├── CMakeLists.txt │ ├── Proxy.cpp │ ├── Proxy.h │ ├── README │ └── main.cpp └── 行为模式 ├── ChainofResponsibility ├── CMakeLists.txt ├── Handle.cpp ├── Handle.h ├── README └── main.cpp ├── Command ├── CMakeLists.txt ├── Command.cpp ├── Command.h ├── Invoker.cpp ├── Invoker.h ├── README ├── Reciever.cpp ├── Reciever.h └── main.cpp ├── Interpreter ├── CMakeLists.txt ├── Context.cpp ├── Context.h ├── Interpret.cpp ├── Interpret.h ├── README └── main.cpp ├── Iterator ├── Aggregate.cpp ├── Aggregate.h ├── CMakeLists.txt ├── Iterator.cpp ├── Iterator.h ├── README └── main.cpp ├── Mediator ├── CMakeLists.txt ├── Colleage.cpp ├── Colleage.h ├── Mediator.cpp ├── Mediator.h ├── README └── main.cpp ├── Memento ├── CMakeLists.txt ├── Memento.cpp ├── Memento.h ├── README └── main.cpp ├── Observer ├── CMakeLists.txt ├── Observer.cpp ├── Observer.h ├── README ├── Subject.cpp ├── Subject.h └── main.cpp ├── State ├── CMakeLists.txt ├── Context.cpp ├── Context.h ├── README ├── State.cpp ├── State.h └── main.cpp ├── Strategy ├── CMakeLists.txt ├── Context.cpp ├── Context.h ├── README ├── Strategy.cpp ├── Strategy.h └── main.cpp ├── Template ├── CMakeLists.txt ├── README ├── Template.cpp ├── Template.h └── main.cpp └── Visitor ├── CMakeLists.txt ├── Element.cpp ├── Element.h ├── README ├── Visitor.cpp ├── Visitor.h └── main.cpp /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | CMAKE_MINIMUM_REQUIRED(VERSION 2.6) 2 | 3 | PROJECT(the-23-gang-of-four-design-patterns-with-cplusplus) 4 | 5 | set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) 6 | 7 | ADD_SUBDIRECTORY(创建型模式/AbstractFactory) 8 | ADD_SUBDIRECTORY(创建型模式/Builder) 9 | ADD_SUBDIRECTORY(创建型模式/Factory) 10 | ADD_SUBDIRECTORY(创建型模式/Prototype) 11 | ADD_SUBDIRECTORY(创建型模式/Singleton) 12 | 13 | ADD_SUBDIRECTORY(结构型模式/Proxy) 14 | ADD_SUBDIRECTORY(结构型模式/Flyweight) 15 | ADD_SUBDIRECTORY(结构型模式/Adapter) 16 | ADD_SUBDIRECTORY(结构型模式/Composite) 17 | ADD_SUBDIRECTORY(结构型模式/Bridge) 18 | ADD_SUBDIRECTORY(结构型模式/Facade) 19 | ADD_SUBDIRECTORY(结构型模式/Decorator) 20 | 21 | ADD_SUBDIRECTORY(行为模式/Visitor) 22 | ADD_SUBDIRECTORY(行为模式/Interpreter) 23 | ADD_SUBDIRECTORY(行为模式/Command) 24 | ADD_SUBDIRECTORY(行为模式/Strategy) 25 | ADD_SUBDIRECTORY(行为模式/Observer) 26 | ADD_SUBDIRECTORY(行为模式/State) 27 | ADD_SUBDIRECTORY(行为模式/ChainofResponsibility) 28 | ADD_SUBDIRECTORY(行为模式/Memento) 29 | ADD_SUBDIRECTORY(行为模式/Template) 30 | ADD_SUBDIRECTORY(行为模式/Mediator) 31 | ADD_SUBDIRECTORY(行为模式/Iterator) 32 | 33 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Fifi Lyu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = the-23-gang-of-four-design-patterns-with-cplusplus 2 | 3 | [NOTE] 4 | 源代码整理自 k_eckel 的《设计模式精解-GoF 23 种设计模式解析附 C++实现源码》 5 | 6 | == 用法 7 | 8 | ---- 9 | $ git clone https://github.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus.git 10 | $ mkdir the-23-gang-of-four-design-patterns-with-cplusplus_build 11 | $ cd the-23-gang-of-four-design-patterns-with-cplusplus_build 12 | $ cmake ../the-23-gang-of-four-design-patterns-with-cplusplus 13 | $ make -j 4 14 | $ ls bin 15 | AbstactFactory Bridge Command Facade Interpreter Memento Proxy Strategy 16 | AdapterClass Builder Composite Factory Iterator Observer Singleton Template 17 | AdapterObject ChainofResponsibility Decorator Flyweight Mediator Prototype State Visitor 18 | ---- 19 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/AbstractFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "AbstractFactory.h" 2 | #include "Product.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | AbstractFactory::AbstractFactory() { 8 | } 9 | 10 | AbstractFactory::~AbstractFactory() { 11 | } 12 | 13 | ConcreteFactory1::ConcreteFactory1() { 14 | } 15 | 16 | ConcreteFactory1::~ConcreteFactory1() { 17 | } 18 | 19 | AbstractProductA* ConcreteFactory1::CreateProductA() { 20 | return new ProductA1(); 21 | } 22 | 23 | AbstractProductB* ConcreteFactory1::CreateProductB() { 24 | return new ProductB1(); 25 | } 26 | 27 | ConcreteFactory2::ConcreteFactory2() { 28 | } 29 | 30 | ConcreteFactory2::~ConcreteFactory2() { 31 | } 32 | 33 | AbstractProductA* ConcreteFactory2::CreateProductA() { 34 | return new ProductA2(); 35 | } 36 | AbstractProductB* ConcreteFactory2::CreateProductB() { 37 | return new ProductB2(); 38 | } 39 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/AbstractFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef _ABSTRACTFACTORY_H_ 2 | #define _ABSTRACTFACTORY_H_ 3 | 4 | class AbstractProductA; 5 | 6 | class AbstractProductB; 7 | 8 | class AbstractFactory { 9 | public: 10 | virtual ~AbstractFactory(); 11 | virtual AbstractProductA* CreateProductA() = 0; 12 | virtual AbstractProductB* CreateProductB() = 0; 13 | 14 | protected: 15 | AbstractFactory(); 16 | 17 | private: 18 | }; 19 | 20 | class ConcreteFactory1: public AbstractFactory { 21 | public: 22 | ConcreteFactory1(); 23 | ~ConcreteFactory1(); 24 | AbstractProductA* CreateProductA(); 25 | AbstractProductB* CreateProductB(); 26 | 27 | protected: 28 | 29 | private: 30 | }; 31 | 32 | class ConcreteFactory2: public AbstractFactory { 33 | public: 34 | ConcreteFactory2(); 35 | ~ConcreteFactory2(); 36 | AbstractProductA* CreateProductA(); 37 | AbstractProductB* CreateProductB(); 38 | 39 | protected: 40 | 41 | private: 42 | }; 43 | 44 | #endif //~_ABSTRACTFACTORY_H_ 45 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(AbstactFactory 2 | main.cpp 3 | AbstractFactory.cpp 4 | Product.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/Product.cpp: -------------------------------------------------------------------------------- 1 | #include "Product.h" 2 | #include 3 | using namespace std; 4 | 5 | AbstractProductA::AbstractProductA() { 6 | } 7 | 8 | AbstractProductA::~AbstractProductA() { 9 | } 10 | 11 | AbstractProductB::AbstractProductB() { 12 | } 13 | 14 | AbstractProductB::~AbstractProductB() { 15 | } 16 | 17 | void ProductA1::show() { 18 | cout << "ProductA1...show" << endl; 19 | } 20 | 21 | ProductA1::ProductA1() { 22 | cout << "ProductA1..." << endl; 23 | } 24 | 25 | ProductA1::~ProductA1() { 26 | } 27 | 28 | void ProductA2::show() { 29 | cout << "ProductA2...show" << endl; 30 | } 31 | 32 | 33 | ProductA2::ProductA2() { 34 | cout << "ProductA2..." << endl; 35 | } 36 | 37 | ProductA2::~ProductA2() { 38 | } 39 | 40 | void ProductB1::show() { 41 | cout << "ProductB1...show" << endl; 42 | } 43 | 44 | ProductB1::ProductB1() { 45 | cout << "ProductB1..." << endl; 46 | } 47 | 48 | ProductB1::~ProductB1() { 49 | } 50 | 51 | void ProductB2::show() { 52 | cout << "ProductB2...show" << endl; 53 | } 54 | 55 | ProductB2::ProductB2() { 56 | cout << "ProductB2..." << endl; 57 | } 58 | 59 | ProductB2::~ProductB2() { 60 | } 61 | 62 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/Product.h: -------------------------------------------------------------------------------- 1 | #ifndef _PRODUCT_H_ 2 | #define _PRODUCT_H_ 3 | 4 | class AbstractProductA { 5 | public: 6 | virtual ~AbstractProductA(); 7 | virtual void show() = 0; 8 | 9 | protected: 10 | AbstractProductA(); 11 | private: 12 | }; 13 | 14 | class AbstractProductB { 15 | public: 16 | virtual ~AbstractProductB(); 17 | virtual void show() = 0; 18 | protected: 19 | AbstractProductB(); 20 | 21 | private: 22 | }; 23 | 24 | class ProductA1: public AbstractProductA { 25 | public: 26 | ProductA1(); 27 | ~ProductA1(); 28 | void show(); 29 | protected: 30 | 31 | private: 32 | 33 | }; 34 | 35 | class ProductA2: public AbstractProductA { 36 | public: 37 | ProductA2(); 38 | ~ProductA2(); 39 | void show(); 40 | protected: 41 | 42 | private: 43 | }; 44 | 45 | class ProductB1: public AbstractProductB { 46 | public: 47 | ProductB1(); 48 | ~ProductB1(); 49 | void show(); 50 | protected: 51 | 52 | private: 53 | }; 54 | 55 | class ProductB2: public AbstractProductB { 56 | public: 57 | ProductB2(); 58 | ~ProductB2(); 59 | void show(); 60 | protected: 61 | 62 | private: 63 | }; 64 | 65 | #endif //~_PRODUCT_H_ 66 | -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/创建型模式/AbstractFactory/README -------------------------------------------------------------------------------- /创建型模式/AbstractFactory/main.cpp: -------------------------------------------------------------------------------- 1 | #include "AbstractFactory.h" 2 | #include "Product.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | AbstractFactory* cf1 = new ConcreteFactory1(); 9 | AbstractProductA *a1 = cf1->CreateProductA(); 10 | a1->show(); 11 | AbstractProductB *b1 = cf1->CreateProductB(); 12 | b1->show(); 13 | 14 | AbstractFactory* cf2 = new ConcreteFactory2(); 15 | AbstractProductA *a2 = cf2->CreateProductA(); 16 | a2->show(); 17 | AbstractProductB *b2 = cf2->CreateProductB(); 18 | b2->show(); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /创建型模式/Builder/Builder.cpp: -------------------------------------------------------------------------------- 1 | #include "Builder.h" 2 | #include "Product.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Builder::Builder() { 8 | } 9 | 10 | Builder::~Builder() { 11 | } 12 | 13 | ConcreteBuilder::ConcreteBuilder() { 14 | } 15 | 16 | ConcreteBuilder::~ConcreteBuilder() { 17 | } 18 | 19 | void ConcreteBuilder::BuildPartA(const string& buildPara) { 20 | cout << "Step1:Build PartA..." << buildPara << endl; 21 | } 22 | 23 | void ConcreteBuilder::BuildPartB(const string& buildPara) { 24 | cout << "Step1:Build PartB..." << buildPara << endl; 25 | } 26 | 27 | void ConcreteBuilder::BuildPartC(const string& buildPara) { 28 | cout << "Step1:Build PartC..." << buildPara << endl; 29 | } 30 | 31 | Product* ConcreteBuilder::GetProduct() { 32 | BuildPartA("pre-defined"); 33 | BuildPartB("pre-defined"); 34 | BuildPartC("pre-defined"); 35 | return new Product(); 36 | } 37 | -------------------------------------------------------------------------------- /创建型模式/Builder/Builder.h: -------------------------------------------------------------------------------- 1 | #ifndef _BUILDER_H_ 2 | #define _BUILDER_H_ 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Product; 8 | 9 | class Builder { 10 | public: 11 | virtual ~Builder(); 12 | virtual void BuildPartA(const string& buildPara) = 0; 13 | virtual void BuildPartB(const string& buildPara) = 0; 14 | virtual void BuildPartC(const string& buildPara) = 0; 15 | virtual Product* GetProduct() = 0; 16 | 17 | protected: 18 | Builder(); 19 | 20 | private: 21 | }; 22 | 23 | class ConcreteBuilder: public Builder { 24 | public: 25 | ConcreteBuilder(); 26 | ~ConcreteBuilder(); 27 | void BuildPartA(const string& buildPara); 28 | void BuildPartB(const string& buildPara); 29 | void BuildPartC(const string& buildPara); 30 | Product* GetProduct(); 31 | 32 | protected: 33 | 34 | private: 35 | }; 36 | #endif //~_BUILDER_H_ 37 | -------------------------------------------------------------------------------- /创建型模式/Builder/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Builder 2 | main.cpp 3 | Builder.cpp 4 | Director.cpp 5 | Product.cpp 6 | ) 7 | -------------------------------------------------------------------------------- /创建型模式/Builder/Director.cpp: -------------------------------------------------------------------------------- 1 | #include "Director.h" 2 | #include "Builder.h" 3 | 4 | Director::Director(Builder* bld) { 5 | _bld = bld; 6 | } 7 | 8 | Director::~Director() { 9 | } 10 | 11 | void Director::Construct() { 12 | _bld->BuildPartA("user-defined"); 13 | _bld->BuildPartB("user-defined"); 14 | _bld->BuildPartC("user-defined"); 15 | } 16 | -------------------------------------------------------------------------------- /创建型模式/Builder/Director.h: -------------------------------------------------------------------------------- 1 | #ifndef _DIRECTOR_H_ 2 | #define _DIRECTOR_H_ 3 | 4 | class Builder; 5 | 6 | class Director { 7 | public: 8 | Director(Builder* bld); 9 | ~Director(); 10 | void Construct(); 11 | 12 | protected: 13 | 14 | private: 15 | Builder* _bld; 16 | }; 17 | 18 | #endif //~_DIRECTOR_H_ 19 | -------------------------------------------------------------------------------- /创建型模式/Builder/Product.cpp: -------------------------------------------------------------------------------- 1 | #include "Product.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Product::Product() { 7 | ProducePart(); 8 | cout << "return a product" << endl; 9 | } 10 | 11 | Product::~Product() { 12 | } 13 | 14 | void Product::ProducePart() { 15 | cout << "build part of product.." << endl; 16 | } 17 | 18 | ProductPart::ProductPart() { 19 | //cout<<"build productpart.."< 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char* argv[]) { 9 | Director* d = new Director(new ConcreteBuilder()); 10 | d->Construct(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /创建型模式/Factory/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Factory 2 | main.cpp 3 | Factory.cpp 4 | Product.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /创建型模式/Factory/Factory.cpp: -------------------------------------------------------------------------------- 1 | #include "Factory.h" 2 | #include "Product.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Factory::Factory() { 8 | } 9 | 10 | Factory::~Factory() { 11 | } 12 | 13 | ConcreteFactory::ConcreteFactory() { 14 | cout << "ConcreteFactory....." << endl; 15 | } 16 | 17 | ConcreteFactory::~ConcreteFactory() { 18 | } 19 | 20 | Product* ConcreteFactory::CreateProduct() { 21 | return new ConcreteProduct(); 22 | } 23 | -------------------------------------------------------------------------------- /创建型模式/Factory/Factory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACTORY_H_ 2 | #define _FACTORY_H_ 3 | 4 | class Product; 5 | 6 | class Factory { 7 | public: 8 | virtual ~Factory() = 0; 9 | virtual Product* CreateProduct() = 0; 10 | 11 | protected: 12 | Factory(); 13 | 14 | private: 15 | }; 16 | 17 | class ConcreteFactory: public Factory { 18 | public: 19 | ~ConcreteFactory(); 20 | ConcreteFactory(); 21 | Product* CreateProduct(); 22 | 23 | protected: 24 | 25 | private: 26 | }; 27 | 28 | #endif //~_FACTORY_H_ 29 | -------------------------------------------------------------------------------- /创建型模式/Factory/Product.cpp: -------------------------------------------------------------------------------- 1 | #include "Product.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Product::Product() { 7 | } 8 | 9 | Product::~Product() { 10 | } 11 | 12 | ConcreteProduct::ConcreteProduct() { 13 | cout << "ConcreteProduct...." << endl; 14 | } 15 | 16 | ConcreteProduct::~ConcreteProduct() { 17 | } 18 | -------------------------------------------------------------------------------- /创建型模式/Factory/Product.h: -------------------------------------------------------------------------------- 1 | #ifndef _PRODUCT_H_ 2 | #define _PRODUCT_H_ 3 | 4 | class Product { 5 | public: 6 | virtual ~Product() =0; 7 | 8 | protected: 9 | Product(); 10 | 11 | private: 12 | }; 13 | 14 | class ConcreteProduct: public Product { 15 | public: 16 | ~ConcreteProduct(); 17 | ConcreteProduct(); 18 | 19 | protected: 20 | 21 | private: 22 | }; 23 | 24 | #endif //~_PRODUCT_H_ 25 | -------------------------------------------------------------------------------- /创建型模式/Factory/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/创建型模式/Factory/README -------------------------------------------------------------------------------- /创建型模式/Factory/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Factory.h" 2 | #include "Product.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | Factory* fac = new ConcreteFactory(); 9 | Product* p = fac->CreateProduct(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /创建型模式/Prototype/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Prototype 2 | main.cpp 3 | Prototype.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /创建型模式/Prototype/Prototype.cpp: -------------------------------------------------------------------------------- 1 | #include "Prototype.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Prototype::Prototype() { 7 | } 8 | 9 | Prototype::~Prototype() { 10 | } 11 | 12 | Prototype* Prototype::Clone() const { 13 | return 0; 14 | } 15 | 16 | ConcretePrototype::ConcretePrototype() { 17 | } 18 | 19 | ConcretePrototype::~ConcretePrototype() { 20 | } 21 | 22 | ConcretePrototype::ConcretePrototype(const ConcretePrototype& cp) { 23 | cout << "ConcretePrototype copy ..." << endl; 24 | } 25 | 26 | Prototype* ConcretePrototype::Clone() const { 27 | return new ConcretePrototype(*this); 28 | } 29 | -------------------------------------------------------------------------------- /创建型模式/Prototype/Prototype.h: -------------------------------------------------------------------------------- 1 | #ifndef _PROTOTYPE_H_ 2 | #define _PROTOTYPE_H_ 3 | 4 | class Prototype { 5 | public: 6 | virtual ~Prototype(); 7 | virtual Prototype* Clone() const = 0; 8 | 9 | protected: 10 | Prototype(); 11 | 12 | private: 13 | }; 14 | 15 | class ConcretePrototype: public Prototype { 16 | public: 17 | ConcretePrototype(); 18 | ConcretePrototype(const ConcretePrototype& cp); 19 | ~ConcretePrototype(); 20 | Prototype* Clone() const; 21 | 22 | protected: 23 | 24 | private: 25 | }; 26 | 27 | #endif //~_PROTOTYPE_H_ 28 | -------------------------------------------------------------------------------- /创建型模式/Prototype/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/创建型模式/Prototype/README -------------------------------------------------------------------------------- /创建型模式/Prototype/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Prototype.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Prototype* p = new ConcretePrototype(); 8 | Prototype* p1 = p->Clone(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /创建型模式/Singleton/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Singleton 2 | main.cpp 3 | Singleton.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /创建型模式/Singleton/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/创建型模式/Singleton/README -------------------------------------------------------------------------------- /创建型模式/Singleton/Singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Singleton* Singleton::_instance = 0; 7 | 8 | Singleton::Singleton() { 9 | cout << "Singleton...." << endl; 10 | } 11 | 12 | Singleton* Singleton::Instance() { 13 | if (_instance == 0) { 14 | _instance = new Singleton(); 15 | } 16 | return _instance; 17 | } 18 | -------------------------------------------------------------------------------- /创建型模式/Singleton/Singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef _SINGLETON_H_ 2 | #define _SINGLETON_H_ 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Singleton { 8 | public: 9 | static Singleton* Instance(); 10 | 11 | protected: 12 | Singleton(); 13 | 14 | private: 15 | static Singleton* _instance; 16 | }; 17 | 18 | #endif //~_SINGLETON_H_ 19 | -------------------------------------------------------------------------------- /创建型模式/Singleton/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Singleton.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Singleton* sgn = Singleton::Instance(); 8 | return 0; 9 | } 10 | -------------------------------------------------------------------------------- /结构型模式/Adapter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_SUBDIRECTORY(对象模式) 2 | ADD_SUBDIRECTORY(类模式) 3 | -------------------------------------------------------------------------------- /结构型模式/Adapter/对象模式/Adapter.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | #include 3 | 4 | Target::Target() { 5 | } 6 | 7 | Target::~Target() { 8 | } 9 | 10 | void Target::Request() { 11 | std::cout << "Target::Request" << std::endl; 12 | } 13 | 14 | Adaptee::Adaptee() { 15 | } 16 | 17 | Adaptee::~Adaptee() { 18 | } 19 | 20 | void Adaptee::SpecificRequest() { 21 | std::cout << "Adaptee::SpecificRequest" << std::endl; 22 | } 23 | 24 | Adapter::Adapter(Adaptee* ade) { 25 | this->_ade = ade; 26 | } 27 | 28 | Adapter::~Adapter() { 29 | 30 | } 31 | 32 | void Adapter::Request() { 33 | _ade->SpecificRequest(); 34 | } 35 | -------------------------------------------------------------------------------- /结构型模式/Adapter/对象模式/Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef _ADAPTER_H_ 2 | #define _ADAPTER_H_ 3 | 4 | class Target { 5 | public: 6 | Target(); 7 | virtual ~Target(); 8 | virtual void Request(); 9 | 10 | protected: 11 | 12 | private: 13 | }; 14 | 15 | class Adaptee { 16 | public: 17 | Adaptee(); 18 | ~Adaptee(); 19 | void SpecificRequest(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class Adapter: public Target { 27 | public: 28 | Adapter(Adaptee* ade); 29 | ~Adapter(); 30 | void Request(); 31 | 32 | protected: 33 | 34 | private: 35 | Adaptee* _ade; 36 | }; 37 | 38 | #endif //~_ADAPTER_H_ 39 | -------------------------------------------------------------------------------- /结构型模式/Adapter/对象模式/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(AdapterObject 2 | main.cpp 3 | Adapter.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /结构型模式/Adapter/对象模式/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Adapter/对象模式/README -------------------------------------------------------------------------------- /结构型模式/Adapter/对象模式/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Adaptee* ade = new Adaptee; 8 | Target* adt = new Adapter(ade); 9 | adt->Request(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /结构型模式/Adapter/类模式/Adapter.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | #include 3 | 4 | Target::Target() { 5 | } 6 | 7 | Target::~Target() { 8 | } 9 | 10 | void Target::Request() { 11 | std::cout << "Target::Request" << std::endl; 12 | } 13 | 14 | Adaptee::Adaptee() { 15 | } 16 | 17 | Adaptee::~Adaptee() { 18 | } 19 | 20 | void Adaptee::SpecificRequest() { 21 | std::cout << "Adaptee::SpecificRequest" << std::endl; 22 | } 23 | 24 | Adapter::Adapter() { 25 | } 26 | 27 | Adapter::~Adapter() { 28 | } 29 | 30 | void Adapter::Request() { 31 | this->SpecificRequest(); 32 | } 33 | -------------------------------------------------------------------------------- /结构型模式/Adapter/类模式/Adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef _ADAPTER_H_ 2 | #define _ADAPTER_H_ 3 | 4 | class Target { 5 | public: 6 | Target(); 7 | virtual ~Target(); 8 | virtual void Request(); 9 | 10 | protected: 11 | 12 | private: 13 | }; 14 | 15 | class Adaptee { 16 | public: 17 | Adaptee(); 18 | ~Adaptee(); 19 | void SpecificRequest(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class Adapter: public Target, private Adaptee { 27 | public: 28 | Adapter(); 29 | ~Adapter(); 30 | void Request(); 31 | 32 | protected: 33 | 34 | private: 35 | }; 36 | 37 | #endif //~_ADAPTER_H_ 38 | -------------------------------------------------------------------------------- /结构型模式/Adapter/类模式/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(AdapterClass 2 | main.cpp 3 | Adapter.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /结构型模式/Adapter/类模式/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Adapter/类模式/README -------------------------------------------------------------------------------- /结构型模式/Adapter/类模式/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Adapter.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Target* adt = new Adapter(); 8 | adt->Request(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /结构型模式/Bridge/Abstraction.cpp: -------------------------------------------------------------------------------- 1 | #include "Abstraction.h" 2 | #include "AbstractionImp.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Abstraction::Abstraction() { 8 | } 9 | 10 | Abstraction::~Abstraction() { 11 | } 12 | 13 | RefinedAbstraction::RefinedAbstraction(AbstractionImp* imp) { 14 | _imp = imp; 15 | } 16 | 17 | RefinedAbstraction::~RefinedAbstraction() { 18 | } 19 | 20 | void RefinedAbstraction::Operation() { 21 | _imp->Operation(); 22 | } 23 | -------------------------------------------------------------------------------- /结构型模式/Bridge/Abstraction.h: -------------------------------------------------------------------------------- 1 | #ifndef _ABSTRACTION_H_ 2 | #define _ABSTRACTION_H_ 3 | 4 | class AbstractionImp; 5 | 6 | class Abstraction { 7 | public: 8 | virtual ~Abstraction(); 9 | virtual void Operation() = 0; 10 | 11 | protected: 12 | Abstraction(); 13 | 14 | private: 15 | }; 16 | 17 | class RefinedAbstraction: public Abstraction { 18 | public: 19 | RefinedAbstraction(AbstractionImp* imp); 20 | ~RefinedAbstraction(); 21 | void Operation(); 22 | 23 | protected: 24 | 25 | private: 26 | AbstractionImp* _imp; 27 | }; 28 | 29 | #endif //~_ABSTRACTION_H_ 30 | -------------------------------------------------------------------------------- /结构型模式/Bridge/AbstractionImp.cpp: -------------------------------------------------------------------------------- 1 | #include "AbstractionImp.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | AbstractionImp::AbstractionImp() { 7 | } 8 | 9 | AbstractionImp::~AbstractionImp() { 10 | } 11 | 12 | void AbstractionImp::Operation() { 13 | cout << "AbstractionImp....imp..." << endl; 14 | } 15 | 16 | ConcreteAbstractionImpA::ConcreteAbstractionImpA() { 17 | } 18 | 19 | ConcreteAbstractionImpA::~ConcreteAbstractionImpA() { 20 | } 21 | 22 | void ConcreteAbstractionImpA::Operation() { 23 | cout << "ConcreteAbstractionImpA...." << endl; 24 | } 25 | 26 | ConcreteAbstractionImpB::ConcreteAbstractionImpB() { 27 | } 28 | 29 | ConcreteAbstractionImpB::~ConcreteAbstractionImpB() { 30 | } 31 | 32 | void ConcreteAbstractionImpB::Operation() { 33 | cout << "ConcreteAbstractionImpB...." << endl; 34 | } 35 | -------------------------------------------------------------------------------- /结构型模式/Bridge/AbstractionImp.h: -------------------------------------------------------------------------------- 1 | #ifndef _ABSTRACTIONIMP_H_ 2 | #define _ABSTRACTIONIMP_H_ 3 | 4 | class AbstractionImp { 5 | public: 6 | virtual ~AbstractionImp(); 7 | virtual void Operation() = 0; 8 | 9 | protected: 10 | AbstractionImp(); 11 | 12 | private: 13 | }; 14 | 15 | class ConcreteAbstractionImpA: public AbstractionImp { 16 | public: 17 | ConcreteAbstractionImpA(); 18 | ~ConcreteAbstractionImpA(); 19 | virtual void Operation(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class ConcreteAbstractionImpB: public AbstractionImp { 27 | public: 28 | ConcreteAbstractionImpB(); 29 | ~ConcreteAbstractionImpB(); 30 | virtual void Operation(); 31 | 32 | protected: 33 | 34 | private: 35 | }; 36 | 37 | #endif //~_ABSTRACTIONIMP_H_ 38 | -------------------------------------------------------------------------------- /结构型模式/Bridge/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Bridge 2 | main.cpp 3 | Abstraction.cpp 4 | AbstractionImp.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /结构型模式/Bridge/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Bridge/README -------------------------------------------------------------------------------- /结构型模式/Bridge/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Abstraction.h" 2 | #include "AbstractionImp.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | AbstractionImp* imp = new ConcreteAbstractionImpA(); 9 | Abstraction* abs = new RefinedAbstraction(imp); 10 | abs->Operation(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /结构型模式/Composite/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Composite 2 | main.cpp 3 | Component.cpp 4 | Composite.cpp 5 | Leaf.cpp 6 | ) 7 | -------------------------------------------------------------------------------- /结构型模式/Composite/Component.cpp: -------------------------------------------------------------------------------- 1 | #include "Component.h" 2 | 3 | Component::Component() { 4 | } 5 | 6 | Component::~Component() { 7 | } 8 | 9 | void Component::Add(const Component& com) { 10 | } 11 | 12 | Component* Component::GetChild(int index) { 13 | return 0; 14 | } 15 | 16 | void Component::Remove(const Component& com) { 17 | } 18 | -------------------------------------------------------------------------------- /结构型模式/Composite/Component.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMPONENT_H_ 2 | #define _COMPONENT_H_ 3 | 4 | class Component { 5 | public: 6 | Component(); 7 | virtual ~Component(); 8 | 9 | public: 10 | virtual void Operation() = 0; 11 | virtual void Add(const Component&); 12 | virtual void Remove(const Component&); 13 | virtual Component* GetChild(int); 14 | 15 | protected: 16 | 17 | private: 18 | }; 19 | 20 | #endif //~_COMPONENT_H_ 21 | -------------------------------------------------------------------------------- /结构型模式/Composite/Composite.cpp: -------------------------------------------------------------------------------- 1 | #include "Composite.h" 2 | #include "Component.h" 3 | #include 4 | 5 | Composite::Composite() { 6 | 7 | } 8 | 9 | Composite::~Composite() { 10 | } 11 | 12 | void Composite::Operation() { 13 | vector::iterator comIter = comVec.begin(); 14 | 15 | for (; comIter != comVec.end(); comIter++) { 16 | (*comIter)->Operation(); 17 | } 18 | } 19 | 20 | void Composite::Add(Component* com) { 21 | comVec.push_back(com); 22 | } 23 | 24 | void Composite::Remove(Component* com) { 25 | vector::iterator comIter = find(comVec.begin(), comVec.end(), com); 26 | if ( comIter != comVec.end()) comVec.erase(comIter); 27 | } 28 | 29 | Component* Composite::GetChild(int index) { 30 | return comVec[index]; 31 | } 32 | -------------------------------------------------------------------------------- /结构型模式/Composite/Composite.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMPOSITE_H_ 2 | #define _COMPOSITE_H_ 3 | #include "Component.h" 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Composite: public Component { 9 | public: 10 | Composite(); 11 | ~Composite(); 12 | 13 | public: 14 | void Operation(); 15 | void Add(Component* com); 16 | void Remove(Component* com); 17 | Component* GetChild(int index); 18 | 19 | protected: 20 | 21 | private: 22 | vector comVec; 23 | }; 24 | 25 | #endif //~_COMPOSITE_H_ 26 | -------------------------------------------------------------------------------- /结构型模式/Composite/Leaf.cpp: -------------------------------------------------------------------------------- 1 | #include "Leaf.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Leaf::Leaf() { 7 | } 8 | 9 | Leaf::~Leaf() { 10 | } 11 | 12 | void Leaf::Operation() { 13 | cout << "Leaf operation....." << endl; 14 | } 15 | -------------------------------------------------------------------------------- /结构型模式/Composite/Leaf.h: -------------------------------------------------------------------------------- 1 | #ifndef _LEAF_H_ 2 | #define _LEAF_H_ 3 | 4 | #include "Component.h" 5 | 6 | class Leaf: public Component { 7 | public: 8 | Leaf(); 9 | ~Leaf(); 10 | void Operation(); 11 | 12 | protected: 13 | 14 | private: 15 | }; 16 | 17 | #endif //~_LEAF_H_ 18 | -------------------------------------------------------------------------------- /结构型模式/Composite/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Composite/README -------------------------------------------------------------------------------- /结构型模式/Composite/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Component.h" 2 | #include "Composite.h" 3 | #include "Leaf.h" 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char* argv[]) { 9 | Leaf* l = new Leaf(); 10 | l->Operation(); 11 | Composite* com = new Composite(); 12 | com->Add(l); 13 | com->Operation(); 14 | Component* ll = com->GetChild(0); 15 | ll->Operation(); 16 | 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /结构型模式/Decorator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Decorator 2 | main.cpp 3 | Decorator.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /结构型模式/Decorator/Decorator.cpp: -------------------------------------------------------------------------------- 1 | #include "Decorator.h" 2 | #include 3 | 4 | Component::Component() { 5 | } 6 | 7 | Component::~Component() { 8 | } 9 | 10 | void Component::Operation() { 11 | } 12 | 13 | ConcreteComponent::ConcreteComponent() { 14 | } 15 | 16 | ConcreteComponent::~ConcreteComponent() { 17 | } 18 | 19 | void ConcreteComponent::Operation() { 20 | std::cout << "ConcreteComponent operation..." << std::endl; 21 | } 22 | 23 | Decorator::Decorator(Component* com) { 24 | this->_com = com; 25 | } 26 | 27 | Decorator::~Decorator() { 28 | delete _com; 29 | } 30 | 31 | void Decorator::Operation() { 32 | } 33 | 34 | ConcreteDecorator::ConcreteDecorator(Component*com) : 35 | Decorator(com) { 36 | } 37 | 38 | ConcreteDecorator::~ConcreteDecorator() { 39 | } 40 | 41 | void ConcreteDecorator::AddedBehavior() { 42 | std::cout << "ConcreteDecorator::AddedBehacior...." << std::endl; 43 | } 44 | 45 | void ConcreteDecorator::Operation() { 46 | _com->Operation(); 47 | this->AddedBehavior(); 48 | } 49 | -------------------------------------------------------------------------------- /结构型模式/Decorator/Decorator.h: -------------------------------------------------------------------------------- 1 | #ifndef _DECORATOR_H_ 2 | #define _DECORATOR_H_ 3 | 4 | class Component { 5 | public: 6 | virtual ~Component(); 7 | virtual void Operation(); 8 | 9 | protected: 10 | Component(); 11 | 12 | private: 13 | }; 14 | 15 | class ConcreteComponent: public Component { 16 | public: 17 | ConcreteComponent(); 18 | ~ConcreteComponent(); 19 | void Operation(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class Decorator: public Component { 27 | public: 28 | Decorator(Component* com); 29 | virtual ~Decorator(); 30 | void Operation(); 31 | 32 | protected: 33 | Component* _com; 34 | 35 | private: 36 | }; 37 | 38 | class ConcreteDecorator: public Decorator { 39 | public: 40 | ConcreteDecorator(Component* com); 41 | ~ConcreteDecorator(); 42 | void Operation(); 43 | void AddedBehavior(); 44 | 45 | protected: 46 | 47 | private: 48 | }; 49 | 50 | #endif //~_DECORATOR_H_ 51 | -------------------------------------------------------------------------------- /结构型模式/Decorator/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Decorator/README -------------------------------------------------------------------------------- /结构型模式/Decorator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Decorator.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Component* com = new ConcreteComponent(); 8 | Decorator* dec = new ConcreteDecorator(com); 9 | dec->Operation(); 10 | delete dec; 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /结构型模式/Facade/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Facade 2 | main.cpp 3 | Facade.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /结构型模式/Facade/Facade.cpp: -------------------------------------------------------------------------------- 1 | #include "Facade.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Subsystem1::Subsystem1() { 7 | } 8 | 9 | Subsystem1::~Subsystem1() { 10 | } 11 | 12 | void Subsystem1::Operation() { 13 | cout << "Subsystem2 operation.." << endl; 14 | } 15 | 16 | Subsystem2::Subsystem2() { 17 | } 18 | 19 | Subsystem2::~Subsystem2() { 20 | } 21 | 22 | void Subsystem2::Operation() { 23 | cout << "Subsystem2 operation.." << endl; 24 | } 25 | 26 | Facade::Facade() { 27 | this->_subs1 = new Subsystem1(); 28 | this->_subs2 = new Subsystem2(); 29 | } 30 | 31 | Facade::~Facade() { 32 | delete _subs1; 33 | delete _subs2; 34 | } 35 | 36 | void Facade::OperationWrapper() { 37 | this->_subs1->Operation(); 38 | this->_subs2->Operation(); 39 | } 40 | -------------------------------------------------------------------------------- /结构型模式/Facade/Facade.h: -------------------------------------------------------------------------------- 1 | #ifndef _FACADE_H_ 2 | #define _FACADE_H_ 3 | 4 | class Subsystem1 { 5 | public: 6 | Subsystem1(); 7 | ~Subsystem1(); 8 | void Operation(); 9 | 10 | protected: 11 | 12 | private: 13 | }; 14 | 15 | class Subsystem2 { 16 | public: 17 | Subsystem2(); 18 | ~Subsystem2(); 19 | void Operation(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class Facade { 27 | public: 28 | Facade(); 29 | ~Facade(); 30 | void OperationWrapper(); 31 | 32 | protected: 33 | 34 | private: 35 | Subsystem1* _subs1; 36 | Subsystem2* _subs2; 37 | }; 38 | 39 | #endif //~_FACADE_H_ 40 | -------------------------------------------------------------------------------- /结构型模式/Facade/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Facade/README -------------------------------------------------------------------------------- /结构型模式/Facade/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Facade.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Facade* f = new Facade(); 8 | f->OperationWrapper(); 9 | return 0; 10 | } 11 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Flyweight 2 | main.cpp 3 | Flyweight.cpp 4 | FlyweightFactory.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/Flyweight.cpp: -------------------------------------------------------------------------------- 1 | #include "Flyweight.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Flyweight::Flyweight(string intrinsicState) { 7 | this->_intrinsicState = intrinsicState; 8 | } 9 | 10 | Flyweight::~Flyweight() { 11 | } 12 | 13 | void Flyweight::Operation(const string& extrinsicState) { 14 | } 15 | 16 | string Flyweight::GetIntrinsicState() { 17 | return this->_intrinsicState; 18 | } 19 | 20 | ConcreteFlyweight::ConcreteFlyweight(string intrinsicState) : 21 | Flyweight(intrinsicState) { 22 | cout << "ConcreteFlyweight Build....." << intrinsicState << endl; 23 | } 24 | 25 | ConcreteFlyweight::~ConcreteFlyweight() { 26 | } 27 | 28 | void ConcreteFlyweight::Operation(const string& extrinsicState) { 29 | cout << "ConcreteFlyweight:内蕴[" << this->GetIntrinsicState() << "] 外蕴[" 30 | << extrinsicState << "]" << endl; 31 | } 32 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/Flyweight.h: -------------------------------------------------------------------------------- 1 | #ifndef _FLYWEIGHT_H_ 2 | #define _FLYWEIGHT_H_ 3 | #include 4 | 5 | using namespace std; 6 | 7 | class Flyweight { 8 | public: 9 | 10 | virtual ~Flyweight(); 11 | virtual void Operation(const string& extrinsicState); 12 | string GetIntrinsicState(); 13 | 14 | protected: 15 | Flyweight(string intrinsicState); 16 | 17 | private: 18 | string _intrinsicState; 19 | }; 20 | 21 | class ConcreteFlyweight: public Flyweight { 22 | public: 23 | ConcreteFlyweight(string intrinsicState); 24 | ~ConcreteFlyweight(); 25 | void Operation(const string& extrinsicState); 26 | 27 | protected: 28 | 29 | private: 30 | }; 31 | 32 | #endif //~_FLYWEIGHT_H_ 33 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/FlyweightFactory.cpp: -------------------------------------------------------------------------------- 1 | #include "FlyweightFactory.h" 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | FlyweightFactory::FlyweightFactory() { 9 | } 10 | 11 | FlyweightFactory::~FlyweightFactory() { 12 | } 13 | 14 | Flyweight* FlyweightFactory::GetFlyweight(const string& key) { 15 | vector::iterator it = _fly.begin(); 16 | for (; it != _fly.end(); it++) { 17 | //找到了,就一起用,^_^ 18 | if ((*it)->GetIntrinsicState() == key) { 19 | cout << "already created by users...." << endl; 20 | return *it; 21 | } 22 | } 23 | 24 | Flyweight* fn = new ConcreteFlyweight(key); 25 | _fly.push_back(fn); 26 | return fn; 27 | } 28 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/FlyweightFactory.h: -------------------------------------------------------------------------------- 1 | #ifndef _FLYWEIGHTFACTORY_H_ 2 | #define _FLYWEIGHTFACTORY_H_ 3 | 4 | #include "Flyweight.h" 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | class FlyweightFactory { 11 | public: 12 | FlyweightFactory(); 13 | ~FlyweightFactory(); 14 | Flyweight* GetFlyweight(const string& key); 15 | 16 | protected: 17 | 18 | private: 19 | vector _fly; 20 | }; 21 | 22 | #endif //~_FLYWEIGHTFACTORY_H_ 23 | -------------------------------------------------------------------------------- /结构型模式/Flyweight/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Flyweight/README -------------------------------------------------------------------------------- /结构型模式/Flyweight/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Flyweight.h" 2 | #include "FlyweightFactory.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | FlyweightFactory* fc = new FlyweightFactory(); 9 | Flyweight* fw1 = fc->GetFlyweight("hello"); 10 | Flyweight* fw2 = fc->GetFlyweight("world!"); 11 | Flyweight* fw3 = fc->GetFlyweight("hello"); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /结构型模式/Proxy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Proxy 2 | main.cpp 3 | Proxy.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /结构型模式/Proxy/Proxy.cpp: -------------------------------------------------------------------------------- 1 | #include "Proxy.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Subject::Subject() { 7 | } 8 | 9 | Subject::~Subject() { 10 | } 11 | 12 | ConcreteSubject::ConcreteSubject() { 13 | } 14 | 15 | ConcreteSubject::~ConcreteSubject() { 16 | } 17 | 18 | void ConcreteSubject::Request() { 19 | cout << "ConcreteSubject......request...."<Request(); 36 | } 37 | -------------------------------------------------------------------------------- /结构型模式/Proxy/Proxy.h: -------------------------------------------------------------------------------- 1 | #ifndef _PROXY_H_ 2 | #define _PROXY_H_ 3 | 4 | class Subject { 5 | public: 6 | virtual ~Subject(); 7 | virtual void Request() = 0; 8 | 9 | protected: 10 | Subject(); 11 | 12 | private: 13 | }; 14 | 15 | class ConcreteSubject: public Subject { 16 | public: 17 | ConcreteSubject(); 18 | ~ConcreteSubject(); 19 | void Request(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class Proxy { 27 | public: 28 | Proxy(); 29 | Proxy(Subject* sub); 30 | ~Proxy(); 31 | void Request(); 32 | 33 | protected: 34 | 35 | private: 36 | Subject* _sub; 37 | }; 38 | 39 | #endif //~_PROXY_H_ 40 | -------------------------------------------------------------------------------- /结构型模式/Proxy/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/结构型模式/Proxy/README -------------------------------------------------------------------------------- /结构型模式/Proxy/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Proxy.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Subject* sub = new ConcreteSubject(); 8 | Proxy* p = new Proxy(sub); 9 | p->Request(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /行为模式/ChainofResponsibility/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(ChainofResponsibility 2 | main.cpp 3 | Handle.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /行为模式/ChainofResponsibility/Handle.cpp: -------------------------------------------------------------------------------- 1 | #include "Handle.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Handle::Handle() { 7 | _succ = 0; 8 | } 9 | 10 | Handle::~Handle() { 11 | delete _succ; 12 | } 13 | 14 | Handle::Handle(Handle* succ) { 15 | this->_succ = succ; 16 | } 17 | 18 | void Handle::SetSuccessor(Handle* succ) { 19 | _succ = succ; 20 | } 21 | 22 | Handle* Handle::GetSuccessor() { 23 | return _succ; 24 | } 25 | 26 | void Handle::HandleRequest() { 27 | } 28 | 29 | ConcreteHandleA::ConcreteHandleA() { 30 | } 31 | 32 | ConcreteHandleA::ConcreteHandleA(Handle* succ) : Handle(succ) { 33 | } 34 | 35 | ConcreteHandleA::~ConcreteHandleA() { 36 | } 37 | 38 | void ConcreteHandleA::HandleRequest() { 39 | if (this->GetSuccessor() != 0) { 40 | cout << "ConcreteHandleA 我把处理权给后继节点....." << endl; 41 | this->GetSuccessor()->HandleRequest(); 42 | } else { 43 | cout << "ConcreteHandleA 没有后继了,我必须自己处理...." << endl; 44 | } 45 | } 46 | 47 | ConcreteHandleB::ConcreteHandleB() { 48 | } 49 | 50 | ConcreteHandleB::ConcreteHandleB(Handle* succ) : Handle(succ) { 51 | } 52 | 53 | ConcreteHandleB::~ConcreteHandleB() { 54 | } 55 | 56 | void ConcreteHandleB::HandleRequest() { 57 | if (this->GetSuccessor() != 0) { 58 | cout << "ConcreteHandleB 我把处理权给后继节点....." << endl; 59 | this->GetSuccessor()->HandleRequest(); 60 | } else { 61 | cout << "ConcreteHandleB 没有后继了,我必须自己处理...." << endl; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /行为模式/ChainofResponsibility/Handle.h: -------------------------------------------------------------------------------- 1 | #ifndef _HANDLE_H_ 2 | #define _HANDLE_H_ 3 | 4 | class Handle { 5 | public: 6 | virtual ~Handle(); 7 | virtual void HandleRequest() = 0; 8 | void SetSuccessor(Handle* succ); 9 | Handle* GetSuccessor(); 10 | 11 | protected: 12 | Handle(); 13 | Handle(Handle* succ); 14 | 15 | private: 16 | Handle* _succ; 17 | }; 18 | 19 | class ConcreteHandleA: public Handle { 20 | public: 21 | ConcreteHandleA(); 22 | ~ConcreteHandleA(); 23 | ConcreteHandleA(Handle* succ); 24 | void HandleRequest(); 25 | 26 | protected: 27 | 28 | private: 29 | }; 30 | 31 | class ConcreteHandleB: public Handle { 32 | public: 33 | ConcreteHandleB(); 34 | ~ConcreteHandleB(); 35 | ConcreteHandleB(Handle* succ); 36 | void HandleRequest(); 37 | 38 | protected: 39 | 40 | private: 41 | }; 42 | 43 | #endif //~_HANDLE_H_ 44 | -------------------------------------------------------------------------------- /行为模式/ChainofResponsibility/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/ChainofResponsibility/README -------------------------------------------------------------------------------- /行为模式/ChainofResponsibility/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Handle.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Handle* h1 = new ConcreteHandleA(); 8 | Handle* h2 = new ConcreteHandleB(); 9 | h1->SetSuccessor(h2); 10 | h1->HandleRequest(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /行为模式/Command/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Command 2 | main.cpp 3 | Command.cpp 4 | Invoker.cpp 5 | Reciever.cpp 6 | ) 7 | -------------------------------------------------------------------------------- /行为模式/Command/Command.cpp: -------------------------------------------------------------------------------- 1 | #include "Command.h" 2 | #include "Reciever.h" 3 | #include 4 | 5 | Command::Command() { 6 | } 7 | 8 | Command::~Command() { 9 | } 10 | 11 | void Command::Excute() { 12 | } 13 | 14 | ConcreteCommand::ConcreteCommand(Reciever* rev) { 15 | this->_rev = rev; 16 | } 17 | 18 | ConcreteCommand::~ConcreteCommand() { 19 | delete this->_rev; 20 | } 21 | 22 | void ConcreteCommand::Excute() { 23 | _rev->Action(); 24 | std::cout << "ConcreteCommand..." << std::endl; 25 | } 26 | -------------------------------------------------------------------------------- /行为模式/Command/Command.h: -------------------------------------------------------------------------------- 1 | #ifndef _COMMAND_H_ 2 | #define _COMMAND_H_ 3 | 4 | class Reciever; 5 | 6 | class Command { 7 | public: 8 | virtual ~Command(); 9 | virtual void Excute() = 0; 10 | 11 | protected: 12 | Command(); 13 | 14 | private: 15 | }; 16 | 17 | class ConcreteCommand: public Command { 18 | public: 19 | ConcreteCommand(Reciever* rev); 20 | ~ConcreteCommand(); 21 | void Excute(); 22 | 23 | protected: 24 | 25 | private: 26 | Reciever* _rev; 27 | }; 28 | 29 | #endif //~_COMMAND_H_ 30 | -------------------------------------------------------------------------------- /行为模式/Command/Invoker.cpp: -------------------------------------------------------------------------------- 1 | #include "Invoker.h" 2 | #include "Command.h" 3 | #include 4 | 5 | Invoker::Invoker(Command* cmd) { 6 | _cmd = cmd; 7 | } 8 | 9 | Invoker::~Invoker() { 10 | delete _cmd; 11 | } 12 | 13 | void Invoker::Invoke() { 14 | _cmd->Excute(); 15 | } 16 | -------------------------------------------------------------------------------- /行为模式/Command/Invoker.h: -------------------------------------------------------------------------------- 1 | #ifndef _INVOKER_H_ 2 | #define _INVOKER_H_ 3 | 4 | class Command; 5 | 6 | class Invoker { 7 | public: 8 | Invoker(Command* cmd); 9 | ~Invoker(); 10 | void Invoke(); 11 | 12 | protected: 13 | 14 | private: 15 | Command* _cmd; 16 | }; 17 | 18 | #endif //~_INVOKER_H_ 19 | -------------------------------------------------------------------------------- /行为模式/Command/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Command/README -------------------------------------------------------------------------------- /行为模式/Command/Reciever.cpp: -------------------------------------------------------------------------------- 1 | #include "Reciever.h" 2 | #include 3 | 4 | Reciever::Reciever() { 5 | } 6 | 7 | Reciever::~Reciever() { 8 | } 9 | 10 | void Reciever::Action() { 11 | std::cout << "Reciever action......." << std::endl; 12 | } 13 | -------------------------------------------------------------------------------- /行为模式/Command/Reciever.h: -------------------------------------------------------------------------------- 1 | #ifndef _RECIEVER_H_ 2 | #define _RECIEVER_H_ 3 | 4 | class Reciever { 5 | public: 6 | Reciever(); 7 | ~Reciever(); 8 | void Action(); 9 | 10 | protected: 11 | 12 | private: 13 | }; 14 | 15 | #endif //~_RECIEVER_H_ 16 | -------------------------------------------------------------------------------- /行为模式/Command/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Command.h" 2 | #include "Invoker.h" 3 | #include "Reciever.h" 4 | #include 5 | 6 | using namespace std; 7 | 8 | int main(int argc, char* argv[]) { 9 | Reciever* rev = new Reciever(); 10 | Command* cmd = new ConcreteCommand(rev); 11 | Invoker* inv = new Invoker(cmd); 12 | inv->Invoke(); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /行为模式/Interpreter/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Interpreter 2 | main.cpp 3 | Context.cpp 4 | Interpret.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Interpreter/Context.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | 3 | Context::Context() { 4 | } 5 | 6 | Context::~Context() { 7 | } 8 | -------------------------------------------------------------------------------- /行为模式/Interpreter/Context.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONTEXT_H_ 2 | #define _CONTEXT_H_ 3 | 4 | class Context { 5 | public: 6 | Context(); 7 | ~Context(); 8 | 9 | protected: 10 | 11 | private: 12 | }; 13 | 14 | #endif //~_CONTEXT_H_ 15 | -------------------------------------------------------------------------------- /行为模式/Interpreter/Interpret.cpp: -------------------------------------------------------------------------------- 1 | #include "Interpret.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | AbstractExpression::AbstractExpression() { 7 | } 8 | 9 | AbstractExpression::~AbstractExpression() { 10 | } 11 | 12 | void AbstractExpression::Interpret(const Context& c) { 13 | } 14 | 15 | TerminalExpression::TerminalExpression(const string& statement) { 16 | this->_statement = statement; 17 | } 18 | 19 | TerminalExpression::~TerminalExpression() { 20 | } 21 | 22 | void TerminalExpression::Interpret(const Context& c) { 23 | cout << this->_statement << " TerminalExpression" << endl; 24 | } 25 | 26 | NonterminalExpression::NonterminalExpression(AbstractExpression* expression, 27 | int times) { 28 | this->_expression = expression; 29 | this->_times = times; 30 | } 31 | 32 | NonterminalExpression::~NonterminalExpression() { 33 | } 34 | 35 | void NonterminalExpression::Interpret(const Context& c) { 36 | for (int i = 0; i < _times; i++) { 37 | this->_expression->Interpret(c); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /行为模式/Interpreter/Interpret.h: -------------------------------------------------------------------------------- 1 | #ifndef _INTERPRET_H_ 2 | #define _INTERPRET_H_ 3 | 4 | #include "Context.h" 5 | #include 6 | 7 | using namespace std; 8 | 9 | class AbstractExpression { 10 | public: 11 | virtual ~AbstractExpression(); 12 | virtual void Interpret(const Context& c); 13 | 14 | protected: 15 | AbstractExpression(); 16 | 17 | private: 18 | }; 19 | 20 | class TerminalExpression: public AbstractExpression { 21 | public: 22 | TerminalExpression(const string& statement); 23 | ~ TerminalExpression(); 24 | void Interpret(const Context& c); 25 | 26 | protected: 27 | 28 | private: 29 | string _statement; 30 | }; 31 | 32 | class NonterminalExpression: public AbstractExpression { 33 | public: 34 | NonterminalExpression(AbstractExpression* expression, int times); 35 | ~ NonterminalExpression(); 36 | void Interpret(const Context& c); 37 | 38 | protected: 39 | 40 | private: 41 | AbstractExpression* _expression; 42 | int _times; 43 | }; 44 | 45 | #endif //~_INTERPRET_H_ 46 | -------------------------------------------------------------------------------- /行为模式/Interpreter/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Interpreter/README -------------------------------------------------------------------------------- /行为模式/Interpreter/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "Interpret.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | Context* c = new Context(); 9 | AbstractExpression* te = new TerminalExpression("hello"); 10 | AbstractExpression* nte = new NonterminalExpression(te, 2); 11 | nte->Interpret(*c); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /行为模式/Iterator/Aggregate.cpp: -------------------------------------------------------------------------------- 1 | #include "Aggregate.h" 2 | #include "Iterator.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Aggregate::Aggregate() { 8 | } 9 | 10 | Aggregate::~Aggregate() { 11 | } 12 | 13 | ConcreteAggregate::ConcreteAggregate() { 14 | for (int i = 0; i < SIZE; i++) 15 | _objs[i] = i; 16 | } 17 | 18 | ConcreteAggregate::~ConcreteAggregate() { 19 | } 20 | 21 | Iterator* ConcreteAggregate::CreateIterator() { 22 | return new ConcreteIterator(this); 23 | } 24 | 25 | Object ConcreteAggregate::GetItem(int idx) { 26 | if (idx < this->GetSize()) 27 | return _objs[idx]; 28 | else 29 | 30 | return -1; 31 | } 32 | 33 | int ConcreteAggregate::GetSize() { 34 | return SIZE; 35 | } 36 | -------------------------------------------------------------------------------- /行为模式/Iterator/Aggregate.h: -------------------------------------------------------------------------------- 1 | #ifndef _AGGREGATE_H_ 2 | #define _AGGREGATE_H_ 3 | 4 | class Iterator; 5 | 6 | typedef int Object; 7 | 8 | class Interator; 9 | 10 | class Aggregate { 11 | public: 12 | virtual ~Aggregate(); 13 | virtual Iterator* CreateIterator() = 0; 14 | virtual Object GetItem(int idx) = 0; 15 | virtual int GetSize() = 0; 16 | 17 | protected: 18 | Aggregate(); 19 | 20 | private: 21 | }; 22 | 23 | class ConcreteAggregate: public Aggregate { 24 | public: 25 | enum { 26 | SIZE = 3 27 | }; 28 | ConcreteAggregate(); 29 | ~ConcreteAggregate(); 30 | Iterator* CreateIterator(); 31 | Object GetItem(int idx); 32 | int GetSize(); 33 | 34 | protected: 35 | 36 | private: 37 | Object _objs[SIZE]; 38 | }; 39 | 40 | #endif //~_AGGREGATE_H_ 41 | -------------------------------------------------------------------------------- /行为模式/Iterator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Iterator 2 | main.cpp 3 | Aggregate.cpp 4 | Iterator.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Iterator/Iterator.cpp: -------------------------------------------------------------------------------- 1 | #include "Iterator.h" 2 | #include "Aggregate.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Iterator::Iterator() { 8 | } 9 | 10 | Iterator::~Iterator() { 11 | } 12 | 13 | ConcreteIterator::ConcreteIterator(Aggregate* ag, int idx) { 14 | this->_ag = ag; 15 | this->_idx = idx; 16 | } 17 | 18 | ConcreteIterator::~ConcreteIterator() { 19 | } 20 | 21 | Object ConcreteIterator::CurrentItem() { 22 | return _ag->GetItem(_idx); 23 | } 24 | 25 | void ConcreteIterator::First() { 26 | _idx = 0; 27 | } 28 | 29 | void ConcreteIterator::Next() { 30 | if (_idx < _ag->GetSize()) 31 | _idx++; 32 | } 33 | 34 | bool ConcreteIterator::IsDone() { 35 | return (_idx == _ag->GetSize()); 36 | } 37 | -------------------------------------------------------------------------------- /行为模式/Iterator/Iterator.h: -------------------------------------------------------------------------------- 1 | #ifndef _ITERATOR_H_ 2 | #define _ITERATOR_H_ 3 | 4 | class Aggregate; 5 | 6 | typedef int Object; 7 | 8 | class Iterator { 9 | public: 10 | virtual ~Iterator(); 11 | virtual void First() = 0; 12 | virtual void Next() = 0; 13 | virtual bool IsDone() = 0; 14 | virtual Object CurrentItem() = 0; 15 | 16 | protected: 17 | Iterator(); 18 | 19 | private: 20 | }; 21 | 22 | class ConcreteIterator: public Iterator { 23 | public: 24 | ConcreteIterator(Aggregate* ag, int idx = 0); 25 | ~ConcreteIterator(); 26 | void First(); 27 | void Next(); 28 | bool IsDone(); 29 | Object CurrentItem(); 30 | 31 | protected: 32 | 33 | private: 34 | Aggregate* _ag; 35 | int _idx; 36 | }; 37 | 38 | #endif //~_ITERATOR_H_ 39 | -------------------------------------------------------------------------------- /行为模式/Iterator/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Iterator/README -------------------------------------------------------------------------------- /行为模式/Iterator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Iterator.h" 2 | #include "Aggregate.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | Aggregate* ag = new ConcreteAggregate(); 9 | Iterator* it = new ConcreteIterator(ag); 10 | for (; !(it->IsDone()); it->Next()) { 11 | cout << it->CurrentItem() << endl; 12 | } 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /行为模式/Mediator/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Mediator 2 | main.cpp 3 | Colleage.cpp 4 | Mediator.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Mediator/Colleage.cpp: -------------------------------------------------------------------------------- 1 | #include "Mediator.h" 2 | #include "Colleage.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Colleage::Colleage() { 8 | //_sdt = " "; 9 | } 10 | 11 | Colleage::Colleage(Mediator* mdt) { 12 | this->_mdt = mdt; 13 | //_sdt = " "; 14 | } 15 | 16 | Colleage::~Colleage() { 17 | } 18 | 19 | ConcreteColleageA::ConcreteColleageA() { 20 | } 21 | 22 | ConcreteColleageA::~ConcreteColleageA() { 23 | } 24 | 25 | ConcreteColleageA::ConcreteColleageA(Mediator* mdt) : 26 | Colleage(mdt) { 27 | } 28 | 29 | string ConcreteColleageA::GetState() { 30 | return _sdt; 31 | } 32 | 33 | void ConcreteColleageA::SetState(const string& sdt) { 34 | _sdt = sdt; 35 | } 36 | 37 | void ConcreteColleageA::Aciton() { 38 | _mdt->DoActionFromAtoB(); 39 | cout << "State of ConcreteColleageB:" << "" << this->GetState() << endl; 40 | } 41 | 42 | ConcreteColleageB::ConcreteColleageB() { 43 | } 44 | 45 | ConcreteColleageB::~ConcreteColleageB() { 46 | } 47 | 48 | ConcreteColleageB::ConcreteColleageB(Mediator* mdt) : 49 | Colleage(mdt) { 50 | } 51 | 52 | void ConcreteColleageB::Aciton() { 53 | _mdt->DoActionFromBtoA(); 54 | cout << "State of ConcreteColleageA:" << "" << this->GetState() << endl; 55 | } 56 | 57 | string ConcreteColleageB::GetState() { 58 | return _sdt; 59 | } 60 | 61 | void ConcreteColleageB::SetState(const string& sdt) { 62 | _sdt = sdt; 63 | } 64 | -------------------------------------------------------------------------------- /行为模式/Mediator/Colleage.h: -------------------------------------------------------------------------------- 1 | #ifndef _COLLEAGE_H_ 2 | #define _COLLEAGE_H_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Mediator; 9 | 10 | class Colleage { 11 | public: 12 | virtual ~Colleage(); 13 | virtual void Aciton() = 0; 14 | virtual void SetState(const string& sdt) = 0; 15 | virtual string GetState() = 0; 16 | 17 | protected: 18 | Colleage(); 19 | Colleage(Mediator* mdt); 20 | Mediator* _mdt; 21 | 22 | private: 23 | }; 24 | 25 | class ConcreteColleageA: public Colleage { 26 | public: 27 | ConcreteColleageA(); 28 | ConcreteColleageA(Mediator* mdt); 29 | ~ConcreteColleageA(); 30 | void Aciton(); 31 | void SetState(const string& sdt); 32 | string GetState(); 33 | 34 | protected: 35 | 36 | private: 37 | string _sdt; 38 | }; 39 | 40 | class ConcreteColleageB: public Colleage { 41 | public: 42 | ConcreteColleageB(); 43 | ConcreteColleageB(Mediator* mdt); 44 | ~ConcreteColleageB(); 45 | void Aciton(); 46 | void SetState(const string& sdt); 47 | string GetState(); 48 | 49 | protected: 50 | 51 | private: 52 | string _sdt; 53 | }; 54 | 55 | #endif //~_COLLEAGE_H_ 56 | -------------------------------------------------------------------------------- /行为模式/Mediator/Mediator.cpp: -------------------------------------------------------------------------------- 1 | #include "Mediator.h" 2 | #include "Colleage.h" 3 | 4 | Mediator::Mediator() { 5 | } 6 | 7 | Mediator::~Mediator() { 8 | } 9 | 10 | ConcreteMediator::ConcreteMediator() { 11 | } 12 | 13 | ConcreteMediator::~ConcreteMediator() { 14 | } 15 | 16 | ConcreteMediator::ConcreteMediator(Colleage* clgA, Colleage* clgB) { 17 | this->_clgA = clgA; 18 | this->_clgB = clgB; 19 | } 20 | 21 | void ConcreteMediator::DoActionFromAtoB() { 22 | _clgB->SetState(_clgA->GetState()); 23 | } 24 | 25 | void ConcreteMediator::SetConcreteColleageA(Colleage* clgA) { 26 | this->_clgA = clgA; 27 | } 28 | 29 | void ConcreteMediator::SetConcreteColleageB(Colleage* clgB) { 30 | this->_clgB = clgB; 31 | } 32 | 33 | Colleage* ConcreteMediator::GetConcreteColleageA() { 34 | return _clgA; 35 | } 36 | 37 | Colleage* ConcreteMediator::GetConcreteColleageB() { 38 | return _clgB; 39 | } 40 | 41 | void ConcreteMediator::IntroColleage(Colleage* clgA, Colleage* clgB) { 42 | this->_clgA = clgA; 43 | this->_clgB = clgB; 44 | } 45 | 46 | void ConcreteMediator::DoActionFromBtoA() { 47 | _clgA->SetState(_clgB->GetState()); 48 | } 49 | -------------------------------------------------------------------------------- /行为模式/Mediator/Mediator.h: -------------------------------------------------------------------------------- 1 | #ifndef _MEDIATOR_H_ 2 | #define _MEDIATOR_H_ 3 | 4 | class Colleage; 5 | class Mediator { 6 | public: 7 | virtual ~Mediator(); 8 | virtual void DoActionFromAtoB() = 0; 9 | virtual void DoActionFromBtoA() = 0; 10 | 11 | protected: 12 | Mediator(); 13 | 14 | private: 15 | }; 16 | 17 | class ConcreteMediator: public Mediator { 18 | public: 19 | ConcreteMediator(); 20 | ConcreteMediator(Colleage* clgA, Colleage* clgB); 21 | ~ConcreteMediator(); 22 | void SetConcreteColleageA(Colleage* clgA); 23 | void SetConcreteColleageB(Colleage* clgB); 24 | Colleage* GetConcreteColleageA(); 25 | Colleage* GetConcreteColleageB(); 26 | void IntroColleage(Colleage* clgA, Colleage* clgB); 27 | void DoActionFromAtoB(); 28 | void DoActionFromBtoA(); 29 | 30 | protected: 31 | 32 | private: 33 | Colleage* _clgA; 34 | Colleage* _clgB; 35 | }; 36 | 37 | #endif //~_MEDIATOR_H_ 38 | -------------------------------------------------------------------------------- /行为模式/Mediator/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Mediator/README -------------------------------------------------------------------------------- /行为模式/Mediator/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Mediator.h" 2 | #include "Colleage.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | ConcreteMediator* m = new ConcreteMediator(); 9 | ConcreteColleageA* c1 = new ConcreteColleageA(m); 10 | ConcreteColleageB* c2 = new ConcreteColleageB(m); 11 | m->IntroColleage(c1, c2); 12 | c1->SetState("old"); 13 | c2->SetState("old"); 14 | c1->Aciton(); 15 | c2->Aciton(); 16 | cout << endl; 17 | c1->SetState("new"); 18 | c1->Aciton(); 19 | c2->Aciton(); 20 | cout << endl; 21 | c2->SetState("old"); 22 | c2->Aciton(); 23 | c1->Aciton(); 24 | 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /行为模式/Memento/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Memento 2 | main.cpp 3 | Memento.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /行为模式/Memento/Memento.cpp: -------------------------------------------------------------------------------- 1 | #include "Memento.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | typedef string State; 7 | 8 | Originator::Originator() { 9 | _sdt = ""; 10 | _mt = 0; 11 | } 12 | 13 | Originator::Originator(const State& sdt) { 14 | _sdt = sdt; 15 | _mt = 0; 16 | } 17 | 18 | Originator::~Originator() { 19 | } 20 | 21 | Memento* Originator::CreateMemento() { 22 | return new Memento(_sdt); 23 | } 24 | 25 | State Originator::GetState() { 26 | return _sdt; 27 | } 28 | 29 | void Originator::SetState(const State& sdt) { 30 | _sdt = sdt; 31 | } 32 | 33 | void Originator::PrintState() { 34 | cout << this->_sdt << "....." << endl; 35 | } 36 | 37 | void Originator::SetMemento(Memento* men) { 38 | } 39 | 40 | void Originator::RestoreToMemento(Memento* mt) { 41 | this->_sdt = mt->GetState(); 42 | } 43 | 44 | Memento::Memento() { 45 | } 46 | 47 | Memento::Memento(const State& sdt) { 48 | _sdt = sdt; 49 | } 50 | 51 | State Memento::GetState() { 52 | return _sdt; 53 | } 54 | 55 | void Memento::SetState(const State& sdt) { 56 | _sdt = sdt; 57 | } 58 | -------------------------------------------------------------------------------- /行为模式/Memento/Memento.h: -------------------------------------------------------------------------------- 1 | #ifndef _MEMENTO_H_ 2 | #define _MEMENTO_H_ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | class Memento; 9 | 10 | class Originator { 11 | public: 12 | typedef string State; 13 | Originator(); 14 | Originator(const State& sdt); 15 | ~Originator(); 16 | 17 | Memento* CreateMemento(); 18 | void SetMemento(Memento* men); 19 | void RestoreToMemento(Memento* mt); 20 | State GetState(); 21 | void SetState(const State& sdt); 22 | void PrintState(); 23 | 24 | protected: 25 | 26 | private: 27 | State _sdt; 28 | Memento* _mt; 29 | }; 30 | 31 | class Memento { 32 | public: 33 | 34 | protected: 35 | 36 | private: 37 | //这是最关键的地方,将 Originator 为 friend 类,可以访问内部信息,但是其他类不能访问 38 | friend class Originator; 39 | typedef string State; 40 | Memento(); 41 | Memento(const State& sdt); 42 | ~Memento(); 43 | void SetState(const State& sdt); 44 | State GetState(); 45 | 46 | private: 47 | State _sdt; 48 | }; 49 | 50 | #endif //~_MEMENTO_H_ 51 | -------------------------------------------------------------------------------- /行为模式/Memento/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Memento/README -------------------------------------------------------------------------------- /行为模式/Memento/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Memento.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | Originator* o = new Originator(); 8 | o->SetState("old"); //备忘前状态 9 | o->PrintState(); 10 | Memento* m = o->CreateMemento(); //将状态备忘 11 | o->SetState("new"); //修改状态 12 | o->PrintState(); 13 | o->RestoreToMemento(m); 14 | //恢复修改前状态 15 | o->PrintState(); 16 | return 0; 17 | } 18 | -------------------------------------------------------------------------------- /行为模式/Observer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Observer 2 | main.cpp 3 | Observer.cpp 4 | Subject.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Observer/Observer.cpp: -------------------------------------------------------------------------------- 1 | #include "Observer.h" 2 | #include "Subject.h" 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | Observer::Observer() { 9 | _st = '\0'; 10 | } 11 | 12 | Observer::~Observer() { 13 | } 14 | 15 | ConcreteObserverA::ConcreteObserverA(Subject* sub) { 16 | _sub = sub; 17 | _sub->Attach(this); 18 | } 19 | 20 | ConcreteObserverA::~ConcreteObserverA() { 21 | _sub->Detach(this); 22 | if (_sub != 0) { 23 | delete _sub; 24 | } 25 | } 26 | 27 | Subject* ConcreteObserverA::GetSubject() { 28 | return _sub; 29 | } 30 | 31 | void ConcreteObserverA::PrintInfo() { 32 | cout << "ConcreteObserverA observer...." << _sub->GetState() << endl; 33 | } 34 | 35 | void ConcreteObserverA::Update(Subject* sub) { 36 | _st = sub->GetState(); 37 | PrintInfo(); 38 | } 39 | 40 | ConcreteObserverB::ConcreteObserverB(Subject* sub) { 41 | _sub = sub; 42 | _sub->Attach(this); 43 | } 44 | 45 | ConcreteObserverB::~ConcreteObserverB() { 46 | _sub->Detach(this); 47 | if (_sub != 0) { 48 | delete _sub; 49 | } 50 | } 51 | Subject* ConcreteObserverB::GetSubject() { 52 | 53 | return _sub; 54 | } 55 | 56 | void ConcreteObserverB::PrintInfo() { 57 | cout << "ConcreteObserverB observer...." << _sub->GetState() << endl; 58 | } 59 | 60 | void ConcreteObserverB::Update(Subject* sub) { 61 | _st = sub->GetState(); 62 | PrintInfo(); 63 | } 64 | -------------------------------------------------------------------------------- /行为模式/Observer/Observer.h: -------------------------------------------------------------------------------- 1 | #ifndef _OBSERVER_H_ 2 | #define _OBSERVER_H_ 3 | 4 | #include "Subject.h" 5 | #include 6 | 7 | using namespace std; 8 | 9 | typedef string State; 10 | 11 | class Observer { 12 | public: 13 | virtual ~Observer(); 14 | virtual void Update(Subject* sub) = 0; 15 | virtual void PrintInfo() = 0; 16 | 17 | protected: 18 | Observer(); 19 | State _st; 20 | 21 | private: 22 | }; 23 | 24 | class ConcreteObserverA: public Observer { 25 | public: 26 | virtual Subject* GetSubject(); 27 | ConcreteObserverA(Subject* sub); 28 | virtual ~ConcreteObserverA(); 29 | //传入 Subject 作为参数,这样可以让一个 View 属于多个的 Subject。 30 | void Update(Subject* sub); 31 | void PrintInfo(); 32 | 33 | protected: 34 | 35 | private: 36 | Subject* _sub; 37 | }; 38 | 39 | class ConcreteObserverB: public Observer { 40 | public: 41 | virtual Subject* GetSubject(); 42 | ConcreteObserverB(Subject* sub); 43 | virtual ~ConcreteObserverB(); 44 | //传入 Subject 作为参数,这样可以让一个 View 属于多个的 Subject。 45 | void Update(Subject* sub); 46 | void PrintInfo(); 47 | 48 | protected: 49 | 50 | private: 51 | Subject* _sub; 52 | }; 53 | 54 | #endif //~_OBSERVER_H_ 55 | -------------------------------------------------------------------------------- /行为模式/Observer/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Observer/README -------------------------------------------------------------------------------- /行为模式/Observer/Subject.cpp: -------------------------------------------------------------------------------- 1 | #include "Subject.h" 2 | #include "Observer.h" 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | 8 | typedef string state; 9 | 10 | Subject::Subject() { 11 | // 在模板的使用之前一定要 new,创建 12 | _obvs = new list; 13 | } 14 | 15 | Subject::~Subject() { 16 | } 17 | 18 | void Subject::Attach(Observer* obv) { 19 | _obvs->push_front(obv); 20 | } 21 | 22 | void Subject::Detach(Observer* obv) { 23 | if (obv != NULL) 24 | _obvs->remove(obv); 25 | } 26 | 27 | void Subject::Notify() { 28 | list::iterator it; 29 | it = _obvs->begin(); 30 | for (; it != _obvs->end(); it++) { 31 | //关于模板和 iterator 的用法 32 | (*it)->Update(this); 33 | } 34 | } 35 | 36 | ConcreteSubject::ConcreteSubject() { 37 | _st = '\0'; 38 | } 39 | 40 | ConcreteSubject::~ConcreteSubject() { 41 | } 42 | 43 | State ConcreteSubject::GetState() { 44 | return _st; 45 | } 46 | 47 | void ConcreteSubject::SetState(const State& st) { 48 | _st = st; 49 | } 50 | -------------------------------------------------------------------------------- /行为模式/Observer/Subject.h: -------------------------------------------------------------------------------- 1 | #ifndef _SUBJECT_H_ 2 | #define _SUBJECT_H_ 3 | 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | 9 | typedef string State; 10 | 11 | class Observer; 12 | 13 | class Subject { 14 | public: 15 | virtual ~Subject(); 16 | virtual void Attach(Observer* obv); 17 | virtual void Detach(Observer* obv); 18 | virtual void Notify(); 19 | virtual void SetState(const State& st) = 0; 20 | virtual State GetState() = 0; 21 | 22 | protected: 23 | Subject(); 24 | 25 | private: 26 | list* _obvs; 27 | }; 28 | 29 | class ConcreteSubject: public Subject { 30 | public: 31 | ConcreteSubject(); 32 | ~ConcreteSubject(); 33 | State GetState(); 34 | void SetState(const State& st); 35 | 36 | protected: 37 | 38 | private: 39 | State _st; 40 | }; 41 | #endif //~_SUBJECT_H_ 42 | -------------------------------------------------------------------------------- /行为模式/Observer/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Subject.h" 2 | #include "Observer.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | ConcreteSubject* sub = new ConcreteSubject(); 9 | Observer* o1 = new ConcreteObserverA(sub); 10 | Observer* o2 = new ConcreteObserverB(sub); 11 | sub->SetState("old"); 12 | sub->Notify(); 13 | sub->SetState("new"); //也可以由 Observer 调用 14 | sub->Notify(); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /行为模式/State/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(State 2 | main.cpp 3 | Context.cpp 4 | State.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/State/Context.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "State.h" 3 | 4 | Context::Context() { 5 | } 6 | 7 | Context::Context(State* state) { 8 | this->_state = state; 9 | } 10 | 11 | Context::~Context() { 12 | delete _state; 13 | } 14 | 15 | void Context::OprationInterface() { 16 | _state->OperationInterface(this); 17 | } 18 | 19 | bool Context::ChangeState(State* state) { 20 | ///_state->ChangeState(this,state); 21 | this->_state = state; 22 | return true; 23 | } 24 | 25 | void Context::OperationChangState() { 26 | _state->OperationChangeState(this); 27 | 28 | } 29 | -------------------------------------------------------------------------------- /行为模式/State/Context.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONTEXT_H_ 2 | #define _CONTEXT_H_ 3 | 4 | class State; 5 | 6 | class Context { 7 | public: 8 | Context(); 9 | Context(State* state); 10 | ~Context(); 11 | void OprationInterface(); 12 | void OperationChangState(); 13 | protected: 14 | 15 | private: 16 | friend class State; //表明在 State 类中可以访问 Context 类的 private 字段 17 | 18 | bool ChangeState(State* state); 19 | 20 | private: 21 | State* _state; 22 | }; 23 | 24 | #endif //~_CONTEXT_H_ 25 | -------------------------------------------------------------------------------- /行为模式/State/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/State/README -------------------------------------------------------------------------------- /行为模式/State/State.cpp: -------------------------------------------------------------------------------- 1 | #include "State.h" 2 | #include "Context.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | State::State() { 8 | } 9 | 10 | State::~State() { 11 | } 12 | 13 | void State::OperationInterface(Context* con) { 14 | cout << "State::.." << endl; 15 | 16 | } 17 | 18 | bool State::ChangeState(Context* con, State* st) { 19 | con->ChangeState(st); 20 | return true; 21 | } 22 | 23 | void State::OperationChangeState(Context* con) { 24 | } 25 | 26 | ConcreteStateA::ConcreteStateA() { 27 | } 28 | 29 | ConcreteStateA::~ConcreteStateA() { 30 | } 31 | 32 | void ConcreteStateA::OperationInterface(Context* con) { 33 | cout << "ConcreteStateA::OperationInterface......" << endl; 34 | } 35 | 36 | void ConcreteStateA::OperationChangeState(Context* con) { 37 | OperationInterface(con); 38 | this->ChangeState(con, new ConcreteStateB()); 39 | } 40 | 41 | ConcreteStateB::ConcreteStateB() { 42 | } 43 | 44 | ConcreteStateB::~ConcreteStateB() { 45 | } 46 | 47 | void ConcreteStateB::OperationInterface(Context* con) { 48 | cout << "ConcreteStateB::OperationInterface......" << endl; 49 | } 50 | 51 | void ConcreteStateB::OperationChangeState(Context* con) { 52 | OperationInterface(con); 53 | this->ChangeState(con, new ConcreteStateA()); 54 | } 55 | -------------------------------------------------------------------------------- /行为模式/State/State.h: -------------------------------------------------------------------------------- 1 | #ifndef _STATE_H_ 2 | #define _STATE_H_ 3 | 4 | class Context; 5 | 6 | class State { 7 | public: 8 | State(); 9 | virtual ~State(); 10 | virtual void OperationInterface(Context*) = 0; 11 | virtual void OperationChangeState(Context*) = 0; 12 | 13 | protected: 14 | bool ChangeState(Context* con, State* st); 15 | 16 | private: 17 | }; 18 | 19 | class ConcreteStateA: public State { 20 | public: 21 | ConcreteStateA(); 22 | virtual ~ConcreteStateA(); 23 | virtual void OperationInterface(Context*); 24 | virtual void OperationChangeState(Context*); 25 | 26 | protected: 27 | 28 | private: 29 | }; 30 | 31 | class ConcreteStateB: public State { 32 | public: 33 | ConcreteStateB(); 34 | virtual ~ConcreteStateB(); 35 | virtual void OperationInterface(Context*); 36 | virtual void OperationChangeState(Context*); 37 | 38 | protected: 39 | 40 | private: 41 | }; 42 | 43 | #endif //~_STATE_H_ 44 | -------------------------------------------------------------------------------- /行为模式/State/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "State.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | State* st = new ConcreteStateA(); 9 | Context* con = new Context(st); 10 | con->OperationChangState(); 11 | con->OperationChangState(); 12 | con->OperationChangState(); 13 | if (con != NULL) 14 | delete con; 15 | if (st != NULL) 16 | st = NULL; 17 | return 0; 18 | } 19 | -------------------------------------------------------------------------------- /行为模式/Strategy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Strategy 2 | main.cpp 3 | Context.cpp 4 | Strategy.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Strategy/Context.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "Strategy.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Context::Context(Strategy* stg) { 8 | _stg = stg; 9 | } 10 | 11 | Context::~Context() { 12 | if (!_stg) 13 | delete _stg; 14 | } 15 | 16 | void Context::DoAction() { 17 | _stg->AlgrithmInterface(); 18 | } 19 | 20 | -------------------------------------------------------------------------------- /行为模式/Strategy/Context.h: -------------------------------------------------------------------------------- 1 | #ifndef _CONTEXT_H_ 2 | #define _CONTEXT_H_ 3 | 4 | class Strategy; 5 | 6 | /** 7 | *这个类是 Strategy 模式的关键,也是 Strategy 模式和 Template 模式的根本 8 | 第 83 页 共 171 页 9 | k_eckel设计模式精解-GoF 23 种设计模式解析附 C++实现源码 10 | http://www.mscenter.edu.cn/blog/k_eckel 11 | 区别所在。 12 | *Strategy 通过“组合”(委托)方式实现算法(实现)的异构,而 Template 模 13 | 式则采取的是继承的方式 14 | *这两个模式的区别也是继承和组合两种实现接口重用的方式的区别 15 | */ 16 | class Context { 17 | public: 18 | Context(Strategy* stg); 19 | ~Context(); 20 | void DoAction(); 21 | 22 | protected: 23 | 24 | private: 25 | Strategy* _stg; 26 | }; 27 | 28 | #endif //~_CONTEXT_H_ 29 | -------------------------------------------------------------------------------- /行为模式/Strategy/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Strategy/README -------------------------------------------------------------------------------- /行为模式/Strategy/Strategy.cpp: -------------------------------------------------------------------------------- 1 | #include "Strategy.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | Strategy::Strategy() { 7 | } 8 | 9 | Strategy::~Strategy() { 10 | 11 | } 12 | 13 | void Strategy::AlgrithmInterface() { 14 | } 15 | 16 | ConcreteStrategyA::ConcreteStrategyA() { 17 | } 18 | 19 | ConcreteStrategyA::~ConcreteStrategyA() { 20 | cout << "~ConcreteStrategyA....." << endl; 21 | } 22 | 23 | void ConcreteStrategyA::AlgrithmInterface() { 24 | cout << "test ConcreteStrategyA....." << endl; 25 | } 26 | 27 | ConcreteStrategyB::ConcreteStrategyB() { 28 | } 29 | 30 | ConcreteStrategyB::~ConcreteStrategyB() { 31 | cout << "~ConcreteStrategyB....." << endl; 32 | } 33 | 34 | void ConcreteStrategyB::AlgrithmInterface() { 35 | cout << "test ConcreteStrategyB....." << endl; 36 | } 37 | -------------------------------------------------------------------------------- /行为模式/Strategy/Strategy.h: -------------------------------------------------------------------------------- 1 | #ifndef _STRATEGY_H_ 2 | #define _STRATEGY_H_ 3 | 4 | class Strategy { 5 | public: 6 | Strategy(); 7 | virtual ~Strategy(); 8 | virtual void AlgrithmInterface() = 0; 9 | 10 | protected: 11 | 12 | private: 13 | }; 14 | 15 | class ConcreteStrategyA: public Strategy { 16 | public: 17 | ConcreteStrategyA(); 18 | virtual ~ConcreteStrategyA(); 19 | void AlgrithmInterface(); 20 | 21 | protected: 22 | 23 | private: 24 | }; 25 | 26 | class ConcreteStrategyB: public Strategy { 27 | public: 28 | ConcreteStrategyB(); 29 | virtual ~ConcreteStrategyB(); 30 | void AlgrithmInterface(); 31 | 32 | protected: 33 | 34 | private: 35 | }; 36 | 37 | #endif //~_STRATEGY_H_ 38 | -------------------------------------------------------------------------------- /行为模式/Strategy/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Context.h" 2 | #include "Strategy.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | Strategy* ps = new ConcreteStrategyA(); 9 | Context* pc = new Context(ps); 10 | pc->DoAction(); 11 | if (NULL != pc) 12 | delete pc; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /行为模式/Template/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Template 2 | main.cpp 3 | Template.cpp 4 | ) 5 | -------------------------------------------------------------------------------- /行为模式/Template/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Template/README -------------------------------------------------------------------------------- /行为模式/Template/Template.cpp: -------------------------------------------------------------------------------- 1 | #include "Template.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | AbstractClass::AbstractClass() { 7 | } 8 | 9 | AbstractClass::~AbstractClass() { 10 | } 11 | 12 | void AbstractClass::TemplateMethod() { 13 | this->PrimitiveOperation1(); 14 | this->PrimitiveOperation2(); 15 | } 16 | 17 | ConcreteClass1::ConcreteClass1() { 18 | } 19 | 20 | ConcreteClass1::~ConcreteClass1() { 21 | } 22 | 23 | void ConcreteClass1::PrimitiveOperation1() { 24 | cout << "ConcreteClass1...PrimitiveOperation1" << endl; 25 | } 26 | 27 | void ConcreteClass1::PrimitiveOperation2() { 28 | cout << "ConcreteClass1...PrimitiveOperation2" << endl; 29 | } 30 | 31 | ConcreteClass2::ConcreteClass2() { 32 | } 33 | 34 | ConcreteClass2::~ConcreteClass2() { 35 | } 36 | 37 | void ConcreteClass2::PrimitiveOperation1() { 38 | cout << "ConcreteClass2...PrimitiveOperation1" << endl; 39 | } 40 | 41 | void ConcreteClass2::PrimitiveOperation2() { 42 | cout << "ConcreteClass2...PrimitiveOperation2" << endl; 43 | } 44 | -------------------------------------------------------------------------------- /行为模式/Template/Template.h: -------------------------------------------------------------------------------- 1 | #ifndef _TEMPLATE_H_ 2 | #define _TEMPLATE_H_ 3 | 4 | class AbstractClass { 5 | public: 6 | virtual ~AbstractClass(); 7 | void TemplateMethod(); 8 | 9 | protected: 10 | virtual void PrimitiveOperation1() = 0; 11 | virtual void PrimitiveOperation2() = 0; 12 | AbstractClass(); 13 | 14 | private: 15 | }; 16 | 17 | class ConcreteClass1: public AbstractClass { 18 | public: 19 | ConcreteClass1(); 20 | ~ConcreteClass1(); 21 | 22 | protected: 23 | void PrimitiveOperation1(); 24 | void PrimitiveOperation2(); 25 | 26 | private: 27 | }; 28 | 29 | class ConcreteClass2: public AbstractClass { 30 | public: 31 | ConcreteClass2(); 32 | ~ConcreteClass2(); 33 | 34 | protected: 35 | void PrimitiveOperation1(); 36 | void PrimitiveOperation2(); 37 | 38 | private: 39 | }; 40 | 41 | #endif //~_TEMPLATE_H_ 42 | -------------------------------------------------------------------------------- /行为模式/Template/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Template.h" 2 | #include 3 | 4 | using namespace std; 5 | 6 | int main(int argc, char* argv[]) { 7 | AbstractClass* p1 = new ConcreteClass1(); 8 | AbstractClass* p2 = new ConcreteClass2(); 9 | p1->TemplateMethod(); 10 | p2->TemplateMethod(); 11 | return 0; 12 | } 13 | -------------------------------------------------------------------------------- /行为模式/Visitor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ADD_EXECUTABLE(Visitor 2 | main.cpp 3 | Element.cpp 4 | Visitor.cpp 5 | ) 6 | -------------------------------------------------------------------------------- /行为模式/Visitor/Element.cpp: -------------------------------------------------------------------------------- 1 | #include "Element.h" 2 | #include "Visitor.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Element::Element() { 8 | } 9 | 10 | Element::~Element() { 11 | } 12 | 13 | void Element::Accept(Visitor* vis) { 14 | } 15 | 16 | ConcreteElementA::ConcreteElementA() { 17 | } 18 | 19 | ConcreteElementA::~ConcreteElementA() { 20 | } 21 | 22 | void ConcreteElementA::Accept(Visitor* vis) { 23 | vis->VisitConcreteElementA(this); 24 | cout << "visiting ConcreteElementA..." << endl; 25 | } 26 | 27 | ConcreteElementB::ConcreteElementB() { 28 | } 29 | 30 | ConcreteElementB::~ConcreteElementB() { 31 | } 32 | 33 | void ConcreteElementB::Accept(Visitor* vis) { 34 | cout << "visiting ConcreteElementB..." << endl; 35 | vis->VisitConcreteElementB(this); 36 | } 37 | -------------------------------------------------------------------------------- /行为模式/Visitor/Element.h: -------------------------------------------------------------------------------- 1 | #ifndef _ELEMENT_H_ 2 | #define _ELEMENT_H_ 3 | 4 | class Visitor; 5 | 6 | class Element { 7 | public: 8 | virtual ~Element(); 9 | virtual void Accept(Visitor* vis) = 0; 10 | 11 | protected: 12 | Element(); 13 | 14 | private: 15 | }; 16 | 17 | class ConcreteElementA: public Element { 18 | public: 19 | ConcreteElementA(); 20 | ~ConcreteElementA(); 21 | void Accept(Visitor* vis); 22 | 23 | protected: 24 | 25 | private: 26 | }; 27 | 28 | class ConcreteElementB: public Element { 29 | public: 30 | ConcreteElementB(); 31 | ~ConcreteElementB(); 32 | void Accept(Visitor* vis); 33 | 34 | protected: 35 | 36 | private: 37 | }; 38 | 39 | #endif //~_ELEMENT_H_ 40 | -------------------------------------------------------------------------------- /行为模式/Visitor/README: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/fifilyu/the-23-gang-of-four-design-patterns-with-cplusplus/98827524c3ead056417f35b825298d028eff9654/行为模式/Visitor/README -------------------------------------------------------------------------------- /行为模式/Visitor/Visitor.cpp: -------------------------------------------------------------------------------- 1 | #include "Visitor.h" 2 | #include "Element.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | Visitor::Visitor() { 8 | } 9 | 10 | Visitor::~Visitor() { 11 | } 12 | 13 | ConcreteVisitorA::ConcreteVisitorA() { 14 | } 15 | 16 | ConcreteVisitorA::~ConcreteVisitorA() { 17 | } 18 | 19 | void ConcreteVisitorA::VisitConcreteElementA(Element* elm) { 20 | cout << "i will visit ConcreteElementA..." << endl; 21 | } 22 | 23 | void ConcreteVisitorA::VisitConcreteElementB(Element* elm) { 24 | cout << "i will visit ConcreteElementB..." << endl; 25 | } 26 | 27 | ConcreteVisitorB::ConcreteVisitorB() { 28 | } 29 | 30 | ConcreteVisitorB::~ConcreteVisitorB() { 31 | } 32 | 33 | void ConcreteVisitorB::VisitConcreteElementA(Element* elm) { 34 | cout << "i will visit ConcreteElementA..." << endl; 35 | } 36 | 37 | void ConcreteVisitorB::VisitConcreteElementB(Element* elm) { 38 | cout << "i will visit ConcreteElementB..." << endl; 39 | } 40 | -------------------------------------------------------------------------------- /行为模式/Visitor/Visitor.h: -------------------------------------------------------------------------------- 1 | #ifndef _VISITOR_H_ 2 | #define _VISITOR_H_ 3 | 4 | class ConcreteElementA; 5 | 6 | class ConcreteElementB; 7 | 8 | class Element; 9 | 10 | class Visitor { 11 | public: 12 | virtual ~Visitor(); 13 | virtual void VisitConcreteElementA(Element* elm) = 0; 14 | virtual void VisitConcreteElementB(Element* elm) = 0; 15 | 16 | protected: 17 | Visitor(); 18 | 19 | private: 20 | }; 21 | 22 | class ConcreteVisitorA: public Visitor { 23 | public: 24 | ConcreteVisitorA(); 25 | virtual ~ConcreteVisitorA(); 26 | virtual void VisitConcreteElementA(Element* elm); 27 | virtual void VisitConcreteElementB(Element* elm); 28 | 29 | protected: 30 | 31 | private: 32 | }; 33 | 34 | class ConcreteVisitorB: public Visitor { 35 | public: 36 | ConcreteVisitorB(); 37 | virtual ~ConcreteVisitorB(); 38 | virtual void VisitConcreteElementA(Element* elm); 39 | virtual void VisitConcreteElementB(Element* elm); 40 | 41 | protected: 42 | 43 | private: 44 | }; 45 | 46 | #endif //~_VISITOR_H_ 47 | -------------------------------------------------------------------------------- /行为模式/Visitor/main.cpp: -------------------------------------------------------------------------------- 1 | #include "Element.h" 2 | #include "Visitor.h" 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main(int argc, char* argv[]) { 8 | Visitor* vis = new ConcreteVisitorA(); 9 | Element* elm = new ConcreteElementA(); 10 | elm->Accept(vis); 11 | return 0; 12 | } 13 | --------------------------------------------------------------------------------