C++常用设计模式之工厂模式
序言
在工厂模式中,我们创建对象时不会对客户暴露逻辑并且是通过一个共同的接口来指向新创建的对象。工厂模式作为一种创建模式,一般在创建复杂对象时,考虑使用;创建简单对象时,建议直接new完成一个实例对象的创建。
简单工厂模式
主要特点是需要在工厂类中做判断,从而创建相应的产品,当增加新的产品时,需要修改工厂类。使用简单工厂模式,我们只需要知道具体的产品型号就可以创建一个产品。
缺点:工厂类中集中了所有产品的创建逻辑,如果产品量过大,会使工程类变得臃肿。
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
| #include <iostream>
using namespace std;
typedef enum { Clothes_Min = -1, Clothes_pants, Clothes_jeans, Clothes_Max, }Clothes_t;
class Clothes { public: virtual const string& type() = 0; };
class Pants : public Clothes { public: Pants():m_type("Pants") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Jeans : public Clothes { public: Jeans():m_type("Jeans") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Factory { public: Clothes *MakeClothes(Clothes_t clothes_type) { switch(clothes_type) { case Clothes_pants: return new Pants(); case Clothes_jeans: return new Jeans(); default: return nullptr; } } };
int main() { Factory fac;
Clothes *pants = fac.MakeClothes(Clothes_pants); pants->type(); Clothes *jeans = fac.MakeClothes(Clothes_jeans); jeans->type();
delete pants; pants = nullptr; delete jeans; jeans = nullptr;
return 0; }
|
UML类图:

工厂方法模式
定义一个创建对象的接口,其子类去具体实现这个接口已完成具体的创建工作。如果需要增加新的产品类,只需要扩展一个相应的工厂类即可。
缺点:产品类数据较多时,需要实现大量的工厂类,这无疑增加了代码量。
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
| #include <iostream>
using namespace std;
class Clothes { public: virtual const string& type() = 0; };
class Pants : public Clothes { public: Pants():m_type("Pants") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Jeans : public Clothes { public: Jeans():m_type("Jeans") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Factory { public: virtual Clothes* MakeClothes() = 0; };
class PantsFactory : public Factory { public:
Clothes* MakeClothes() { return new Pants(); } };
class JeansFactory : public Factory { public:
Clothes* MakeClothes() { return new Jeans(); } };
int main() { Factory *pants_fac = nullptr; Clothes *pants_clo = nullptr; pants_fac = new PantsFactory(); pants_clo = pants_fac->MakeClothes(); pants_clo->type();
delete pants_fac; pants_fac = nullptr; delete pants_clo; pants_clo = nullptr;
Factory *jeans_fac = nullptr; Clothes *jeans_clo = nullptr;
jeans_fac = new JeansFactory(); jeans_clo = jeans_fac->MakeClothes(); jeans_clo->type();
delete jeans_fac; jeans_fac = nullptr; delete jeans_clo; jeans_clo = nullptr;
return 0; }
|
UML类图:

抽象工厂模式
抽象工厂模式提供创建一系列相关或者相互依赖对象的接口,而无需指定他们具体的类。
当存在多个产品系列,而客户端只使用一格系列的产品时,可以考虑使用抽象工厂模式
缺点:当增加一个新系列的产品时,不仅需要实现具体的产品类,还需要增加一个新的创建接口,扩展相对困难。
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 153 154 155 156 157 158 159 160 161 162 163
| #include <iostream>
using namespace std;
class Pants { public: virtual const string& type() = 0; };
class WhitePants : public Pants { public: WhitePants():m_type("WhitePants") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class BlackPants : public Pants { public: BlackPants():m_type("BlackPants") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Jeans { public: virtual const string& type() = 0; };
class WhiteJeans : public Jeans { public: WhiteJeans():m_type("WhiteJeans") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class BlackJeans : public Jeans { public: BlackJeans():m_type("BlackJeans") {
} const string& type() { cout << m_type << endl; return m_type; } private: string m_type; };
class Factory { public: virtual Pants* MakePants() = 0; virtual Jeans* MakeJeans() = 0; };
class WhiteFactory : public Factory { public: Pants* MakePants() { return new WhitePants(); }
Jeans* MakeJeans() { return new WhiteJeans(); } };
class BlackFactory : public Factory { public: Pants* MakePants() { return new BlackPants(); }
Jeans* MakeJeans() { return new BlackJeans(); } };
int main() { Factory *white_fac = nullptr; Pants* white_pants = nullptr; Jeans* white_jeans = nullptr;
white_fac = new WhiteFactory(); white_pants = white_fac->MakePants(); white_pants->type(); white_jeans = white_fac->MakeJeans(); white_jeans->type();
delete white_fac; white_fac = nullptr; delete white_pants; white_pants = nullptr; delete white_jeans; white_jeans = nullptr;
Factory *black_fac = nullptr; Pants* black_pants = nullptr; Jeans* black_jeans = nullptr;
black_fac = new BlackFactory(); black_pants = black_fac->MakePants(); black_pants->type(); black_jeans = black_fac->MakeJeans(); black_jeans->type();
delete black_fac; black_fac = nullptr; delete black_pants; black_pants = nullptr; delete black_jeans; black_jeans = nullptr;
return 0; }
|
UML类图:
