File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // 基類
5+ class COperation {
6+ public:
7+ int m_nFirst;
8+ int m_nSecond;
9+ virtual double GetResult () {
10+ double dResult = 0 ;
11+ return dResult;
12+ }
13+ };
14+
15+ // 加法
16+ class AddOperation : public COperation {
17+ public:
18+ virtual double GetResult () {
19+ return m_nFirst + m_nSecond;
20+ }
21+ };
22+
23+ // 減法
24+ class SubOperation : public COperation {
25+ public:
26+ virtual double GetResult () {
27+ return m_nFirst - m_nSecond;
28+ }
29+ };
30+
31+ // 工廠類
32+ class CCalculatorFactory {
33+ public:
34+ static COperation* Create (char cOperator);
35+ };
36+
37+ COperation* CCalculatorFactory::Create (char cOperator) {
38+ COperation* oper;
39+
40+ // 在 C#中可以用反射來取消判斷時用的 switch,在 C++中用什麼呢?RTTI??
41+ switch (cOperator) {
42+ case ' +' :
43+ oper = new AddOperation ();
44+ break ;
45+
46+ case ' -' :
47+ oper = new SubOperation ();
48+ break ;
49+
50+ default :
51+ oper = new AddOperation ();
52+ break ;
53+ }
54+
55+ return oper;
56+ }
57+
58+ // 用戶端
59+ int main () {
60+ int a, b;
61+ cin >> a >> b;
62+ COperation* op = CCalculatorFactory::Create (' -' );
63+ op->m_nFirst = a;
64+ op->m_nSecond = b;
65+ cout << op->GetResult () << endl;
66+ return 0 ;
67+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // 策略基類
5+ class COperation {
6+ public:
7+ int m_nFirst;
8+ int m_nSecond;
9+ virtual double GetResult () {
10+ double dResult = 0 ;
11+ return dResult;
12+ }
13+ };
14+
15+ // 策略具體類—加法類
16+ class AddOperation : public COperation {
17+ public:
18+ AddOperation (int a, int b) {
19+ m_nFirst = a;
20+ m_nSecond = b;
21+ }
22+
23+ AddOperation () {
24+ }
25+
26+ virtual double GetResult () {
27+ return m_nFirst + m_nSecond;
28+ }
29+ };
30+
31+ class Context {
32+ private:
33+ COperation* op;
34+ public:
35+ Context (char cType) {
36+ switch (cType) {
37+ case ' +' :
38+ op = new AddOperation (3 , 8 );
39+ break ;
40+
41+ default :
42+ op = new AddOperation ();
43+ break ;
44+ }
45+ }
46+ double GetResult () {
47+ return op->GetResult ();
48+ }
49+ };
50+
51+ // 用戶端
52+ int main () {
53+ Context* test = new Context (' +' );
54+ cout << test->GetResult () << endl;
55+ return 0 ;
56+ }
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ using namespace std ;
3+
4+ // 策略基類
5+ class COperation {
6+ public:
7+ int m_nFirst;
8+ int m_nSecond;
9+ virtual double GetResult () {
10+ double dResult = 0 ;
11+ return dResult;
12+ }
13+ };
14+
15+ // 策略具體類—加法類
16+ class AddOperation : public COperation {
17+ public:
18+ AddOperation (int a, int b) {
19+ m_nFirst = a;
20+ m_nSecond = b;
21+ }
22+ virtual double GetResult () {
23+ return m_nFirst + m_nSecond;
24+ }
25+ };
26+
27+ class Context {
28+ private:
29+ COperation* op;
30+ public:
31+ Context (COperation* temp) {
32+ op = temp;
33+ }
34+ double GetResult () {
35+ return op->GetResult ();
36+ }
37+ };
38+
39+ // 用戶端
40+ int main () {
41+ int a, b;
42+ char c;
43+ cin >> a >> b;
44+ cout << " 請輸入運算子:"
45+ ;
46+ cin >> c;
47+
48+ switch (c) {
49+ case ' +' :
50+ {
51+ Context *context = new Context (new AddOperation (a, b));
52+ cout << context->GetResult () << endl;
53+ }
54+ break ;
55+ default :
56+ break ;
57+ }
58+
59+ return 0 ;
60+ }
You can’t perform that action at this time.
0 commit comments