5 工厂方法模式

工厂方法模式(Factory Method):定义了一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。

结构图:

图解:一个具体的产品,对应一个工厂子类。同时,解决了工厂类与分支的耦合。

  简单工厂模式最大的优点在于工厂类中包含了必要的逻辑判断(也是工厂类中的耦合),根据客户端的选择条件实例化相关的类,对于客户端来说,去除了与具体产品的以来。

  例子:

  计算器的简单工厂模式结构图:

 

  计算器的工厂方法模式结构图:

增加方法时,只需扩展,不需修改(开-闭模式)

优点:“工厂方法克服了简单工厂违背开放-封闭原则的缺点,又保持了封装对象创建原则的优点。”

缺点:“工厂模式实现时,客户端需要决定哪一个工厂来实例化运算类,选择判断问题还是存在的,也就是说,工厂方法把简单工厂的内部逻辑判断移到了客户端代码来进行。你想要加功能,本来是改工厂类的,而现在是修改客户端。”

计算器方法工程模式C++代码实现
1 #include <iostream>
2 #include <string>
3
4  using std::string;
5  using std::cout;
6 using std::endl;
7
8 template<class T>
9 class Operation
10 {
11 public:
12 Operation()
13 {
14 numberA = 0;
15 numberB = 0;
16 }
17 virtual T GetResult()
18 {
19 T result = 0;
20 return result;
21 }
22 public:
23 T numberA;
24 T numberB;
25 };
26
27 template<class T>
28 class OperationAdd : public Operation<T>
29 {
30 public:
31 T GetResult()
32 {
33 return numberA + numberB;
34 }
35 };
36
37 template<class T>
38 class OperationMinus : public Operation<T>
39 {
40 public:
41 T GetResult()
42 {
43 return numberA - numberB;
44 }
45 };
46
47 template<class T>
48 class OperationMultiply : public Operation<T>
49 {
50 public:
51 T GetResult()
52 {
53 return numberA * numberB;
54 }
55 };
56
57 template<class T>
58 class OperationDivide : public Operation<T>
59 {
60 public:
61 T GetResult()
62 {
63 return numberA / numberB;
64 }
65 };
66
67
68 /*//简单工厂模式的工厂类
69 template<class T>
70 class OperationFactory
71 {
72 public:
73 static Operation<T>* CreateOperate(string operate) //根据客户需要,返回一个产品
74 {
75 Operation<T>* oper = NULL;
76 if(operate == "+")
77 {
78 oper = new OperationAdd<T>();
79 }
80 else if(operate == "-")
81 {
82 oper = new OperationMinus<T>();
83 }
84 else if(operate == "*")
85 {
86 oper = new OperationMultiply<T>();
87 }
88 else if(operate == "/")
89 {
90 oper = new OperationDivide<T>();
91 }
92 else //默认产生的是加法运算
93 {
94 oper = new OperationAdd<T>();
95 }
96
97 return oper;
98
99 }
100 };
101 */
102 //抽象工厂接口类
103 template<class T>
104 class IFactory
105 {
106 public:
107 virtual Operation<T>* CreateOperate() = 0;
108 };
109
110 //加减乘除各建一个具体工厂去实现这个接口
111 template<class T>
112 class AddFactory : public IFactory<T>
113 {
114 public:
115 Operation<T>* CreateOperate()
116 {
117 return new OperationAdd<T>();
118 }
119 };
120
121 template<class T>
122 class MinusFactory : public IFactory<T>
123 {
124 public:
125 Operation<T>* CreateOperate()
126 {
127 return new OperationMinus<T>();
128 }
129 };
130
131 template<class T>
132 class MultiplyFactory : public IFactory<T>
133 {
134 public:
135 Operation<T>* CreateOperate()
136 {
137 return new OperationMultiply<T>();
138 }
139 };
140
141 template<class T>
142 class DevideFactory : public IFactory<T>
143 {
144 public:
145 Operation<T>* CreateOperate()
146 {
147 return new OperationDevide<T>();
148 }
149 };
150
151 int main()
152 {
153 /*//简单工厂模式的客户端
154 Operation<int>* oper = OperationFactory<int>::CreateOperate("+");
155 oper->numberA = 100;
156 oper->numberB = 200;
157 double result = oper->GetResult();
158 cout<<result<<endl;
159 */
160
161 //工厂方法模式的客户端
162 IFactory<int>* operFactory = new AddFactory<int>();
163 Operation<int>* oper = operFactory->CreateOperate();
164 oper->numberA = 100;
165 oper->numberB = 200;
166 double result = oper->GetResult();
167 cout<<result<<endl;
168 return 0;
169 }
原文地址:https://www.cnblogs.com/sifenkesi/p/1721942.html