├── .gitignore ├── AdapterPattern ├── AdapterPattern.pro ├── adaptee.h ├── adapter.h ├── concretetarget.h ├── main.cpp └── target.h ├── BuilderPattern ├── BuilderPattern.pro ├── builder.h ├── concrete_builder.h ├── director.h ├── main.cpp └── product.h ├── ChainResponsibilityPattern ├── ChainResponsibilityPattern.pro ├── concretehandler.h ├── handler.h ├── level.h ├── main.cpp ├── request.h └── response.h ├── CommandPattern ├── CommandPattern.pro ├── command.h ├── concretecommand.h ├── concretereciver.h ├── invoker.h ├── main.cpp └── receiver.h ├── CompositePattern ├── CompositePattern.pro ├── component.h ├── composite.h ├── leaf.h └── main.cpp ├── DecoratorPattern ├── DecoratorPattern.pro ├── component.h ├── concretecomponent.h ├── concretedecorator.h ├── decorator.h └── main.cpp ├── FactoryMethodPattern ├── FactoryMethodPattern.pro ├── concrete_creator.h ├── concrete_product.h ├── creator.h ├── main.cpp └── product.h ├── LICENSE ├── MediatorPattern ├── MediatorPattern.pro ├── colleague.h ├── concretecolleague.h ├── concretemediator.h ├── main.cpp └── mediator.h ├── MementoPattern ├── MementoPattern.pro ├── caretaker.h ├── main.cpp ├── memento.h └── originator.h ├── ObserverPattern ├── ObserverPattern.pro ├── concreteobserver.h ├── concretesubject.h ├── main.cpp ├── observer.h └── subject.h ├── PrototypePattern ├── PrototypePattern.pro ├── concreteprototype.h ├── main.cpp └── prototype.h ├── ProxyPattern ├── ProxyPattern.pro ├── main.cpp ├── proxy.h ├── realsubject.h └── subject.h ├── README.md ├── SingletonPattern ├── SingletonPattern.pro ├── cpp11_sigleton.h ├── main.cpp ├── sigleton.cpp ├── sigleton.h ├── thread_safe_singleton.cpp └── thread_safe_singleton.h ├── StatePattern ├── StatePattern.pro ├── concretestate.h ├── context.cpp ├── context.h ├── main.cpp └── state.h ├── StrategyPattern ├── StrategyPattern.pro ├── concretestrategy.h ├── context.h ├── main.cpp └── strategy.h ├── TemplateMethodPattern ├── TemplateMethodPattern.pro ├── abstract_class.h ├── concrete_class.h └── main.cpp └── VisitorPattern ├── VisitorPattern.pro ├── concreteelement.h ├── element.h ├── ivisitor.h ├── main.cpp ├── objectstruture.h └── visitor.h /.gitignore: -------------------------------------------------------------------------------- 1 | # C++ objects and libs 2 | 3 | *.slo 4 | *.lo 5 | *.o 6 | *.a 7 | *.la 8 | *.lai 9 | *.so 10 | *.dll 11 | *.dylib 12 | 13 | # Qt-es 14 | 15 | /.qmake.cache 16 | /.qmake.stash 17 | *.pro.user 18 | *.pro.user.* 19 | *.qbs.user 20 | *.qbs.user.* 21 | *.moc 22 | moc_*.cpp 23 | moc_*.h 24 | qrc_*.cpp 25 | ui_*.h 26 | Makefile* 27 | *build-* 28 | 29 | # QtCreator 30 | 31 | *.autosave 32 | 33 | # QtCtreator Qml 34 | *.qmlproject.user 35 | *.qmlproject.user.* 36 | 37 | # QtCtreator CMake 38 | CMakeLists.txt.user* 39 | 40 | -------------------------------------------------------------------------------- /AdapterPattern/AdapterPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | target.h \ 21 | concretetarget.h \ 22 | adaptee.h \ 23 | adapter.h 24 | -------------------------------------------------------------------------------- /AdapterPattern/adaptee.h: -------------------------------------------------------------------------------- 1 | #ifndef ADAPTEE_H 2 | #define ADAPTEE_H 3 | #include 4 | class Adaptee { 5 | //原有的业务逻辑 6 | public: 7 | void doSomething(){ 8 | qDebug()<<"I'm kind of busy,leave me alone,pls!"; 9 | } 10 | }; 11 | #endif // ADAPTEE_H 12 | -------------------------------------------------------------------------------- /AdapterPattern/adapter.h: -------------------------------------------------------------------------------- 1 | #ifndef ADAPTER_H 2 | #define ADAPTER_H 3 | #include "adaptee.h" 4 | #include "target.h" 5 | class Adapter : public Adaptee, public Target { 6 | public: 7 | void request() override { 8 | doSomething(); 9 | } 10 | }; 11 | #endif // ADAPTER_H 12 | -------------------------------------------------------------------------------- /AdapterPattern/concretetarget.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETETARGET_H 2 | #define CONCRETETARGET_H 3 | #include 4 | #include "target.h" 5 | class ConcreteTarget : public Target { 6 | public: 7 | void request() override { 8 | qDebug()<<"if you need any help,pls call me!"; 9 | } 10 | }; 11 | #endif // CONCRETETARGET_H 12 | -------------------------------------------------------------------------------- /AdapterPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "adapter.h" 2 | #include "concretetarget.h" 3 | int main(int argc, char *argv[]) { 4 | Target *target = new ConcreteTarget(); 5 | target->request(); 6 | //现在增加了适配器角色后的业务逻辑 7 | Target *target2 = new Adapter(); 8 | target2->request(); 9 | } 10 | -------------------------------------------------------------------------------- /AdapterPattern/target.h: -------------------------------------------------------------------------------- 1 | #ifndef TARGET_H 2 | #define TARGET_H 3 | class Target { 4 | //目标角色有自己的方法 5 | public: 6 | virtual void request() = 0; 7 | }; 8 | #endif // TARGET_H 9 | -------------------------------------------------------------------------------- /BuilderPattern/BuilderPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | product.h \ 21 | builder.h \ 22 | director.h \ 23 | concrete_builder.h 24 | -------------------------------------------------------------------------------- /BuilderPattern/builder.h: -------------------------------------------------------------------------------- 1 | #ifndef BUILDER_H 2 | #define BUILDER_H 3 | #include "product.h" 4 | class Builder { 5 | public: 6 | virtual void setA() = 0; 7 | virtual void setB() = 0; 8 | virtual Product getProduct() = 0; 9 | }; 10 | #endif // BUILDER_H 11 | -------------------------------------------------------------------------------- /BuilderPattern/concrete_builder.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEPRODUCT_H 2 | #define CONCRETEPRODUCT_H 3 | #include "builder.h" 4 | class ConcreteBuilder : public Builder { 5 | public: 6 | virtual void setA() override {product.setA(1);} 7 | virtual void setB() override {product.setB(2);} 8 | Product getProduct() { 9 | return product; 10 | } 11 | private: 12 | Product product; 13 | }; 14 | #endif // CONCRETEPRODUCT_H 15 | -------------------------------------------------------------------------------- /BuilderPattern/director.h: -------------------------------------------------------------------------------- 1 | #ifndef DIRECTOR_H 2 | #define DIRECTOR_H 3 | #include "concrete_builder.h".h" 4 | class Director { 5 | public: 6 | Product getAProduct(Builder *builder){ 7 | builder->setA(); 8 | builder->setB(); 9 | return builder->getProduct(); 10 | } 11 | }; 12 | #endif // DIRECTOR_H 13 | -------------------------------------------------------------------------------- /BuilderPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "director.h" 2 | int main(int argc, char *argv[]) { 3 | Director d; 4 | Product p =d.getAProduct(new ConcreteBuilder()); 5 | p.Show(); 6 | } 7 | -------------------------------------------------------------------------------- /BuilderPattern/product.h: -------------------------------------------------------------------------------- 1 | #ifndef PRODUCT_H 2 | #define PRODUCT_H 3 | #include 4 | class Product { 5 | public: 6 | void setA(int a) {a_ = a;} 7 | void setB(int b) {b_ = b;} 8 | void Show() {qDebug()<getRequestLevel()){ 14 | response = echo(request); 15 | } else { //不属于自己的处理级别 16 | } 17 | //判断是否有下一个处理者 18 | //注意此处如果已经符合了级别并处理完成了消息还会继续向下传 19 | //若只需要当前级别消息被一次处理只需要将下属判断你放到上面的else内 20 | if(nextHandler != nullptr){ 21 | response = nextHandler->handleMessage(request); 22 | } else {//没有其他的处理者 23 | } 24 | return response; 25 | } 26 | //设置下一个处理者是谁 27 | void setNext(Handler *handler) { 28 | nextHandler = handler; 29 | } 30 | protected: 31 | //每个处理者都有一个处理级别 32 | virtual Level getHandlerLevel() = 0; 33 | //每个处理者都必须实现处理任务 34 | virtual Response echo(Request *request) = 0; 35 | }; 36 | #endif // HANDLER_H 37 | -------------------------------------------------------------------------------- /ChainResponsibilityPattern/level.h: -------------------------------------------------------------------------------- 1 | #ifndef LEVEL_H 2 | #define LEVEL_H 3 | enum Level { 4 | Level1 = 0, 5 | Level2, 6 | }; 7 | #endif // LEVEL_H 8 | -------------------------------------------------------------------------------- /ChainResponsibilityPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretehandler.h" 2 | #include "request.h" 3 | #include "response.h" 4 | int main(int argc, char *argv[]) { 5 | //声明所有的处理节点 6 | Handler *handler1 = new ConcreteHandler1(); 7 | Handler *handler2 = new ConcreteHandler2(); 8 | Handler *handler3 = new ConcreteHandler3(); 9 | //设置链中的阶段顺序1-->2-->3 10 | handler1->setNext(handler2); 11 | handler2->setNext(handler3); 12 | //提交请求, 返回结果 13 | Response response = handler1->handleMessage(new Request1()); 14 | response = handler1->handleMessage(new Request2()); 15 | } 16 | -------------------------------------------------------------------------------- /ChainResponsibilityPattern/request.h: -------------------------------------------------------------------------------- 1 | #ifndef REQUEST_H 2 | #define REQUEST_H 3 | #include "level.h" 4 | class Request { 5 | public: 6 | virtual Level getRequestLevel() = 0; 7 | }; 8 | class Request1 : public Request{ 9 | public: 10 | Level getRequestLevel() override { 11 | return Level1; 12 | } 13 | }; 14 | class Request2 : public Request{ 15 | public: 16 | Level getRequestLevel() override { 17 | return Level2; 18 | } 19 | }; 20 | #endif // REQUEST_H 21 | -------------------------------------------------------------------------------- /ChainResponsibilityPattern/response.h: -------------------------------------------------------------------------------- 1 | #ifndef RESPONSE_H 2 | #define RESPONSE_H 3 | #include "QDebug" 4 | class Response { 5 | public: 6 | void text() { 7 | qDebug()<<"get text"; 8 | } 9 | }; 10 | #endif // RESPONSE_H 11 | -------------------------------------------------------------------------------- /CommandPattern/CommandPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | receiver.h \ 21 | concretereciver.h \ 22 | command.h \ 23 | concretecommand.h \ 24 | invoker.h 25 | -------------------------------------------------------------------------------- /CommandPattern/command.h: -------------------------------------------------------------------------------- 1 | #ifndef COMMAND_H 2 | #define COMMAND_H 3 | class Command { 4 | //每个命令类都必须有一个执行命令的方法 5 | public: 6 | virtual void execute() = 0; 7 | }; 8 | #endif // COMMAND_H 9 | -------------------------------------------------------------------------------- /CommandPattern/concretecommand.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETECOMMAND_H 2 | #define CONCRETECOMMAND_H 3 | #include 4 | #include "command.h" 5 | #include "receiver.h" 6 | class ConcreteCommand1 : public Command { 7 | private: 8 | Receiver *receiver_;//哪个Receiver类进行命令处理 9 | public: 10 | //构造函数传递接收者 11 | ConcreteCommand1(Receiver *receiver) { 12 | receiver_ = receiver; 13 | } 14 | //必须实现一个命令 15 | void execute() { 16 | //业务处理 17 | qDebug()<<"command1 run"; 18 | receiver_->doSomething(); 19 | } 20 | }; 21 | class ConcreteCommand2 : public Command { 22 | private: 23 | Receiver *receiver_; 24 | public: 25 | ConcreteCommand2(Receiver *receiver) { 26 | receiver_ = receiver; 27 | } 28 | void execute() { 29 | qDebug()<<"command2 run"; 30 | receiver_->doSomething(); 31 | } 32 | }; 33 | #endif // CONCRETECOMMAND_H 34 | -------------------------------------------------------------------------------- /CommandPattern/concretereciver.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETERECIVER_H 2 | #define CONCRETERECIVER_H 3 | #include 4 | #include "receiver.h" 5 | class ConcreteReciver1 : public Receiver{ 6 | //每个接收者都必须处理一定的业务逻辑 7 | public: 8 | void doSomething(){ 9 | qDebug()<<"Reciver1 doing"; 10 | } 11 | }; 12 | class ConcreteReciver2 : public Receiver{ 13 | //每个接收者都必须处理一定的业务逻辑 14 | public: 15 | void doSomething(){ 16 | qDebug()<<"Reciver2 doing"; 17 | } 18 | }; 19 | #endif // CONCRETERECIVER_H 20 | -------------------------------------------------------------------------------- /CommandPattern/invoker.h: -------------------------------------------------------------------------------- 1 | #ifndef INVOKER_H 2 | #define INVOKER_H 3 | #include 4 | #include "command.h" 5 | class Invoker { 6 | private: 7 | Command *command_; 8 | public: 9 | //接受命令 10 | void setCommand(Command *command) { 11 | command_ = command; 12 | qDebug()<<"invoker add command"; 13 | } 14 | //执行命令 15 | void action(){ 16 | command_->execute(); 17 | qDebug()<<"invoker action command"; 18 | } 19 | }; 20 | #endif // INVOKER_H 21 | -------------------------------------------------------------------------------- /CommandPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretecommand.h" 2 | #include "concretereciver.h" 3 | #include "invoker.h" 4 | int main(int argc, char *argv[]) { 5 | //首先声明调用者Invoker 6 | Invoker *invoker = new Invoker(); 7 | //定义接收者 8 | Receiver *receiver = new ConcreteReciver1(); 9 | Receiver *receiver2 = new ConcreteReciver2(); 10 | //定义一个发送给接收者的命令 11 | Command *command = new ConcreteCommand1(receiver); 12 | Command *command2 = new ConcreteCommand2(receiver); 13 | Command *command3 = new ConcreteCommand1(receiver2); 14 | Command *command4 = new ConcreteCommand2(receiver2); 15 | //把命令交给调用者去执行 16 | invoker->setCommand(command); 17 | invoker->action(); 18 | invoker->setCommand(command2); 19 | invoker->action(); 20 | invoker->setCommand(command3); 21 | invoker->action(); 22 | invoker->setCommand(command4); 23 | invoker->action(); 24 | } 25 | -------------------------------------------------------------------------------- /CommandPattern/receiver.h: -------------------------------------------------------------------------------- 1 | #ifndef RECEIVER_H 2 | #define RECEIVER_H 3 | class Receiver { 4 | //抽象接收者, 定义每个接收者都必须完成的业务 5 | public: 6 | virtual ~Receiver() { } 7 | virtual void doSomething() = 0; 8 | }; 9 | #endif // RECEIVER_H 10 | -------------------------------------------------------------------------------- /CompositePattern/CompositePattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | component.h \ 21 | composite.h \ 22 | leaf.h 23 | -------------------------------------------------------------------------------- /CompositePattern/component.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPONENT_H 2 | #define COMPONENT_H 3 | class Component { 4 | //个体和整体都具有的共享 5 | public: 6 | virtual ~Component() { } 7 | virtual void doSomething() = 0; 8 | }; 9 | #endif // COMPONENT_H 10 | -------------------------------------------------------------------------------- /CompositePattern/composite.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPOSITE_H 2 | #define COMPOSITE_H 3 | #include 4 | #include "component.h" 5 | class Composite : public Component { 6 | public: 7 | ~Composite() { 8 | for(auto component : componentArrayList) { 9 | delete component; 10 | } 11 | } 12 | void doSomething() override { } 13 | //增加一个叶子构件或树枝构件 14 | void add(Component *component){ 15 | componentArrayList.append(component); 16 | } 17 | //删除一个叶子构件或树枝构件 18 | void remove(Component *component){ 19 | delete component; 20 | componentArrayList.removeOne(component); 21 | } 22 | //获得分支下的所有叶子构件和树枝构件 23 | QList getChildren(){ 24 | return componentArrayList; 25 | } 26 | //构件容器 27 | private: 28 | QList componentArrayList; 29 | }; 30 | #endif // COMPOSITE_H 31 | -------------------------------------------------------------------------------- /CompositePattern/leaf.h: -------------------------------------------------------------------------------- 1 | #ifndef LEAF_H 2 | #define LEAF_H 3 | #include "component.h" 4 | class Leaf : public Component { 5 | public: 6 | void doSomething() override { } 7 | }; 8 | #endif // LEAF_H 9 | -------------------------------------------------------------------------------- /CompositePattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "composite.h" 2 | #include "leaf.h" 3 | int main(int argc, char *argv[]) { 4 | Composite *root = new Composite(); 5 | root->doSomething(); 6 | //创建一个树枝构件 7 | Composite *branch = new Composite(); 8 | //创建一个叶子节点 9 | Leaf *leaf = new Leaf(); 10 | //建立整体 11 | root->add(branch); 12 | branch->add(leaf); 13 | //父节点析构会删除子节点 14 | delete root; 15 | } 16 | -------------------------------------------------------------------------------- /DecoratorPattern/DecoratorPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | component.h \ 21 | concretecomponent.h \ 22 | decorator.h \ 23 | concretedecorator.h 24 | -------------------------------------------------------------------------------- /DecoratorPattern/component.h: -------------------------------------------------------------------------------- 1 | #ifndef COMPONENT_H 2 | #define COMPONENT_H 3 | class Component { 4 | //抽象的构件 5 | public: 6 | virtual void operate() = 0; 7 | }; 8 | #endif // COMPONENT_H 9 | -------------------------------------------------------------------------------- /DecoratorPattern/concretecomponent.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETECOMPONENT_H 2 | #define CONCRETECOMPONENT_H 3 | #include "component.h" 4 | #include 5 | class ConcreteComponent : public Component { 6 | public: 7 | void operate() override { 8 | qDebug()<<"ConcreteComponent do Something"; 9 | } 10 | }; 11 | #endif // CONCRETECOMPONENT_H 12 | -------------------------------------------------------------------------------- /DecoratorPattern/concretedecorator.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEDECORATOR_H 2 | #define CONCRETEDECORATOR_H 3 | #include 4 | #include "decorator.h" 5 | class ConcreteDecorator1 : public Decorator { 6 | public: 7 | ConcreteDecorator1(Component *component) 8 | : Decorator(component) { 9 | } 10 | //重写父类的Operation方法 11 | void operate() override { 12 | method1(); 13 | Decorator::operate(); 14 | } 15 | private: 16 | //定义自己的修饰方法 17 | void method1(){ 18 | qDebug()<<"method1 of Decorator1"; 19 | } 20 | }; 21 | class ConcreteDecorator2 : public Decorator { 22 | public: 23 | ConcreteDecorator2(Component *component) 24 | : Decorator(component) { 25 | } 26 | //重写父类的Operation方法 27 | void operate() override { 28 | method1(); 29 | Decorator::operate(); 30 | } 31 | private: 32 | //定义自己的修饰方法 33 | void method1(){ 34 | qDebug()<<"method1 of Decorator2"; 35 | } 36 | }; 37 | #endif // CONCRETEDECORATOR_H 38 | -------------------------------------------------------------------------------- /DecoratorPattern/decorator.h: -------------------------------------------------------------------------------- 1 | #ifndef DECORATOR_H 2 | #define DECORATOR_H 3 | #include "component.h" 4 | class Decorator : public Component { 5 | //通过构造函数传递被修饰者 6 | public: 7 | Decorator(Component *component){ 8 | component_ = component; 9 | } 10 | //委托给被修饰者执行 11 | void operate() override { 12 | component_->operate(); 13 | } 14 | private: 15 | Component *component_ = nullptr; 16 | }; 17 | #endif // DECORATOR_H 18 | -------------------------------------------------------------------------------- /DecoratorPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretecomponent.h" 2 | #include "concretedecorator.h" 3 | int main(int argc, char *argv[]) { 4 | Component *component = new ConcreteComponent(); 5 | //第一次修饰 6 | component = new ConcreteDecorator1(component); 7 | //第二次修饰 8 | component = new ConcreteDecorator2(component); 9 | //修饰后运行 10 | component->operate(); 11 | } 12 | -------------------------------------------------------------------------------- /FactoryMethodPattern/FactoryMethodPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | product.h \ 21 | concrete_product.h \ 22 | creator.h \ 23 | concrete_creator.h 24 | -------------------------------------------------------------------------------- /FactoryMethodPattern/concrete_creator.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETE_CREATOR_H 2 | #define CONCRETE_CREATOR_H 3 | #include "creator.h" 4 | #include "concrete_product.h" 5 | //下面把多个工厂写到一起了,应该分开 6 | class ConcreteCreator1 : public Creator { 7 | public: 8 | virtual Product *Create() override { 9 | auto product = new ConcreteProduct1(); 10 | product->Init();//工厂负责完成初始化过程 11 | //返回一个可直接使用的产品 12 | return product; 13 | } 14 | }; 15 | class ConcreteCreator2 : public Creator { 16 | public: 17 | virtual Product *Create() override { 18 | auto product = new ConcreteProduct2(); 19 | product->Init(); 20 | return product; 21 | } 22 | }; 23 | class ConcreteCreator3 : public Creator { 24 | public: 25 | virtual Product *Create() override { 26 | auto product = new ConcreteProduct3(); 27 | product->Init(); 28 | return product; 29 | } 30 | }; 31 | #endif // CONCRETE_CREATOR_H 32 | -------------------------------------------------------------------------------- /FactoryMethodPattern/concrete_product.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETE_PRODUCT_H 2 | #define CONCRETE_PRODUCT_H 3 | #include "product.h" 4 | //下面把多个产品写到一起了,应该分开 5 | class ConcreteProduct1 : public Product { 6 | public: 7 | virtual void Init() override { } 8 | virtual int Number() override { 9 | return 1; 10 | } 11 | }; 12 | class ConcreteProduct2 : public Product { 13 | public: 14 | virtual void Init() override { } 15 | virtual int Number() override { 16 | return 2; 17 | } 18 | }; 19 | class ConcreteProduct3 : public Product { 20 | public: 21 | virtual void Init() override { } 22 | virtual int Number() override { 23 | return 3; 24 | } 25 | }; 26 | #endif // CONCRETE_PRODUCT_H 27 | -------------------------------------------------------------------------------- /FactoryMethodPattern/creator.h: -------------------------------------------------------------------------------- 1 | #ifndef CREATOR_H 2 | #define CREATOR_H 3 | #include "product.h" 4 | //抽象工厂 5 | //若工厂则直接传入标识符通过case判断返回对应产品即可,不需要建立不同的具体工厂 6 | class Creator { 7 | public: 8 | virtual ~Creator() { }; 9 | virtual Product *Create() = 0; 10 | }; 11 | #endif // CREATOR_H 12 | -------------------------------------------------------------------------------- /FactoryMethodPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | //包含某一个具体工厂头文件即可 3 | #include "concrete_creator.h" 4 | int main(int argc, char *argv[]) { 5 | Creator *c1 = new ConcreteCreator1(); 6 | Product *p1 = c1->Create(); 7 | Creator *c2 = new ConcreteCreator2(); 8 | Product *p2 = c2->Create(); 9 | Creator *c3 = new ConcreteCreator3(); 10 | Product *p3 = c3->Create(); 11 | qDebug()<Number(); 12 | qDebug()<Number(); 13 | qDebug()<Number(); 14 | delete c1; 15 | delete c2; 16 | delete c3; 17 | delete p1; 18 | delete p2; 19 | delete p3; 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /FactoryMethodPattern/product.h: -------------------------------------------------------------------------------- 1 | #ifndef PRODUCT_H 2 | #define PRODUCT_H 3 | class Product { 4 | public: 5 | virtual ~Product() { }; 6 | virtual void Init() = 0; 7 | virtual int Number() = 0; 8 | }; 9 | #endif // PRODUCT_H 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Techie Liang 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 | -------------------------------------------------------------------------------- /MediatorPattern/MediatorPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | mediator.h \ 21 | concretemediator.h \ 22 | colleague.h \ 23 | concretecolleague.h 24 | -------------------------------------------------------------------------------- /MediatorPattern/colleague.h: -------------------------------------------------------------------------------- 1 | #ifndef COLLEAGUE_H 2 | #define COLLEAGUE_H 3 | class Colleague { 4 | public: 5 | virtual void depMethod() = 0; 6 | virtual void selfMethod() = 0; 7 | }; 8 | #endif // COLLEAGUE_H 9 | -------------------------------------------------------------------------------- /MediatorPattern/concretecolleague.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETECOLLEAGUE_H 2 | #define CONCRETECOLLEAGUE_H 3 | #include 4 | #include "mediator.h" 5 | #include "colleague.h" 6 | class ConcreteColleague1 : public Colleague { 7 | public: 8 | //通过构造函数传递中介者 9 | ConcreteColleague1(Mediator *mediator) { 10 | mediator_ = mediator; 11 | } 12 | //自有方法 self-method 13 | void selfMethod() override { 14 | qDebug()<<"c1 get mediator message"; 15 | } 16 | //依赖方法 dep-method 17 | void depMethod() override { 18 | qDebug()<<"c1 send message"; 19 | mediator_->doSomething1(); 20 | } 21 | private: 22 | Mediator *mediator_; 23 | }; 24 | class ConcreteColleague2 : public Colleague { 25 | //通过构造函数传递中介者 26 | public: 27 | ConcreteColleague2(Mediator *mediator) { 28 | mediator_ = mediator; 29 | } 30 | void selfMethod() override { 31 | qDebug()<<"c2 get mediator message"; 32 | } 33 | void depMethod() override { 34 | qDebug()<<"c2 send message"; 35 | mediator_->doSomething2(); 36 | } 37 | private: 38 | Mediator *mediator_; 39 | }; 40 | #endif // CONCRETECOLLEAGUE_H 41 | -------------------------------------------------------------------------------- /MediatorPattern/concretemediator.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEMEDIATOR_H 2 | #define CONCRETEMEDIATOR_H 3 | #include 4 | #include "mediator.h" 5 | class ConcreteMediator : public Mediator { 6 | public: 7 | void setC1(Colleague *c1) override { 8 | c1_ = c1; 9 | } 10 | void setC2(Colleague *c2) override { 11 | c2_ = c2; 12 | } 13 | void doSomething1() override { 14 | qDebug()<<"mediator get c1 sended message"; 15 | c2_->selfMethod(); 16 | } 17 | void doSomething2() override { 18 | qDebug()<<"mediator get c2 sended message"; 19 | c1_->selfMethod(); 20 | } 21 | private: 22 | Colleague *c1_; 23 | Colleague *c2_; 24 | }; 25 | #endif // CONCRETEMEDIATOR_H 26 | -------------------------------------------------------------------------------- /MediatorPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretecolleague.h" 2 | #include "concretemediator.h" 3 | int main(int argc, char *argv[]) { 4 | //建立中介,完成联系 5 | Mediator *m = new ConcreteMediator; 6 | Colleague *c1 = new ConcreteColleague1(m); 7 | Colleague *c2 = new ConcreteColleague2(m); 8 | m->setC1(c1); 9 | m->setC2(c2); 10 | c1->depMethod(); 11 | c2->depMethod(); 12 | } 13 | -------------------------------------------------------------------------------- /MediatorPattern/mediator.h: -------------------------------------------------------------------------------- 1 | #ifndef MEDIATOR_H 2 | #define MEDIATOR_H 3 | #include "colleague.h" 4 | class Mediator { 5 | public: 6 | virtual void setC1(Colleague *c1) = 0; 7 | virtual void setC2(Colleague *c2) = 0; 8 | virtual void doSomething1() = 0; 9 | virtual void doSomething2() = 0; 10 | }; 11 | #endif // MEDIATOR_H 12 | -------------------------------------------------------------------------------- /MementoPattern/MementoPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | memento.h \ 21 | caretaker.h \ 22 | originator.h 23 | -------------------------------------------------------------------------------- /MementoPattern/caretaker.h: -------------------------------------------------------------------------------- 1 | #ifndef CARETAKER_H 2 | #define CARETAKER_H 3 | #include "memento.h" 4 | class Caretaker { 5 | public: 6 | Memento *getMemento() { 7 | return memento_; 8 | } 9 | void setMemento(Memento *memento) { 10 | memento_ = memento; 11 | } 12 | //备忘录对象 13 | private: 14 | Memento *memento_; 15 | }; 16 | #endif // CARETAKER_H 17 | -------------------------------------------------------------------------------- /MementoPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "caretaker.h" 2 | #include "originator.h" 3 | int main(int argc, char *argv[]) { 4 | //定义出发起人 5 | Originator *originator = new Originator(); 6 | //定义出备忘录管理员 7 | Caretaker *caretaker = new Caretaker(); 8 | //创建一个备忘录 9 | caretaker->setMemento(originator->createMemento()); 10 | //恢复一个备忘录 11 | originator->restoreMemento(caretaker->getMemento()); 12 | } 13 | -------------------------------------------------------------------------------- /MementoPattern/memento.h: -------------------------------------------------------------------------------- 1 | #ifndef MEMENTO_H 2 | #define MEMENTO_H 3 | #include 4 | class Memento { 5 | //发起人的内部状态 6 | private: 7 | QString state_ = ""; 8 | //构造函数传递参数 9 | public: 10 | Memento(QString state){ 11 | state_ = state; 12 | } 13 | QString getState() { 14 | return state_; 15 | } 16 | void setState(QString state) { 17 | state_ = state; 18 | } 19 | }; 20 | #endif // MEMENTO_H 21 | -------------------------------------------------------------------------------- /MementoPattern/originator.h: -------------------------------------------------------------------------------- 1 | #ifndef ORIGINATOR_H 2 | #define ORIGINATOR_H 3 | #include "memento.h" 4 | class Originator { 5 | public: 6 | QString getState() { 7 | return state_; 8 | } 9 | void setState(QString state) { 10 | state_ = state; 11 | } 12 | //创建一个备忘录 13 | Memento* createMemento(){ 14 | return new Memento(state_); 15 | } 16 | //恢复一个备忘录 17 | void restoreMemento(Memento *_memento){ 18 | setState(_memento->getState()); 19 | } 20 | //内部状态 21 | private: 22 | QString state_ = ""; 23 | }; 24 | #endif // ORIGINATOR_H 25 | -------------------------------------------------------------------------------- /ObserverPattern/ObserverPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | subject.h \ 21 | concretesubject.h \ 22 | observer.h \ 23 | concreteobserver.h 24 | -------------------------------------------------------------------------------- /ObserverPattern/concreteobserver.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEOBSERVER_H 2 | #define CONCRETEOBSERVER_H 3 | #include 4 | #include "observer.h" 5 | class ConcreteObserver : public Observer { 6 | //实现更新方法 7 | public: 8 | void update() override { 9 | qDebug()<<"get message"; 10 | } 11 | }; 12 | #endif // CONCRETEOBSERVER_H 13 | -------------------------------------------------------------------------------- /ObserverPattern/concretesubject.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETESUBJECT_H 2 | #define CONCRETESUBJECT_H 3 | #include "subject.h" 4 | class ConcreteSubject : public Subject { 5 | //具体的业务 6 | public: 7 | void doSomething() { 8 | notifyObservers(); 9 | } 10 | }; 11 | #endif // CONCRETESUBJECT_H 12 | -------------------------------------------------------------------------------- /ObserverPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concreteobserver.h" 2 | #include "concretesubject.h" 3 | int main(int argc, char *argv[]) { 4 | ConcreteSubject *subject = new ConcreteSubject(); 5 | //定义一个观察者 6 | Observer *obs= new ConcreteObserver(); 7 | //观察者观察被观察者 8 | subject->addObserver(obs); 9 | //观察者开始活动了 10 | subject->doSomething(); 11 | delete subject; 12 | delete obs; 13 | } 14 | -------------------------------------------------------------------------------- /ObserverPattern/observer.h: -------------------------------------------------------------------------------- 1 | #ifndef OBSERVER_H 2 | #define OBSERVER_H 3 | class Observer { 4 | //更新方法 5 | public: 6 | virtual ~Observer() { } 7 | virtual void update() = 0; 8 | }; 9 | #endif // OBSERVER_H 10 | -------------------------------------------------------------------------------- /ObserverPattern/subject.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBJECT_H 2 | #define SUBJECT_H 3 | #include "observer.h" 4 | #include 5 | class Subject { 6 | public: 7 | //增加一个观察者 8 | void addObserver(Observer *o) { 9 | obsVector.append(o); 10 | } 11 | //删除一个观察者 12 | void delObserver(Observer *o) { 13 | obsVector.removeOne(o); 14 | } 15 | //通知所有观察者 16 | void notifyObservers() { 17 | for(Observer *o : obsVector) 18 | o->update(); 19 | } 20 | private: 21 | //定义一个观察者数组 22 | QList obsVector; 23 | }; 24 | #endif // SUBJECT_H 25 | -------------------------------------------------------------------------------- /PrototypePattern/PrototypePattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | prototype.h \ 21 | concreteprototype.h 22 | -------------------------------------------------------------------------------- /PrototypePattern/concreteprototype.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEPROTOTYPE_H 2 | #define CONCRETEPROTOTYPE_H 3 | #include "prototype.h" 4 | class ConcretePrototype : public Prototype { 5 | public: 6 | ConcretePrototype() {b = new int;} 7 | virtual ~ConcretePrototype() {delete b;} 8 | virtual Prototype* Clone() override { 9 | auto r = new ConcretePrototype; 10 | r->a = this->a; 11 | //b在构造函数会new,不需要拷贝 12 | return r; 13 | } 14 | private: 15 | int a; 16 | int *b; 17 | }; 18 | #endif // CONCRETEPROTOTYPE_H 19 | -------------------------------------------------------------------------------- /PrototypePattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concreteprototype.h" 2 | int main(int argc, char *argv[]) { 3 | Prototype *p = new ConcretePrototype(); 4 | Prototype *p2 = p->Clone(); 5 | } 6 | -------------------------------------------------------------------------------- /PrototypePattern/prototype.h: -------------------------------------------------------------------------------- 1 | #ifndef PROTOTYPE_H 2 | #define PROTOTYPE_H 3 | class Prototype { 4 | public: 5 | virtual ~Prototype() { } 6 | virtual Prototype* Clone() = 0; 7 | }; 8 | #endif // PROTOTYPE_H 9 | -------------------------------------------------------------------------------- /ProxyPattern/ProxyPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | subject.h \ 21 | realsubject.h \ 22 | proxy.h 23 | -------------------------------------------------------------------------------- /ProxyPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "realsubject.h" 2 | #include "proxy.h" 3 | int main(int argc, char *argv[]) { 4 | Proxy p; 5 | p.request(10); 6 | p.request(110); 7 | } 8 | -------------------------------------------------------------------------------- /ProxyPattern/proxy.h: -------------------------------------------------------------------------------- 1 | #ifndef PROXY_H 2 | #define PROXY_H 3 | #include 4 | #include "subject.h" 5 | #include "realsubject.h" 6 | class Proxy : public Subject { 7 | public: 8 | //支持自定义具体场景 9 | Proxy(Subject *subject = nullptr){ 10 | if(subject != nullptr) 11 | subject_ = subject; 12 | else 13 | subject_ = new RealSubject; 14 | } 15 | //在原有逻辑外代理对num做了过滤,筛掉了<=100的值 16 | void request(int num) { 17 | if(num > 100) 18 | subject_->request(num); 19 | else 20 | qDebug()<<"not good num"; 21 | } 22 | private: 23 | Subject *subject_ = nullptr; 24 | }; 25 | #endif // PROXY_H 26 | -------------------------------------------------------------------------------- /ProxyPattern/realsubject.h: -------------------------------------------------------------------------------- 1 | #ifndef REALSUBJECT_H 2 | #define REALSUBJECT_H 3 | #include "subject.h" 4 | #include 5 | class RealSubject : public Subject { 6 | public: 7 | void request(int num) override {qDebug()<<"hi";} 8 | }; 9 | #endif // REALSUBJECT_H 10 | -------------------------------------------------------------------------------- /ProxyPattern/subject.h: -------------------------------------------------------------------------------- 1 | #ifndef SUBJECT_H 2 | #define SUBJECT_H 3 | class Subject { 4 | public: 5 | virtual ~Subject() { } 6 | virtual void request(int num) = 0; 7 | }; 8 | #endif // SUBJECT_H 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CppDesignPattern 2 | C++设计模式实现,Qt项目 3 | * Blog:[http://techieliang.com](http://techieliang.com/2017/12/764/) 4 | * 范例在main文件测试完毕后大部分都忘了delete,留下这戏错误提醒以后不要再犯。。。使用各种模式时请根据情况管理指针 5 | 6 | ## SingletonPattern 7 | 单例模式 8 | * Blog:[C++设计模式-单例模式](http://techieliang.com/2017/12/772/) 9 | 10 | ## FactoryMethodPattern 11 | 工厂方法模式/抽象工厂模式 12 | * Blog:[C++设计模式-工厂方法模式/抽象工厂模式](http://techieliang.com/2017/12/775/) 13 | 14 | ## TemplateMethodPattern 15 | 模板方法模式 16 | * Blog:[C++设计模式-模板方法模式](http://techieliang.com/2017/12/790/) 17 | 18 | ## BuilderPattern 19 | 建造者模式 20 | * Blog:[C++设计模式-建造者模式](http://techieliang.com/2017/12/794/) 21 | 22 | ## PrototypePattern 23 | 原型模式 24 | * Blog:[C++设计模式-原型模式](http://techieliang.com/2017/12/799/) 25 | 26 | ## ProxyPattern 27 | 代理模式 28 | * Blog:[C++设计模式-代理模式](http://techieliang.com/2017/12/802/) 29 | 30 | ## MediatorPattern 31 | 中介者模式 32 | * Blog:[C++设计模式-中介者模式](http://techieliang.com/2017/12/806/) 33 | 34 | ## CommandPattern 35 | 命令模式 36 | * Blog:[C++设计模式-命令模式](http://techieliang.com/2017/12/808/) 37 | 38 | ## ChainResponsibilityPattern 39 | 责任链模式 40 | * Blog:[C++设计模式-责任链模式](http://techieliang.com/2017/12/811/) 41 | 42 | ## DecoratorPattern 43 | 装饰模式 44 | * Blog:[C++设计模式-装饰模式](http://techieliang.com/2017/12/815/) 45 | 46 | ## StrategyPattern 47 | 策略模式 48 | * Blog:[C++设计模式-策略模式](http://techieliang.com/2017/12/819/) 49 | 50 | ## AdapterPattern 51 | 适配器模式 52 | * Blog:[C++设计模式-适配器模式](http://techieliang.com/2017/12/821/) 53 | 54 | ## CompositePattern 55 | 组合模式 56 | * Blog:[C++设计模式-组合模式](http://techieliang.com/2017/12/826/) 57 | 58 | ## ObserverPattern 59 | 观察者模式 60 | * Blog:[C++设计模式-观察者模式](http://techieliang.com/2017/12/829/) 61 | 62 | ## FacadePattern 63 | 门面模式,比较简单,没做范例 64 | * Blog:[C++设计模式-门面模式](http://techieliang.com/2017/12/831/) 65 | 66 | ## MementoPattern 67 | 备忘录模式 68 | * Blog:[C++设计模式-备忘录模式](http://techieliang.com/2017/12/835/) 69 | 70 | ## VisitorPattern 71 | 访问者模式 72 | * Blog:[C++设计模式-访问者模式](http://techieliang.com/2017/12/838/) 73 | 74 | 75 | ## StatePattern 76 | 状态模式 77 | * Blog:[C++设计模式-状态模式](http://techieliang.com/2017/12/840/) 78 | -------------------------------------------------------------------------------- /SingletonPattern/SingletonPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp \ 18 | sigleton.cpp \ 19 | thread_safe_singleton.cpp 20 | 21 | HEADERS += \ 22 | sigleton.h \ 23 | thread_safe_singleton.h \ 24 | cpp11_sigleton.h 25 | -------------------------------------------------------------------------------- /SingletonPattern/cpp11_sigleton.h: -------------------------------------------------------------------------------- 1 | #ifndef CPP11_SIGLETON_H 2 | #define CPP11_SIGLETON_H 3 | #include 4 | //C++11的单例实现 5 | //c++11自动处理静态成员初始化竞争,是线程安全的 6 | template 7 | class Cpp11Sigleton { 8 | public: 9 | static T *GetInstance() { 10 | static T instance; 11 | return &instance; 12 | } 13 | }; 14 | class TestCpp11Sigleton { 15 | friend class Cpp11Sigleton; 16 | private: 17 | TestCpp11Sigleton(){qDebug()<<"created"< 2 | #include 3 | int main(int argc, char *argv[]) { 4 | std::thread t1(Cpp11Sigleton::GetInstance); 5 | t1.detach(); 6 | std::thread t2(Cpp11Sigleton::GetInstance); 7 | t2.detach(); 8 | std::thread t3(Cpp11Sigleton::GetInstance); 9 | t3.detach(); 10 | std::thread t4(Cpp11Sigleton::GetInstance); 11 | t4.detach(); 12 | Cpp11Sigleton::GetInstance(); 13 | Cpp11Sigleton::GetInstance(); 14 | Cpp11Sigleton::GetInstance(); 15 | Cpp11Sigleton::GetInstance(); 16 | } 17 | -------------------------------------------------------------------------------- /SingletonPattern/sigleton.cpp: -------------------------------------------------------------------------------- 1 | #include "sigleton.h" 2 | Singleton* Singleton::m_instance = nullptr; 3 | Singleton *Singleton::GetInstance() { 4 | if (m_instance == nullptr) { 5 | m_instance = new Singleton(); 6 | } 7 | return m_instance; 8 | } 9 | 10 | void Singleton::DestoryInstance() { 11 | if (m_instance != nullptr) { 12 | delete m_instance; 13 | m_instance = nullptr; 14 | } 15 | } 16 | 17 | Singleton::Singleton() { 18 | 19 | } 20 | -------------------------------------------------------------------------------- /SingletonPattern/sigleton.h: -------------------------------------------------------------------------------- 1 | #ifndef SIGLETON_H 2 | #define SIGLETON_H 3 | /** 4 | * @brief 非线程安全单例,无多线程时使用 5 | */ 6 | class Singleton { 7 | public: 8 | /** 9 | * @brief 单例模式,获取实例化对象 10 | * @param 无 11 | * @return 单例对象 12 | */ 13 | static Singleton *GetInstance(); 14 | /** 15 | * @brief 单例模式,主动销毁实例化对象 16 | * @param 无 17 | * @return 无 18 | */ 19 | static void DestoryInstance(); 20 | private: 21 | /** 22 | * @brief 构造函数 23 | */ 24 | Singleton(); 25 | /** 26 | * @brief 单例模式在程序结束时自动删除单例的方法 27 | */ 28 | class SingletonDel { 29 | public: 30 | ~SingletonDel() { 31 | if (m_instance != nullptr) { 32 | delete m_instance; 33 | m_instance = nullptr; 34 | } 35 | } 36 | }; 37 | static SingletonDel m_singleton_del;///程序结束时销毁 38 | static Singleton *m_instance; //单例对象指针 39 | }; 40 | #endif // SIGLETON_H 41 | -------------------------------------------------------------------------------- /SingletonPattern/thread_safe_singleton.cpp: -------------------------------------------------------------------------------- 1 | #include "thread_safe_singleton.h" 2 | #include 3 | #include 4 | namespace thread_safe_singleton_private { 5 | static QMutex mutex; 6 | } 7 | ThreadSafeSingleton* ThreadSafeSingleton::m_instance = nullptr; 8 | ThreadSafeSingleton *ThreadSafeSingleton::GetInstance() { 9 | if (m_instance == nullptr) { 10 | QMutexLocker lock(&thread_safe_singleton_private::mutex); 11 | if (m_instance == nullptr) { 12 | m_instance = new ThreadSafeSingleton(); 13 | } 14 | } 15 | return m_instance; 16 | } 17 | 18 | void ThreadSafeSingleton::DestoryInstance() { 19 | if (m_instance != nullptr) { 20 | delete m_instance; 21 | m_instance = nullptr; 22 | } 23 | } 24 | 25 | ThreadSafeSingleton::ThreadSafeSingleton() { 26 | 27 | } 28 | -------------------------------------------------------------------------------- /SingletonPattern/thread_safe_singleton.h: -------------------------------------------------------------------------------- 1 | #ifndef THREAD_SAFE_SINGLETON_H 2 | #define THREAD_SAFE_SINGLETON_H 3 | /** 4 | * @brief 线程安全单例,多线程时使用 5 | */ 6 | class ThreadSafeSingleton { 7 | public: 8 | /** 9 | * @brief 单例模式,获取实例化对象 10 | * @param 无 11 | * @return 单例对象 12 | */ 13 | static ThreadSafeSingleton* GetInstance(); 14 | /** 15 | * @brief 单例模式,主动销毁实例化对象 16 | * @param 无 17 | * @return 无 18 | */ 19 | static void DestoryInstance(); 20 | private: 21 | /** 22 | * @brief 构造函数 23 | */ 24 | ThreadSafeSingleton(); 25 | /** 26 | * @brief 单例模式在程序结束时自动删除单例的方法 27 | */ 28 | class SingletonDel { 29 | public: 30 | ~SingletonDel() { 31 | if (m_instance != nullptr) { 32 | delete m_instance; 33 | m_instance = nullptr; 34 | } 35 | } 36 | }; 37 | static ThreadSafeSingleton m_singleton_del;///程序结束时销毁 38 | static ThreadSafeSingleton *m_instance; //单例对象指针 39 | }; 40 | #endif // THREAD_SAFE_SINGLETON_H 41 | -------------------------------------------------------------------------------- /StatePattern/StatePattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp \ 18 | context.cpp 19 | 20 | HEADERS += \ 21 | state.h \ 22 | concretestate.h \ 23 | context.h 24 | -------------------------------------------------------------------------------- /StatePattern/concretestate.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETESTATE_H 2 | #define CONCRETESTATE_H 3 | #include "state.h" 4 | #include "context.h" 5 | class ConcreteState1 : public State { 6 | public: 7 | void handle1() override { 8 | //本状态下必须处理的逻辑 9 | } 10 | void handle2() override { 11 | //设置当前状态为stat2 12 | context->setCurrentState(Context::STATE2); 13 | //过渡到state2状态, 由Context实现 14 | context->handle2(); 15 | } 16 | }; 17 | class ConcreteState2 : public State { 18 | public: 19 | void handle1() override { 20 | //设置当前状态为stat2 21 | context->setCurrentState(Context::STATE1); 22 | //过渡到state2状态, 由Context实现 23 | context->handle1(); 24 | } 25 | void handle2() override { 26 | 27 | } 28 | }; 29 | #endif // CONCRETESTATE_H 30 | -------------------------------------------------------------------------------- /StatePattern/context.cpp: -------------------------------------------------------------------------------- 1 | #include "context.h" 2 | #include "concretestate.h" 3 | State * Context::STATE1 = new ConcreteState1(); 4 | State * Context::STATE2 = new ConcreteState2(); 5 | -------------------------------------------------------------------------------- /StatePattern/context.h: -------------------------------------------------------------------------------- 1 | #ifndef CONTEXT_H 2 | #define CONTEXT_H 3 | #include "state.h" 4 | class Context { 5 | //定义状态 6 | public: 7 | static State *STATE1; 8 | static State *STATE2; 9 | //当前状态 10 | //获得当前状态 11 | State *getCurrentState() { 12 | return CurrentState; 13 | } 14 | //设置当前状态 15 | void setCurrentState(State *currentState) { 16 | CurrentState = currentState; 17 | CurrentState->setContext(this); 18 | } 19 | //行为委托 20 | void handle1(){ 21 | CurrentState->handle1(); 22 | } 23 | void handle2(){ 24 | CurrentState->handle2(); 25 | } 26 | private: 27 | State *CurrentState; 28 | }; 29 | #endif // CONTEXT_H 30 | -------------------------------------------------------------------------------- /StatePattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretestate.h" 2 | #include "context.h" 3 | int main(int argc, char *argv[]) { 4 | Context *context = new Context(); 5 | //初始化状态 6 | context->setCurrentState(new ConcreteState1()); 7 | //行为执行 8 | context->handle1(); 9 | context->handle2(); 10 | } 11 | -------------------------------------------------------------------------------- /StatePattern/state.h: -------------------------------------------------------------------------------- 1 | #ifndef STATE_H 2 | #define STATE_H 3 | class Context; 4 | class State { 5 | //设置环境角色 6 | public: 7 | void setContext(Context *_context){ 8 | context = _context; 9 | } 10 | //行为1 11 | virtual void handle1() = 0; 12 | //行为2 13 | virtual void handle2() = 0; 14 | //定义一个环境角色, 提供子类访问 15 | protected: 16 | Context *context; 17 | }; 18 | #endif // STATE_H 19 | -------------------------------------------------------------------------------- /StrategyPattern/StrategyPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | strategy.h \ 21 | concretestrategy.h \ 22 | context.h 23 | -------------------------------------------------------------------------------- /StrategyPattern/concretestrategy.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETESTRATEGY_H 2 | #define CONCRETESTRATEGY_H 3 | #include 4 | #include "strategy.h" 5 | class ConcreteStrategy1 : public Strategy { 6 | public: 7 | void doSomething() override { 8 | qDebug()<<"ConcreteStrategy1 doSomething"; 9 | } 10 | }; 11 | class ConcreteStrategy2 : public Strategy { 12 | public: 13 | void doSomething() override { 14 | qDebug()<<"ConcreteStrategy2 doSomething"; 15 | } 16 | }; 17 | #endif // CONCRETESTRATEGY_H 18 | -------------------------------------------------------------------------------- /StrategyPattern/context.h: -------------------------------------------------------------------------------- 1 | #ifndef CONTEXT_H 2 | #define CONTEXT_H 3 | #include "strategy.h" 4 | class Context { 5 | public: 6 | //构造函数设置具体策略 7 | Context(Strategy *strategy) { 8 | strategy_ = strategy; 9 | } 10 | //封装后的策略方法 11 | void doAnythinig() { 12 | strategy_->doSomething(); 13 | } 14 | private: 15 | Strategy *strategy_ = nullptr; 16 | }; 17 | #endif // CONTEXT_H 18 | -------------------------------------------------------------------------------- /StrategyPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concretestrategy.h" 2 | #include "context.h" 3 | int main(int argc, char *argv[]) { 4 | Strategy *strategy = new ConcreteStrategy1(); 5 | //声明上下文对象 6 | Context context(strategy); 7 | //执行封装后的方法 8 | context.doAnythinig(); 9 | delete strategy; 10 | } 11 | -------------------------------------------------------------------------------- /StrategyPattern/strategy.h: -------------------------------------------------------------------------------- 1 | #ifndef STRATEGY_H 2 | #define STRATEGY_H 3 | class Strategy { 4 | //策略模式的运算法则 5 | public: 6 | virtual ~Strategy() { } 7 | virtual void doSomething() = 0; 8 | }; 9 | #endif // STRATEGY_H 10 | -------------------------------------------------------------------------------- /TemplateMethodPattern/TemplateMethodPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | abstract_class.h \ 21 | concrete_class.h 22 | -------------------------------------------------------------------------------- /TemplateMethodPattern/abstract_class.h: -------------------------------------------------------------------------------- 1 | #ifndef ABSTRACT_CLASS_H 2 | #define ABSTRACT_CLASS_H 3 | 4 | class AbstractClass { 5 | //模板方法 6 | public: 7 | virtual void templateMethod() final{ 8 | doAnything(); 9 | doSomething(); 10 | } 11 | //基本方法 12 | protected: 13 | virtual void doSomething() { }; 14 | virtual void doAnything() { }; 15 | }; 16 | 17 | #endif // ABSTRACT_CLASS_H 18 | -------------------------------------------------------------------------------- /TemplateMethodPattern/concrete_class.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETE_CLASS_H 2 | #define CONCRETE_CLASS_H 3 | #include "abstract_class.h" 4 | #include 5 | class ConcreteClass1 : public AbstractClass{ 6 | //实现基本方法 7 | protected: 8 | virtual void doAnything() override {qDebug()<<"ConcreteClass1";} 9 | virtual void doSomething() override { } 10 | }; 11 | class ConcreteClass2 : public AbstractClass{ 12 | //实现基本方法 13 | protected: 14 | virtual void doAnything() override {qDebug()<<"ConcreteClass2";} 15 | virtual void doSomething() override { } 16 | }; 17 | #endif // CONCRETE_CLASS_H 18 | -------------------------------------------------------------------------------- /TemplateMethodPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "concrete_class.h" 2 | int main(int argc, char *argv[]) { 3 | AbstractClass *t1 = new ConcreteClass1; 4 | AbstractClass *t2 = new ConcreteClass2; 5 | t1->templateMethod(); 6 | t2->templateMethod(); 7 | } 8 | -------------------------------------------------------------------------------- /VisitorPattern/VisitorPattern.pro: -------------------------------------------------------------------------------- 1 | QT -= gui 2 | 3 | CONFIG += c++11 console 4 | CONFIG -= app_bundle 5 | 6 | # The following define makes your compiler emit warnings if you use 7 | # any feature of Qt which as been marked deprecated (the exact warnings 8 | # depend on your compiler). Please consult the documentation of the 9 | # deprecated API in order to know how to port your code away from it. 10 | DEFINES += QT_DEPRECATED_WARNINGS 11 | 12 | # You can also make your code fail to compile if you use deprecated APIs. 13 | # In order to do so, uncomment the following line. 14 | # You can also select to disable deprecated APIs only up to a certain version of Qt. 15 | #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 16 | 17 | SOURCES += main.cpp 18 | 19 | HEADERS += \ 20 | element.h \ 21 | concreteelement.h \ 22 | ivisitor.h \ 23 | visitor.h \ 24 | objectstruture.h 25 | -------------------------------------------------------------------------------- /VisitorPattern/concreteelement.h: -------------------------------------------------------------------------------- 1 | #ifndef CONCRETEELEMENT_H 2 | #define CONCRETEELEMENT_H 3 | #include "element.h" 4 | #include "ivisitor.h" 5 | class ConcreteElement1 : public Element { 6 | //完善业务逻辑 7 | public: 8 | void doSomething() override { } 9 | //允许那个访问者访问 10 | void accept(IVisitor *visitor) override { 11 | visitor->visit1(this); 12 | } 13 | }; 14 | class ConcreteElement2 : public Element { 15 | //完善业务逻辑 16 | public: 17 | void doSomething() override { } 18 | //允许那个访问者访问 19 | void accept(IVisitor *visitor) override { 20 | visitor->visit2(this); 21 | } 22 | }; 23 | #endif // CONCRETEELEMENT_H 24 | -------------------------------------------------------------------------------- /VisitorPattern/element.h: -------------------------------------------------------------------------------- 1 | #ifndef ELEMENT_H 2 | #define ELEMENT_H 3 | class IVisitor; 4 | class Element { 5 | public: 6 | virtual ~Element() { } 7 | virtual void doSomething() = 0; 8 | //允许谁来访问 9 | virtual void accept(IVisitor *visitor) = 0; 10 | }; 11 | #endif // ELEMENT_H 12 | -------------------------------------------------------------------------------- /VisitorPattern/ivisitor.h: -------------------------------------------------------------------------------- 1 | #ifndef IVISITOR_H 2 | #define IVISITOR_H 3 | class Element; 4 | class IVisitor { 5 | //可以访问哪些对象 6 | public: 7 | virtual void visit1(Element *el1) = 0; 8 | virtual void visit2(Element *el2) = 0; 9 | }; 10 | #endif // IVISITOR_H 11 | -------------------------------------------------------------------------------- /VisitorPattern/main.cpp: -------------------------------------------------------------------------------- 1 | #include "element.h" 2 | #include "objectstruture.h" 3 | #include "visitor.h" 4 | int main(int argc, char *argv[]) { 5 | for(int i=0;i<10;i++){ 6 | //获得元素对象 7 | Element *el = ObjectStruture::createElement(); 8 | //接受访问者访问 9 | el->accept(new Visitor()); 10 | delete el; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /VisitorPattern/objectstruture.h: -------------------------------------------------------------------------------- 1 | #ifndef OBJECTSTRUTURE_H 2 | #define OBJECTSTRUTURE_H 3 | #include "concreteelement.h" 4 | class ObjectStruture { 5 | //对象生成器, 这里通过一个工厂方法模式模拟 6 | public: 7 | static Element *createElement() { 8 | if(true)//判断条件 9 | return new ConcreteElement1(); 10 | return new ConcreteElement2(); 11 | } 12 | }; 13 | #endif // OBJECTSTRUTURE_H 14 | -------------------------------------------------------------------------------- /VisitorPattern/visitor.h: -------------------------------------------------------------------------------- 1 | #ifndef VISITOR_H 2 | #define VISITOR_H 3 | #include "ivisitor.h" 4 | class Visitor : public IVisitor { 5 | public: 6 | //访问el1元素 7 | void visit1(Element *el1) override { 8 | el1->doSomething(); 9 | } 10 | //访问el2元素 11 | void visit2(Element *el2) override { 12 | el2->doSomething(); 13 | } 14 | }; 15 | #endif // VISITOR_H 16 | --------------------------------------------------------------------------------