【1】什么是工厂方法模式?
定义一个创建产品对象的工厂接口(IFactory),将实际创建工作推迟到子类(addFactory、subFactory、mulFactory、divFactory)当中。
核心工厂类(IFactory)不再负责产品的创建,仅作为一个抽象(有纯虚方法)工厂角色,只负责声明具体工厂子类必须实现的接口(即纯虚方法)。
这样进一步抽象化的好处:系统在不需要修改具体工厂角色的情况下引进新的产品(与简单工厂模式相比)。
【2】工厂方法模式代码示例
示例代码如下:
1 #include <string> 2 #include <iostream> 3 using namespace std; 4 5 class Operation 6 { 7 protected: 8 double m_numberA; 9 double m_numberB; 10 11 public: 12 double getNumberA() 13 { 14 return m_numberA; 15 } 16 17 void setNumberA(double dValue) 18 { 19 m_numberA = dValue; 20 } 21 22 double getNumberB() 23 { 24 return m_numberB; 25 } 26 27 void setNumberB(double dValue) 28 { 29 m_numberB = dValue; 30 } 31 32 virtual double getResult() 33 { 34 return 0; 35 } 36 }; 37 38 class addOperation : public Operation 39 { 40 public: 41 double getResult() 42 { 43 return m_numberA + m_numberB; 44 } 45 }; 46 47 class subOperation : public Operation 48 { 49 public: 50 double getResult() 51 { 52 return m_numberA - m_numberB; 53 } 54 }; 55 56 class mulOperation : public Operation 57 { 58 public: 59 double getResult() 60 { 61 return m_numberA * m_numberB; 62 } 63 }; 64 65 class divOperation : public Operation 66 { 67 public: 68 double getResult() 69 { 70 return m_numberA / m_numberB; 71 } 72 }; 73 74 class IFactory 75 { 76 public: 77 virtual Operation *createOperation() = 0; 78 }; 79 80 class AddFactory : public IFactory 81 { 82 public: 83 static Operation *createOperation() 84 { 85 return new addOperation(); 86 } 87 }; 88 89 class SubFactory : public IFactory 90 { 91 public: 92 static Operation *createOperation() 93 { 94 return new subOperation(); 95 } 96 }; 97 98 class MulFactory : public IFactory 99 { 100 public: 101 static Operation *createOperation() 102 { 103 return new mulOperation(); 104 } 105 }; 106 107 class DivFactory : public IFactory 108 { 109 public: 110 static Operation *createOperation() 111 { 112 return new divOperation(); 113 } 114 }; 115 116 void main() 117 { 118 Operation *pOper = MulFactory::createOperation(); 119 pOper->setNumberA(9); 120 pOper->setNumberB(99); 121 cout << pOper->getResult() << endl; 122 123 delete pOper; 124 pOper = NULL; 125 126 system("pause"); 127 } 128 129 // run out: 130 /* 131 891 132 请按任意键继续. . . 133 */
【3】工厂方法模式示例代码
代码如下:
1 #include <string> 2 #include <iostream> 3 using namespace std; 4 5 // 抽象产品类1 6 class IUser 7 { 8 public: 9 virtual void getUser() = 0; 10 virtual void setUser() = 0; 11 }; 12 13 // 具体产品类1(SqlUser) 14 class SqlUser : public IUser 15 { 16 public: 17 void getUser() 18 { 19 cout << "在sql中返回user" << endl; 20 } 21 void setUser() 22 { 23 cout << "在sql中设置user" << endl; 24 } 25 }; 26 27 // 具体产品类1(AccessUser) 28 class AccessUser : public IUser 29 { 30 public: 31 void getUser() 32 { 33 cout << "在Access中返回user" << endl; 34 } 35 void setUser() 36 { 37 cout << "在Access中设置user" << endl; 38 } 39 }; 40 41 // 抽象工厂类 42 class IFactory 43 { 44 public: 45 virtual IUser *createUser() = 0; 46 }; 47 48 // 具体工厂类(SqlFactory) 49 class SqlFactory : public IFactory 50 { 51 public: 52 IUser *createUser() 53 { 54 return new SqlUser(); // 创建具体产品1(SqlUser) 55 } 56 }; 57 58 // 具体工厂类(AccessFactory) 59 class AccessFactory : public IFactory 60 { 61 public: 62 IUser *createUser() 63 { 64 return new AccessUser(); // 创建具体产品1(AccessUser) 65 } 66 }; 67 68 void main() 69 { 70 IFactory* pFactory = new SqlFactory; 71 IUser* pUser = pFactory->createUser(); 72 pUser->getUser(); 73 pUser->setUser(); 74 75 delete pFactory; 76 pFactory = new AccessFactory; 77 delete pUser; 78 pUser = pFactory->createUser(); 79 pUser->getUser(); 80 pUser->setUser(); 81 82 delete pUser; 83 pUser = NULL; 84 85 system("pause"); 86 } 87 88 // run out: 89 /* 90 在sql中返回user 91 在sql中设置user 92 在Access中返回user 93 在Access中设置user 94 请按任意键继续. . . 95 */
以上代码主要与《抽象工厂模式》对比,为了更好的理解两者之间的区别。
Good Good Study, Day Day Up.
顺序 选择 循环 总结