赞
踩
C++中常见的五种设计模式包括:单例模式、工厂模式、观察者模式、策略模式和装饰器模式。以下是给出每个设计模式对应的main函数和完整代码示例:
#include<iostream> class Singleton { private: static Singleton* instance; // 私有构造函数,防止外部实例化 Singleton() {} public: // 获取实例的唯一入口 static Singleton* getInstance() { if (instance == nullptr) { instance = new Singleton(); } return instance; } void showMessage() { std::cout << "Hello, I am a Singleton." << std::endl; } }; // 初始化实例指针为nullptr Singleton* Singleton::instance = nullptr; int main() { Singleton* singleton = Singleton::getInstance(); singleton->showMessage(); return 0; }
#include<iostream> // 抽象产品类 class Product { public: virtual void showMessage() = 0; }; // 具体产品类A class ConcreteProductA : public Product { public: void showMessage() override { std::cout << "Hello, I am Product A." << std::endl; } }; // 具体产品类B class ConcreteProductB : public Product { public: void showMessage() override { std::cout << "Hello, I am Product B." << std::endl; } }; // 工厂类 class Factory { public: Product* createProduct(int type) { if (type == 1) { return new ConcreteProductA(); } else if (type == 2) { return new ConcreteProductB(); } else { return nullptr; } } }; int main() { Factory factory; Product* productA = factory.createProduct(1); productA->showMessage(); Product* productB = factory.createProduct(2); productB->showMessage(); delete productA; delete productB; return 0; }
#include<iostream> #include<vector> // 抽象观察者类 class Observer { public: virtual void update(int data) = 0; }; // 具体观察者类A class ConcreteObserverA : public Observer { public: void update(int data) override { std::cout << "Observer A: " << data << std::endl; } }; // 具体观察者类B class ConcreteObserverB : public Observer { public: void update(int data) override { std::cout << "Observer B: " << data << std::endl; } }; // 主题类(被观察者) class Subject { private: int data; std::vector<Observer*> observers; public: void attach(Observer* observer) { observers.push_back(observer); } void setData(int value) { data = value; notifyObservers(); } void notifyObservers() { for (auto observer : observers) { observer->update(data); } } }; int main() { Subject subject; ConcreteObserverA observerA; ConcreteObserverB observerB; subject.attach(&observerA); subject.attach(&observerB); subject.setData(10); return 0; }
#include<iostream> // 抽象策略类 class Strategy { public: virtual void execute() = 0; }; // 具体策略类A class ConcreteStrategyA : public Strategy { public: void execute() override { std::cout << "Executing Strategy A." << std::endl; } }; // 具体策略类B class ConcreteStrategyB : public Strategy { public: void execute() override { std::cout << "Executing Strategy B." << std::endl; } }; // 上下文类 class Context { private: Strategy* strategy; public: void setStrategy(Strategy* strategy) { this->strategy = strategy; } void executeStrategy() { strategy->execute(); } }; int main() { Context context; ConcreteStrategyA strategyA; ConcreteStrategyB strategyB; context.setStrategy(&strategyA); context.executeStrategy(); context.setStrategy(&strategyB); context.executeStrategy(); return 0; }
#include<iostream> // 抽象组件类 class Component { public: virtual void showMessage() = 0; }; // 具体组件类 class ConcreteComponent : public Component { public: void showMessage() override { std::cout << "Hello, I am a Concrete Component." << std::endl; } }; // 抽象装饰器类 class Decorator : public Component { protected: Component* component; public: Decorator(Component* component) : component(component) {} void showMessage() override { component->showMessage(); } }; // 具体装饰器类A class ConcreteDecoratorA : public Decorator { public: ConcreteDecoratorA(Component* component) : Decorator(component) {} void showMessage() override { Decorator::showMessage(); std::cout << "Additional functionality A." << std::endl; } }; // 具体装饰器类B class ConcreteDecoratorB : public Decorator { public: ConcreteDecoratorB(Component* component) : Decorator(component) {} void showMessage() override { Decorator::showMessage(); std::cout << "Additional functionality B." << std::endl; } }; int main() { ConcreteComponent component; ConcreteDecoratorA decoratorA(&component); ConcreteDecoratorB decoratorB(&decoratorA); decoratorB.showMessage(); return 0; }
以上是C++中常见的五种设计模式,并分别给出了每个设计模式对应的main函数和完整代码示例。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。