原文链接:https://www.cnblogs.com/chengjundu/p/8473564.html
设计模式
1、工厂模式
在工厂模式中,我们在创建对象时不会对客户端暴露创建逻辑,并且是通过使用一个共同的接口来指向新创建的对象。工厂模式作为一种创建模式,一般在创建复杂对象时,考虑使用;在创建简单对象时,建议直接new完成一个实例对象的创建。
1.1、简单工厂模式
主要特点是需要在工厂类中做判断,从而创造相应的产品,当增加新产品时,需要修改工厂类。使用简单工厂模式,我们只需要知道具体的产品型号就可以创建一个产品。
缺点:工厂类集中了所有产品类的创建逻辑,如果产品量较大,会使得工厂类变的非常臃肿。
1 /*
2 关键代码:创建过程在工厂类中完成。
3 */ 4 5 #include <iostream> 6 7 using namespace std; 8 9 //定义产品类型信息 10 typedef enum 11 { 12 Tank_Type_56, 13 Tank_Type_96, 14 Tank_Type_Num 15 }Tank_Type; 16 17 //抽象产品类 18 class Tank 19 { 20 public: 21 virtual const string& type() = 0; 22 }; 23 24 //具体的产品类 25 class Tank56 : public Tank 26 { 27 public: 28 Tank56():Tank(),m_strType("Tank56") 29 { 30 } 31 32 const string& type() override 33 { 34 cout << m_strType.data() << endl; 35 return m_strType; 36 } 37 private: 38 string m_strType; 39 }; 40 41 //具体的产品类 42 class Tank96 : public Tank 43 { 44 public: 45 Tank96():Tank(),m_strType("Tank96") 46 { 47 } 48 const string& type() override 49 { 50 cout << m_strType.data() << endl; 51 return m_strType; 52 } 53 54 private: 55 string m_strType; 56 }; 57 58 //工厂类 59 class TankFactory 60 { 61 public: 62 //根据产品信息创建具体的产品类实例,返回一个抽象产品类 63 Tank* createTank(Tank_Type type) 64 { 65 switch(type) 66 { 67 case Tank_Type_56: 68 return new Tank56(); 69 case Tank_Type_96: 70 return new Tank96(); 71 default: 72 return nullptr; 73 } 74 } 75 }; 76 77 78 int main() 79 { 80 TankFactory* factory = new TankFactory(); 81 Tank* tank56 = factory->createTank(Tank_Type_56); 82 tank56->type(); 83 Tank* tank96 = factory->createTank(Tank_Type_96); 84 tank96->type(); 85 86 delete tank96; 87 tank96 = nullptr; 88 delete tank56; 89 tank56 = nullptr; 90 delete factory; 91 factory = nullptr; 92 93 return 0; 94 }
1.2、工厂方法模式
定义一个创建对象的接口,其子类去具体现实这个接口以完成具体的创建工作。如果需要增加新的产品类,只需要扩展一个相应的工厂类即可。
缺点:产品类数据较多时,需要实现大量的工厂类,这无疑增加了代码量。
1 /*
2 关键代码:创建过程在其子类执行。
3 */ 4 5 #include <iostream> 6 7 using namespace std; 8 9 //产品抽象类 10 class Tank 11 { 12 public: 13 virtual const string& type() = 0; 14 }; 15 16 //具体的产品类 17 class Tank56 : public Tank 18 { 19 public: 20 Tank56():Tank(),m_strType("Tank56") 21 { 22 } 23 24 const string& type() override 25 { 26 cout << m_strType.data() << endl; 27 return m_strType; 28 } 29 private: 30 string m_strType; 31 }; 32 33 //具体的产品类 34 class Tank96 : public Tank 35 { 36 public: 37 Tank96():Tank(),m_strType("Tank96") 38 { 39 } 40 const string& type() override 41 { 42 cout << m_strType.data() << endl; 43 return m_strType; 44 } 45 46 private: 47 string m_strType; 48 }; 49 50 //抽象工厂类,提供一个创建接口 51 class TankFactory 52 { 53 public: 54 //提供创建产品实例的接口,返回抽象产品类 55 virtual Tank* createTank() = 0; 56 }; 57 58 //具体的创建工厂类,使用抽象工厂类提供的接口,去创建具体的产品实例 59 class Tank56Factory : public TankFactory 60 { 61 public: 62 Tank* createTank() override 63 { 64 return new Tank56(); 65 } 66 }; 67 68 //具体的创建工厂类,使用抽象工厂类提供的接口,去创建具体的产品实例 69 class Tank96Factory : public TankFactory 70 { 71 public: 72 Tank* createTank() override 73 { 74 return new Tank96(); 75 } 76 }; 77 78 79 int main() 80 { 81 TankFactory* factory56 = new Tank56Factory(); 82 Tank* tank56 = factory56->createTank(); 83 tank56->type(); 84 85 TankFactory* factory96 = new Tank96Factory(); 86 Tank* tank96 = factory96->createTank(); 87 tank96->type(); 88 89 delete tank96; 90 tank96 = nullptr; 91 delete factory96; 92 factory96 = nullptr; 93 94 delete tank56; 95 tank56 =