设计模式学习笔记(一):简单工厂

《大话设计模式》学习笔记,参考链接 伍迷的小菜编程成长记

  1 //operation.h
2 #pragma once
3
4 //操作类
5 class Operation
6 {
7 public:
8 Operation(void);
9 ~Operation(void);
10 int GetNumberA(void) {return this->m_bNumberA;}
11 int GetNumberB(void) {return this->m_bNumberB;}
12 void SetNumberA(int numberA) {this->m_bNumberA = numberA;}
13 void SetNumberB(int numberB) {this->m_bNumberB = numberB;}
14 virtual int GetResult(void) = 0;
15
16 private:
17 int m_bNumberA;
18 int m_bNumberB;
19 };
20
21 //operation.cpp
22 #include "operation.h"
23
24 Operation::Operation(void)
25 {
26 }
27
28 Operation::~Operation(void)
29 {
30 }
31
32 //concreteoperation.h
33 #pragma once
34
35 #include "operation.h"
36
37 //加法操作
38 class OperationAdd : public Operation
39 {
40 public:
41 OperationAdd(void);
42 ~OperationAdd(void);
43 int GetResult(void);
44 };
45
46 //减法操作
47 class OperationSub : public Operation
48 {
49 public:
50 OperationSub(void);
51 ~OperationSub(void);
52 int GetResult(void);
53 };
54
55 //乘法操作
56 class OperationMul : public Operation
57 {
58 public:
59 OperationMul(void);
60 ~OperationMul(void);
61 int GetResult(void);
62 };
63
64 //除法操作
65 class OperationDiv : public Operation
66 {
67 public:
68 OperationDiv(void);
69 ~OperationDiv(void);
70 int GetResult(void);
71 };
72
73 //concreteoperation.cpp
74 #include "concreteoperation.h"
75
76 OperationAdd::OperationAdd(void)
77 {
78 }
79
80 OperationAdd::~OperationAdd(void)
81 {
82 }
83
84 int OperationAdd::GetResult(void)
85 {
86 int result = 0;
87 result = this->GetNumberA() + this->GetNumberB();
88 return result;
89 }
90
91 OperationSub::OperationSub(void)
92 {
93 }
94
95 OperationSub::~OperationSub(void)
96 {
97 }
98
99 int OperationSub::GetResult(void)
100 {
101 int result = 0;
102 result = this->GetNumberA() - this->GetNumberB();
103 return result;
104 }
105
106 OperationMul::OperationMul(void)
107 {
108 }
109
110 OperationMul::~OperationMul(void)
111 {
112 }
113
114 int OperationMul::GetResult(void)
115 {
116 int result = 0;
117 result = this->GetNumberA() * this->GetNumberB();
118 return result;
119 }
120
121 OperationDiv::OperationDiv(void)
122 {
123 }
124
125 OperationDiv::~OperationDiv(void)
126 {
127 }
128
129 int OperationDiv::GetResult(void)
130 {
131 int result = 0;
132 result = this->GetNumberA() / this->GetNumberB();
133 return result;
134 }
135
136 //operationfactory.h
137 #pragma once
138
139 #include "operation.h"
140 #include "concreteoperation.h"
141
142 //操作工厂
143 class OperationFactory
144 {
145 public:
146 OperationFactory(void);
147 ~OperationFactory(void);
148 Operation* CreateOperation(int operationIndex);
149 };
150
151 //operationfactory.cpp
152 #include <iostream>
153 #include "operationfactory.h"
154
155 using namespace std;
156
157 OperationFactory::OperationFactory(void)
158 {
159 }
160
161 OperationFactory::~OperationFactory(void)
162 {
163 }
164
165 Operation* OperationFactory::CreateOperation(int operationIndex)
166 {
167 Operation *pOperation;
168 switch(operationIndex)
169 {
170 case 1:
171 pOperation = new OperationAdd();
172 break;
173 case 2:
174 pOperation = new OperationSub();
175 break;
176 case 3:
177 pOperation = new OperationMul();
178 break;
179 case 4:
180 pOperation = new OperationDiv();
181 break;
182 }
183 return pOperation;
184 }
185
186 //main.cpp
188 #include <iostream>
189 #include "operationfactory.h"
190
191 using namespace std;
192
193 int main(void)
194 {
195 int numberA,numberB,result;
196 int operationIndex;
197
198 cout << "请输入数字A:" << endl;
199 cin >> numberA;
200 cout << endl << "请输入数字B:" << endl;
201 cin >> numberB;
202 cout << endl << "请选择运算类型:"
203 << "(1)加法运算" << endl
204 << "(2)减法运算" << endl
205 << "(3)乘法运算" << endl
206 << "(4)除法运算" << endl << endl;
207 cin >> operationIndex;
208
209 OperationFactory aOperationFactory;
210 Operation *pOperation;
211 pOperation = aOperationFactory.CreateOperation(operationIndex);
212
213 pOperation->SetNumberA(numberA);
214 pOperation->SetNumberB(numberB);
215 result = pOperation->GetResult();
216 cout << endl << "运算结果:" << result << endl << endl;
217
218 delete pOperation;
219 return 0;
220 }

(没有考虑各种输入异常)

posted on 2011-11-30 17:00  会说话的哑巴  阅读(280)  评论(0编辑  收藏  举报