Skip to content

Commit 3598fb0

Browse files
author
jason_yao
committed
update
1 parent 70d7b0b commit 3598fb0

3 files changed

Lines changed: 77 additions & 0 deletions

File tree

大話/note/code/Decorator

40.5 KB
Binary file not shown.

大話/note/code/Decorator.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <string>
2+
#include <iostream>
3+
using namespace std;
4+
5+
//
6+
class Person {
7+
private:
8+
string m_strName;
9+
public:
10+
Person(string strName) {
11+
m_strName = strName;
12+
}
13+
Person() {}
14+
virtual void Show() {
15+
cout << "裝扮的是:" << m_strName << endl;
16+
}
17+
};
18+
19+
// 裝飾類
20+
class Finery : public Person {
21+
protected:
22+
Person* m_component;
23+
public:
24+
void Decorate(Person* component) {
25+
m_component = component;
26+
}
27+
virtual void Show() {
28+
m_component->Show();
29+
}
30+
};
31+
32+
//T 恤
33+
class TShirts: public Finery {
34+
public:
35+
virtual void Show() {
36+
cout << "T Shirts" << endl;
37+
m_component->Show();
38+
}
39+
};
40+
41+
// 褲子
42+
class BigTrouser : public Finery {
43+
public:
44+
virtual void Show() {
45+
cout << " Big Trouser" << endl;
46+
m_component->Show();
47+
}
48+
};
49+
50+
// 用戶端
51+
int main() {
52+
Person* p = new Person("小李");
53+
BigTrouser* bt = new BigTrouser();
54+
TShirts* ts = new TShirts();
55+
bt->Decorate(p);
56+
ts->Decorate(bt);
57+
ts->Show();
58+
return 0;
59+
}
60+

大話/note/code/simpleFactory.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class COperation {
2+
public:
3+
int m_nFirst;
4+
int m_nSecond;
5+
virtual double GetResult() {
6+
double dResult = 0;
7+
return dResult;
8+
}
9+
};
10+
11+
class AddOperation : public COperation {
12+
public:
13+
virtual double GetResult() {
14+
return m_nFirst + m_nSecond;
15+
}
16+
};
17+

0 commit comments

Comments
 (0)