工厂模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 #include <iostream> using namespace std;class Product {public : virtual void display () = 0 ; }; class ConcreteProductA : public Product {public : void display () override { cout << "This is product A" << endl; } }; class ConcreteProductB : public Product {public : void display () override { cout << "This is product B" << endl; } }; class Factory {public : virtual Product* createProduct () = 0 ; }; class ConcreteFactoryA : public Factory {public : Product* createProduct () override { return new ConcreteProductA (); } }; class ConcreteFactoryB : public Factory {public : Product* createProduct () override { return new ConcreteProductB (); } }; int main () { Factory* factoryA = new ConcreteFactoryA (); Product* productA = factoryA->createProduct (); productA->display (); Factory* factoryB = new ConcreteFactoryB (); Product* productB = factoryB->createProduct (); productB->display (); delete factoryA; delete productA; delete factoryB; delete productB; return 0 ; }
单例模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 #include <iostream> using namespace std;class Singleton {public : static Singleton* getInstance () { if (!instance) { instance = new Singleton (); } return instance; } void showMessage () { cout << "Hello, this is the Singleton instance." << endl; } private : Singleton () {} Singleton (const Singleton&); static Singleton* instance; }; Singleton* Singleton::instance = nullptr ; int main () { Singleton* singleton = Singleton::getInstance (); singleton->showMessage (); return 0 ; }
这是一个简单的 C++ 单例模式示例。在该示例中,我们定义了 Singleton
类,其中包含一个私有的静态成员变量 instance
用于保存单例实例,以及一个公有的静态方法 getInstance
用于获取单例实例。在 main
函数中,我们通过 getInstance
方法获取单例实例,并调用其示例方法 showMessage
。注意,为了确保单例模式的唯一性,我们私有化了构造函数和拷贝构造函数,以防止外部直接创建对象实例或复制实例。
适配器模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 #include <iostream> using namespace std;class Target {public : virtual void request () = 0 ; }; class Adaptee {public : void specificRequest () { cout << "Adaptee's specific request." << endl; } }; class ClassAdapter : public Target, private Adaptee {public : void request () override { specificRequest (); } }; class ObjectAdapter : public Target {private : Adaptee* adaptee; public : ObjectAdapter (Adaptee* a) : adaptee (a) {} void request () override { adaptee->specificRequest (); } }; int main () { Target* classAdapter = new ClassAdapter (); classAdapter->request (); Adaptee* adaptee = new Adaptee (); Target* objectAdapter = new ObjectAdapter (adaptee); objectAdapter->request (); delete classAdapter; delete objectAdapter; delete adaptee; return 0 ; }
这是一个简单的 C++ 适配器模式示例。在该示例中,我们定义了目标接口 Target
和适配者类 Adaptee
。然后我们实现了适配器模式的两种形式:类适配器和对象适配器。在 main
函数中,演示了如何分别使用类适配器和对象适配器来调用适配者的方法。通过适配器模式,客户端可以调用目标接口的方法,而适配器内部则负责将请求转发给适配者的方法。
观察者模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 #include <iostream> #include <vector> using namespace std;class Observer {public : virtual void update (const string& message) = 0 ; }; class ConcreteObserverA : public Observer {public : void update (const string& message) override { cout << "ConcreteObserverA received: " << message << endl; } }; class ConcreteObserverB : public Observer {public : void update (const string& message) override { cout << "ConcreteObserverB received: " << message << endl; } }; class Subject {public : virtual void attach (Observer* observer) = 0 ; virtual void detach (Observer* observer) = 0 ; virtual void notify (const string& message) = 0 ; }; class ConcreteSubject : public Subject {private : vector<Observer*> observers; public : void attach (Observer* observer) override { observers.push_back (observer); } void detach (Observer* observer) override { for (auto it = observers.begin (); it != observers.end (); ++it) { if (*it == observer) { observers.erase (it); break ; } } } void notify (const string& message) override { for (const auto & observer : observers) { observer->update (message); } } }; int main () { ConcreteSubject subject; ConcreteObserverA observerA; ConcreteObserverB observerB; subject.attach (&observerA); subject.attach (&observerB); subject.notify ("Hello, this is a notification." ); subject.detach (&observerB); subject.notify ("Second notification." ); return 0 ; }
这是一个简单的 C++ 观察者模式示例。在该示例中,我们定义了观察者基类 Observer
和具体观察者类 ConcreteObserverA
和 ConcreteObserverB
,以及主题接口 Subject
和具体主题类 ConcreteSubject
。在 main
函数中,我们创建了具体主题和具体观察者,并演示了如何将观察者添加到主题、发送通知以及移除观察者的过程。当主题状态发生变化时,所有注册的观察者都会收到相应的通知并执行相应的更新操作。
策略模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 #include <iostream> using namespace std;class Strategy {public : virtual void execute () = 0 ; }; class ConcreteStrategyA : public Strategy {public : void execute () override { cout << "Executing strategy A" << endl; } }; class ConcreteStrategyB : public Strategy {public : void execute () override { cout << "Executing strategy B" << endl; } }; class Context {private : Strategy* strategy; public : void setStrategy (Strategy* s) { strategy = s; } void executeStrategy () { if (strategy) { strategy->execute (); } else { cout << "No strategy set" << endl; } } }; int main () { Context context; ConcreteStrategyA strategyA; context.setStrategy (&strategyA); context.executeStrategy (); ConcreteStrategyB strategyB; context.setStrategy (&strategyB); context.executeStrategy (); return 0 ; }
这是一个简单的 C++ 策略模式示例。在该示例中,我们定义了抽象策略类 Strategy
和两个具体策略类 ConcreteStrategyA
和 ConcreteStrategyB
,以及上下文类 Context
。在 main
函数中,我们创建了上下文对象,并演示了如何使用不同的具体策略来执行特定的操作。通过策略模式,客户端可以在运行时动态地切换不同的策略,而不需要修改原有的上下文代码。
装饰器模式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 #include <iostream> using namespace std;class Component {public : virtual void operation () = 0 ; }; class ConcreteComponent : public Component {public : void operation () override { cout << "This is the original operation." << endl; } }; class Decorator : public Component {protected : Component* component; public : Decorator (Component* c) : component (c) {} void operation () override { if (component) { component->operation (); } } }; class ConcreteDecoratorA : public Decorator {public : ConcreteDecoratorA (Component* c) : Decorator (c) {} void addBehavior () { cout << "Added behavior from decorator A." << endl; } void operation () override { Decorator::operation (); addBehavior (); } }; class ConcreteDecoratorB : public Decorator {public : ConcreteDecoratorB (Component* c) : Decorator (c) {} void addExtraBehavior () { cout << "Added extra behavior from decorator B." << endl; } void operation () override { Decorator::operation (); addExtraBehavior (); } }; int main () { Component* component = new ConcreteComponent (); Decorator* decoratorA = new ConcreteDecoratorA (component); decoratorA->operation (); Decorator* decoratorB = new ConcreteDecoratorB (component); decoratorB->operation (); delete component; delete decoratorA; delete decoratorB; return 0 ; }
这是一个简单的 C++ 装饰器模式示例。在该示例中,我们定义了抽象组件类 Component
和具体组件类 ConcreteComponent
,以及抽象装饰器类 Decorator
和两个具体装饰器类 ConcreteDecoratorA
和 ConcreteDecoratorB
。在 main
函数中,我们创建了具体组件对象,并使用不同的装饰器对其进行装饰。通过装饰器模式,可以动态地扩展对象的功能,同时避免了使用继承带来的静态特性和多子类衍生问题。