Skip to content

Commit 2e51531

Browse files
author
jason_yao
committed
update
1 parent 70d7b0b commit 2e51531

8 files changed

Lines changed: 183 additions & 0 deletions

File tree

4.76 KB
Binary file not shown.

大話/cpp/builder/SConsTest

15.6 KB
Binary file not shown.

大話/note/code/simpleFactory

33.2 KB
Binary file not shown.

大話/note/code/simpleFactory.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
}
32.9 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}

大話/note/code/strategy

33.1 KB
Binary file not shown.

大話/note/code/strategy.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
}

0 commit comments

Comments
 (0)