设计模式

xiaoxiao2021-02-28  43

C++11单利模式

class Singleton { private: Singleton(); Singleton(const Singleton&); Singleton& operator = (const Singleton&); public: static Singleton& GetInstance() { // C++11 标准下local static 对象初始化在多线程条件下安全 static Singleton instance; return instance; } private: // class members };

简单工厂模式

/************************************************************************/ /* 简单工厂模式:实质是根据传入的参数,动态的决定创建哪一个产品类 */ /* author@huluwa date@2017年11月10日 */ /************************************************************************/ #include "stdafx.h" #include <iostream> using namespace std; // 定义产品抽象类 class Product { public: virtual void show() = 0; }; // 定义产品A类 class Product_A :public Product { void show() { cout << "Product_A" << endl; } }; // 定义产品B类 class Product_B :public Product { void show() { cout << "Product_B" << endl; } }; // 定义工厂类 class Factory { public: Product* Create(int i) { switch (i) { case 1: return new Product_A; break; case 2: return new Product_B; break; default: break; } } }; int main() { Factory* factory = new Factory; factory->Create(1)->show(); factory->Create(2)->show(); system("pause"); return 0; }

抽象工厂模式

/************************************************************************/ /* 抽象工厂模式:相较于简单工厂模式和工厂方法模式,可以满足一个工厂需要生产 /* 多种产品的情况。 /* author@huluwa date@2017年11月10日 /************************************************************************/ #include "stdafx.h" #include <iostream> using namespace std; // 定义产品1抽象类 class Product_1 { public: virtual void show() = 0; }; // 定义产品A1类 class Product_A1 :public Product_1 { void show() { cout << "Product_A1" << endl; } }; // 定义产品B1类 class Product_B1 :public Product_1 { void show() { cout << "Product_B1" << endl; } }; // 定义产品2抽象类 class Product_2 { public: virtual void show() = 0; }; // 定义产品A1类 class Product_A2 :public Product_2 { void show() { cout << "Product_A2" << endl; } }; // 定义产品B2类 class Product_B2 :public Product_2 { void show() { cout << "Product_B2" << endl; } }; // 定义工厂抽象类 class Factory { public: virtual Product_1* Create1() = 0; virtual Product_2* Create2() = 0; }; // 定义工厂A子类 class Factory_A :public Factory { public: Product_1* Create1() { return new Product_A1; } Product_2* Create2() { return new Product_A2; } }; // 定义工厂B子类 class Factory_B :public Factory { public: Product_1* Create1() { return new Product_B1; } Product_2* Create2() { return new Product_B2; } }; int main() { Factory_A* factory_a = new Factory_A; factory_a->Create1()->show(); factory_a->Create2()->show(); Factory_B* factory_b = new Factory_B; factory_b->Create1()->show(); factory_b->Create2()->show(); system("pause"); return 0; }

工厂方法模式

/************************************************************************/ /* 工厂方法模式:相较于简单工厂模式直接告诉工厂生产哪一类产品,可以先创建 /*一个特定的工厂子类来生产特定的产品,这样就可以避免修改工厂类。 /* author@huluwa date@2017年11月10日 /************************************************************************/ #include "stdafx.h" #include <iostream> using namespace std; // 定义产品抽象类 class Product { public: virtual void show() = 0; }; // 定义产品A类 class Product_A :public Product { void show() { cout << "Product_A" << endl; } }; // 定义产品B类 class Product_B :public Product { void show() { cout << "Product_B" << endl; } }; // 定义工厂抽象类 class Factory { public: virtual Product* Create() = 0; }; // 定义工厂A子类 class Factory_A :public Factory { public: Product* Create() { return new Product_A; } }; // 定义工厂B子类 class Factory_B :public Factory { public: Product* Create() { return new Product_B; } }; int main() { Factory_A* factory_a = new Factory_A; Factory_B* factory_b = new Factory_B; factory_a->Create()->show(); factory_b->Create()->show(); system("pause"); return 0; }
转载请注明原文地址: https://www.6miu.com/read-2620035.html

最新回复(0)