C++:设计模式

mac2024-11-01  13

C++:设计模式

Ⅰ. 创建型模式1. 工厂模式(Factory Pattern)2. 抽象工厂模式(Abstract Factory Pattern)3. 单例模式(Singleton Pattern)4. 建造者模式(Builder Pattern)5. 原型模式(Prototype Pattern) Ⅱ. 结构型模式1. 迭代器2.观察者3.模板(Template) Ⅲ. 行为型模式Ⅳ. 参考

Ⅰ. 创建型模式

1. 工厂模式(Factory Pattern)

#include <iostream> using namespace std; // 1. 抽象类 class Car { public: virtual void createCar() = 0; }; // 1.1生产奥迪 class Audi: public Car { public: void createCar() { cout << "Create Audi ..." << endl; } }; // 1.2生产宝马 class BMW: public Car { public: void createCar() { cout << "Create BMW ..." << endl; } }; // 2.工厂 class CarFactory { public: Car* create(string carType) { if(carType.compare("Audi") == 0) return new Audi(); else if(carType.compare("BMW") == 0) return new BMW(); return NULL; } }; // 3.使用工厂 int main(int argc, char **argv) { CarFactory* carFactory = new CarFactory(); Car* audi = carFactory->create("Audi"); audi->createCar(); Car* bmw = carFactory->create("BMW"); bmw->createCar(); }

  第11至24行定义了生产奥迪和宝马的类。   第27至36行的工厂类,用于接受用户指令以创建用户想要的对象。

2. 抽象工厂模式(Abstract Factory Pattern)

#include <iostream> using namespace std; // 1.生成车的抽象类 class Car { public: virtual void createCar() = 0; }; // 1.1生产奥迪 class Audi: public Car { public: void createCar() { cout << "Create Audi ..." << endl; } }; // 1.2生产宝马 class BMW: public Car { public: void createCar() { cout << "Create BMW ..." << endl; } }; // 2.涂颜色的抽象类 class Color { public: virtual void fillColor() = 0; }; // 2.1涂红色 class Red: public Color { public: void fillColor() { cout << "file red ..." << endl; } }; // 2.2涂绿色 class Green: public Color { public: void fillColor() { cout << "fill green ..." << endl; } }; // 3.工厂接口 class AbstractFactory { public: virtual Car* makeCarFactory(string carType) = 0; virtual Color* makeColorFactory(string colorType) = 0; }; // 3.1生产车的工厂 class CarFactory: public AbstractFactory { public: Car* makeCarFactory(string carType) { if(carType.compare("Audi") == 0) return new Audi(); else if(carType.compare("BMW") == 0) return new BMW(); return NULL; } Color* makeColorFactory(string colorType) { return NULL; } }; // 3.2涂颜色的工厂 class ColorFactory: public AbstractFactory { public: Car* makeCarFactory(string carType) { return NULL; } Color* makeColorFactory(string colorType) { if(colorType.compare("Red") == 0) return new Red(); else if(colorType.compare("Green") == 0) return new Green(); return NULL; } }; // 4.工厂创造器/生成器类 class FactoryProducer { public: static AbstractFactory* getFactory(string factoryType) { if(factoryType.compare("Car") == 0) return new CarFactory(); else if(factoryType.compare("Color") == 0) return new ColorFactory(); return NULL; } }; int main() { AbstractFactory* carFactory = FactoryProducer::getFactory("Car"); Car* audi = carFactory->makeCarFactory("Audi"); audi->createCar(); Car* bmw = carFactory->makeCarFactory("BMW"); bmw->createCar(); AbstractFactory* colorFactory = FactoryProducer::getFactory("Color"); Color* red = colorFactory->makeColorFactory("Red"); red->fillColor(); Color* green = colorFactory->makeColorFactory("Green"); green->fillColor(); }

3. 单例模式(Singleton Pattern)

#include <iostream> using namespace std; class SingleObject { public: static SingleObject* getInstance() { if(instance == NULL) instance = new SingleObject(); return instance; } private: SingleObject() = default; static SingleObject* instance; }; SingleObject* SingleObject::instance = NULL; int main() { SingleObject* s1 = SingleObject::getInstance(); SingleObject* s2 = SingleObject::getInstance(); if(s1 == s2) cout << "s1 == s2" << endl; return 0; }

[参考]

4. 建造者模式(Builder Pattern)

[参考]

5. 原型模式(Prototype Pattern)

用对象指定创建对象的种类。

#include <iostream> using namespace std; class CPrototype { public: virtual CPrototype* Clone() = 0; }; class CConcretePrototype: public CPrototype { public: virtual CPrototype* Clone() { return new CConcretePrototype(*this); } }; int main() { CPrototype* conProA = new CConcretePrototype(); CPrototype* conProB = conProA->Clone(); if(conProA == conProB) cout << "s1 == s2" << endl; else cout << "s1 != s2" << endl; }

  第19行创建对象时,用对象conProA指定创建对象conProB的种类。   原型模式的核心是clone()方法,从第19行来看,客户不需要知道对象conProA的实际类型,只需知道它的抽象基类是CPrototype即可。

[参考]

Ⅱ. 结构型模式

1. 迭代器

2.观察者

3.模板(Template)

#include<iostream> using namespace std; // 1 class Game { public: void play() { initialize(); startPlay(); endPlay(); } private: virtual void initialize() = 0; virtual void startPlay() = 0; virtual void endPlay() = 0; }; // 2.1 class Cricket: public Game { void initialize() { cout << "Cricket Game Initialized! Start playing." << endl; } void startPlay() { cout << "Cricket Game Started. Enjoy the game!" << endl; } void endPlay() { cout << "Cricket Game Finished!" << endl; } }; // 2.2 class Football: public Game { void initialize() { cout << "Football Game Initialized! Start playing." << endl; } void startPlay() { cout << "Football Game Started. Enjoy the game!" << endl; } void endPlay() { cout << "Football Game Finished!" << endl; } }; int main() { Game* game = new Cricket(); game -> play(); cout << endl; game = new Football(); game -> play(); }

Ⅲ. 行为型模式

Ⅳ. 参考

菜鸟教程。
最新回复(0)