工厂方法模式-设计模式
来自于《大话设计模式》
工厂方法模式(Factory Method):定义一个用于创建对象的接口,让子类决定实例化哪一个类。工厂方法使一个类的实例化延迟到其子类。
有一个操作类 Operation,继承其而派生出具体各个操作的操作派生类。
产生操作类的工厂基类 IFactory,继承其而派生出产生具体操作类的工厂派生类。
UML 类图:
代码实现 C++:
1
#include
<
iostream
>
2 #include < cmath >
3 using namespace std;
4
5 class Operation
6 {
7 protected :
8 double NumberA;
9 double NumberB;
10 public :
11 virtual void SetNumberA( double a)
12 {
13 NumberA = a;
14 }
15 virtual void SetNumberB( double b)
16 {
17 NumberB = b;
18 }
19 virtual double GetResult() = 0 ;
20 };
21
22 class OperationAdd : public Operation
23 {
24 public :
25 virtual double GetResult()
26 {
27 return NumberA + NumberB;
28 }
29 };
30
31 class OperationSub : public Operation
32 {
33 public :
34 virtual double GetResult()
35 {
36 return NumberA - NumberB;
37 }
38 };
39
40 class OperationMul : public Operation
41 {
42 public :
43 virtual double GetResult()
44 {
45 return NumberA * NumberB;
46 }
47 };
48
49 class OperationDiv : public Operation
50 {
51 public :
52 virtual double GetResult()
53 {
54 if (NumberB == 0 )
55 {
56 throw runtime_error( " NumberB = 0! " );
57 }
58 return NumberA / NumberB;
59 }
60 };
61
62 class OperationPow : public Operation
63 {
64 public :
65 virtual double GetResult()
66 {
67 if (NumberA == 0 && NumberB <= 0 )
68 {
69 throw runtime_error( " NumberA = 0, NumberB <= 0! " );
70 }
71 return pow(NumberA, NumberB);
72 }
73 };
74
75 class IFactory
76 {
77 public :
78 virtual Operation * CreateOperation() = 0 ;
79 };
80
81 class AddFactory : public IFactory
82 {
83 public :
84 virtual Operation * CreateOperation()
85 {
86 return new OperationAdd;
87 }
88 };
89
90 class SubFactory : public IFactory
91 {
92 public :
93 virtual Operation * CreateOperation()
94 {
95 return new OperationSub;
96 }
97 };
98
99 class MulFactory : public IFactory
100 {
101 public :
102 virtual Operation * CreateOperation()
103 {
104 return new OperationMul;
105 }
106 };
107
108 class DivFactory : public IFactory
109 {
110 public :
111 virtual Operation * CreateOperation()
112 {
113 return new OperationDiv;
114 }
115 };
116
117 class PowFactory : public IFactory
118 {
119 public :
120 virtual Operation * CreateOperation()
121 {
122 return new OperationPow;
123 }
124 };
125
126 int main()
127 {
128 double a, b;
129 while (cin >> a >> b)
130 {
131 IFactory * pfactory = new AddFactory;
132 Operation * poperation = pfactory -> CreateOperation();
133 poperation -> SetNumberA(a);
134 poperation -> SetNumberB(b);
135 cout << poperation -> GetResult() << endl;
136 delete pfactory;
137 delete poperation;
138
139 pfactory = new SubFactory;
140 poperation = pfactory -> CreateOperation();
141 poperation -> SetNumberA(a);
142 poperation -> SetNumberB(b);
143 cout << poperation -> GetResult() << endl;
144 delete pfactory;
145 delete poperation;
146
147 pfactory = new MulFactory;
148 poperation = pfactory -> CreateOperation();
149 poperation -> SetNumberA(a);
150 poperation -> SetNumberB(b);
151 cout << poperation -> GetResult() << endl;
152 delete pfactory;
153 delete poperation;
154
155 pfactory = new DivFactory;
156 poperation = pfactory -> CreateOperation();
157 poperation -> SetNumberA(a);
158 poperation -> SetNumberB(b);
159 try
160 {
161 cout << poperation -> GetResult() << endl;
162 }
163 catch ( const runtime_error & e)
164 {
165 cerr << e.what() << endl;
166 }
167 delete pfactory;
168 delete poperation;
169
170 pfactory = new PowFactory;
171 poperation = pfactory -> CreateOperation();
172 poperation -> SetNumberA(a);
173 poperation -> SetNumberB(b);
174 cout << poperation -> GetResult() << endl;
175 delete pfactory;
176 delete poperation;
177 }
178 }
2 #include < cmath >
3 using namespace std;
4
5 class Operation
6 {
7 protected :
8 double NumberA;
9 double NumberB;
10 public :
11 virtual void SetNumberA( double a)
12 {
13 NumberA = a;
14 }
15 virtual void SetNumberB( double b)
16 {
17 NumberB = b;
18 }
19 virtual double GetResult() = 0 ;
20 };
21
22 class OperationAdd : public Operation
23 {
24 public :
25 virtual double GetResult()
26 {
27 return NumberA + NumberB;
28 }
29 };
30
31 class OperationSub : public Operation
32 {
33 public :
34 virtual double GetResult()
35 {
36 return NumberA - NumberB;
37 }
38 };
39
40 class OperationMul : public Operation
41 {
42 public :
43 virtual double GetResult()
44 {
45 return NumberA * NumberB;
46 }
47 };
48
49 class OperationDiv : public Operation
50 {
51 public :
52 virtual double GetResult()
53 {
54 if (NumberB == 0 )
55 {
56 throw runtime_error( " NumberB = 0! " );
57 }
58 return NumberA / NumberB;
59 }
60 };
61
62 class OperationPow : public Operation
63 {
64 public :
65 virtual double GetResult()
66 {
67 if (NumberA == 0 && NumberB <= 0 )
68 {
69 throw runtime_error( " NumberA = 0, NumberB <= 0! " );
70 }
71 return pow(NumberA, NumberB);
72 }
73 };
74
75 class IFactory
76 {
77 public :
78 virtual Operation * CreateOperation() = 0 ;
79 };
80
81 class AddFactory : public IFactory
82 {
83 public :
84 virtual Operation * CreateOperation()
85 {
86 return new OperationAdd;
87 }
88 };
89
90 class SubFactory : public IFactory
91 {
92 public :
93 virtual Operation * CreateOperation()
94 {
95 return new OperationSub;
96 }
97 };
98
99 class MulFactory : public IFactory
100 {
101 public :
102 virtual Operation * CreateOperation()
103 {
104 return new OperationMul;
105 }
106 };
107
108 class DivFactory : public IFactory
109 {
110 public :
111 virtual Operation * CreateOperation()
112 {
113 return new OperationDiv;
114 }
115 };
116
117 class PowFactory : public IFactory
118 {
119 public :
120 virtual Operation * CreateOperation()
121 {
122 return new OperationPow;
123 }
124 };
125
126 int main()
127 {
128 double a, b;
129 while (cin >> a >> b)
130 {
131 IFactory * pfactory = new AddFactory;
132 Operation * poperation = pfactory -> CreateOperation();
133 poperation -> SetNumberA(a);
134 poperation -> SetNumberB(b);
135 cout << poperation -> GetResult() << endl;
136 delete pfactory;
137 delete poperation;
138
139 pfactory = new SubFactory;
140 poperation = pfactory -> CreateOperation();
141 poperation -> SetNumberA(a);
142 poperation -> SetNumberB(b);
143 cout << poperation -> GetResult() << endl;
144 delete pfactory;
145 delete poperation;
146
147 pfactory = new MulFactory;
148 poperation = pfactory -> CreateOperation();
149 poperation -> SetNumberA(a);
150 poperation -> SetNumberB(b);
151 cout << poperation -> GetResult() << endl;
152 delete pfactory;
153 delete poperation;
154
155 pfactory = new DivFactory;
156 poperation = pfactory -> CreateOperation();
157 poperation -> SetNumberA(a);
158 poperation -> SetNumberB(b);
159 try
160 {
161 cout << poperation -> GetResult() << endl;
162 }
163 catch ( const runtime_error & e)
164 {
165 cerr << e.what() << endl;
166 }
167 delete pfactory;
168 delete poperation;
169
170 pfactory = new PowFactory;
171 poperation = pfactory -> CreateOperation();
172 poperation -> SetNumberA(a);
173 poperation -> SetNumberB(b);
174 cout << poperation -> GetResult() << endl;
175 delete pfactory;
176 delete poperation;
177 }
178 }