前言
简单工厂、工厂方法和抽象工厂模式(创建型设计模式)的 C++ 代码示例模板。
代码仓库
简单工厂模式(Simple Factory)
结构
- 抽象产品类
- 具体产品类 A
- 具体产品类 B
- 简单工厂类
- 简单工厂类的静态方法创建具体产品对象 A 和具体产品对象 B
核心
代码
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <iostream> #include <string> #include <memory>
using std::cerr; using std::cout; using std::endl; using std::make_unique; using std::string; using std::unique_ptr;
class AbstractProduct { public: AbstractProduct() = default; virtual ~AbstractProduct() = default;
virtual void func() = 0; };
class ConcreteProductA : public AbstractProduct { public: ConcreteProductA() = default; ~ConcreteProductA() override = default;
void func() override { cout << "ConcreteProductA" << endl; } };
class ConcreteProductB : public AbstractProduct { public: ConcreteProductB() = default; ~ConcreteProductB() override = default;
void func() override { cout << "ConcreteProductB" << endl; } };
class SimpleFactory { public: static unique_ptr<AbstractProduct> create_concrete_product(const string &concrete_product_type) { if (concrete_product_type == "A") { return make_unique<ConcreteProductA>(); } else if (concrete_product_type == "B") { return make_unique<ConcreteProductB>(); } else { cerr << "Unknown type of concrete product" << endl;
return nullptr; } } };
int main() { unique_ptr<AbstractProduct> concrete_product_a = SimpleFactory::create_concrete_product("A"); if (concrete_product_a != nullptr) { concrete_product_a->func(); }
unique_ptr<AbstractProduct> concrete_product_b = SimpleFactory::create_concrete_product("B"); if (concrete_product_b != nullptr) { concrete_product_b->func(); }
unique_ptr<AbstractProduct> concrete_product_c = SimpleFactory::create_concrete_product("C"); if (concrete_product_c != nullptr) { concrete_product_c->func(); }
return 0; }
|
工厂方法模式(Factory Method)
结构
- 抽象产品类
- 具体产品类 A
- 具体产品类 B
- 抽象工厂类
- 具体工厂类 A
- 具体工厂类 B
- 具体工厂对象 A 的方法创建具体产品对象 A
- 具体工厂对象 B 的方法创建具体产品对象 B
核心
代码
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
| #include <iostream> #include <memory>
using std::cout; using std::endl; using std::make_unique; using std::unique_ptr;
class AbstractProduct { public: AbstractProduct() = default; virtual ~AbstractProduct() = default;
virtual void func() = 0; };
class ConcreteProductA : public AbstractProduct { public: ConcreteProductA() = default; ~ConcreteProductA() override = default;
void func() override { cout << "ConcreteProductA" << endl; } };
class ConcreteProductB : public AbstractProduct { public: ConcreteProductB() = default; ~ConcreteProductB() override = default;
void func() override { cout << "ConcreteProductB" << endl; } };
class AbstractFactory { public: AbstractFactory() = default; virtual ~AbstractFactory() = default;
virtual unique_ptr<AbstractProduct> create_concrete_product() const = 0; };
class ConcreteFactoryA : public AbstractFactory { public: ConcreteFactoryA() = default; ~ConcreteFactoryA() override = default;
inline unique_ptr<AbstractProduct> create_concrete_product() const override { return make_unique<ConcreteProductA>(); } };
class ConcreteFactoryB : public AbstractFactory { public: ConcreteFactoryB() = default; ~ConcreteFactoryB() override = default;
inline unique_ptr<AbstractProduct> create_concrete_product() const override { return make_unique<ConcreteProductB>(); } };
int main() { unique_ptr<AbstractFactory> concrete_factory_a = make_unique<ConcreteFactoryA>(); unique_ptr<AbstractProduct> concrete_product_a = concrete_factory_a->create_concrete_product(); concrete_product_a->func();
unique_ptr<AbstractFactory> concrete_factory_b = make_unique<ConcreteFactoryB>(); unique_ptr<AbstractProduct> concrete_product_b = concrete_factory_b->create_concrete_product(); concrete_product_b->func();
return 0; }
|
抽象工厂模式(Abstract Factory)
结构
- 抽象产品类 A
- 具体产品类 A1
- 具体产品类 B1
- 抽象产品类 B
- 具体产品类 B1
- 具体产品类 B2
- 抽象工厂类1
- 具体工厂类1
- 抽象工厂类2
- 具体工厂类2
- 具体工厂对象1 的方法创建具体产品对象 A1 和 具体产品对象 B1
- 具体工厂对象2 的方法创建具体产品对象 A2 和 具体产品对象 B2
核心
- 继承
- 多态
- 产品和工厂形成二维结构:工厂1创建产品 A1 和 B1,工厂2创建产品 A2 和 B2
代码
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include <iostream> #include <memory>
using std::cout; using std::endl; using std::make_unique; using std::unique_ptr;
class AbstractProductA { public: AbstractProductA() = default; virtual ~AbstractProductA() = default;
virtual void func() = 0; };
class ConcreteProductA1 : public AbstractProductA { public: ConcreteProductA1() = default; ~ConcreteProductA1() override = default;
void func() override { cout << "ConcreteProductA1" << endl; } };
class ConcreteProductA2 : public AbstractProductA { public: ConcreteProductA2() = default; ~ConcreteProductA2() override = default;
void func() override { cout << "ConcreteProductA2" << endl; } };
class AbstractProductB { public: AbstractProductB() = default; virtual ~AbstractProductB() = default;
virtual void func() = 0; };
class ConcreteProductB1 : public AbstractProductB { public: ConcreteProductB1() = default; ~ConcreteProductB1() override = default;
void func() override { cout << "ConcreteProductB1" << endl; } };
class ConcreteProductB2 : public AbstractProductB { public: ConcreteProductB2() = default; ~ConcreteProductB2() override = default;
void func() override { cout << "ConcreteProductB2" << endl; } };
class AbstractFactory { public: AbstractFactory() = default; virtual ~AbstractFactory() = default;
virtual unique_ptr<AbstractProductA> create_product_a() const = 0; virtual unique_ptr<AbstractProductB> create_product_b() const = 0; };
class ConcreteFactory1 : public AbstractFactory { public: ConcreteFactory1() = default; virtual ~ConcreteFactory1() = default;
inline unique_ptr<AbstractProductA> create_product_a() const override { return make_unique<ConcreteProductA1>(); }
inline unique_ptr<AbstractProductB> create_product_b() const override { return make_unique<ConcreteProductB1>(); } };
class ConcreteFactory2 : public AbstractFactory { public: ConcreteFactory2() = default; virtual ~ConcreteFactory2() = default;
inline unique_ptr<AbstractProductA> create_product_a() const override { return make_unique<ConcreteProductA2>(); }
inline unique_ptr<AbstractProductB> create_product_b() const override { return make_unique<ConcreteProductB2>(); } };
int main() { unique_ptr<AbstractFactory> concrete_factory_1 = make_unique<ConcreteFactory1>(); unique_ptr<AbstractProductA> concrete_product_a1 = concrete_factory_1->create_product_a(); unique_ptr<AbstractProductB> concrete_product_b1 = concrete_factory_1->create_product_b(); concrete_product_a1->func(); concrete_product_b1->func();
unique_ptr<AbstractFactory> concrete_factory_2 = make_unique<ConcreteFactory2>(); unique_ptr<AbstractProductA> concrete_product_a2 = concrete_factory_2->create_product_a(); unique_ptr<AbstractProductB> concrete_product_b2 = concrete_factory_2->create_product_b(); concrete_product_a2->func(); concrete_product_b2->func();
return 0; }
|
总结
简单工厂、工厂方法和抽象工厂模式(创建型设计模式)的 C++ 代码示例模板。
参考资料
作者的话
- 感谢参考资料的作者/博主
- 作者:夜悊
- 版权所有,转载请注明出处,谢谢~
- 如果文章对你有帮助,请点个赞或加个粉丝吧,你的支持就是作者的动力~
- 文章在描述时有疑惑的地方,请留言,定会一一耐心讨论、解答
- 文章在认识上有错误的地方, 敬请批评指正
- 望读者们都能有所收获