Skip to content

Commit 698871f

Browse files
author
jason_yao
committed
update
1 parent b4cc3e5 commit 698871f

21 files changed

Lines changed: 669 additions & 0 deletions

大話/note/code/Bridge

34.4 KB
Binary file not shown.

大話/note/code/Bridge.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
using namespace std;
5+
6+
//手機軟體
7+
class HandsetSoft {
8+
public:
9+
virtual void Run() = 0;
10+
};
11+
12+
//遊戲軟體
13+
class HandsetGame : public HandsetSoft {
14+
public:
15+
virtual void Run() {
16+
cout << "運行手機遊戲" << endl;
17+
}
18+
};
19+
20+
//通訊錄軟體
21+
class HandSetAddressList : public HandsetSoft {
22+
public:
23+
virtual void Run() {
24+
cout << "手機通訊錄" << endl;
25+
}
26+
};
27+
28+
//手機品牌
29+
class HandsetBrand {
30+
protected:
31+
HandsetSoft* m_soft;
32+
public:
33+
void SetHandsetSoft(HandsetSoft* temp) {
34+
m_soft = temp;
35+
}
36+
virtual void Run() = 0;
37+
};
38+
39+
//M 品牌
40+
class HandsetBrandM : public HandsetBrand {
41+
public:
42+
virtual void Run() {
43+
m_soft->Run();
44+
}
45+
};
46+
47+
//N 品牌
48+
class HandsetBrandN : public HandsetBrand {
49+
public:
50+
virtual void Run() {
51+
m_soft->Run();
52+
}
53+
};
54+
55+
//用戶端
56+
int main() {
57+
HandsetBrand* brand;
58+
brand = new HandsetBrandM();
59+
brand->SetHandsetSoft(new HandsetGame());
60+
brand->Run();
61+
brand->SetHandsetSoft(new HandSetAddressList());
62+
brand->Run();
63+
return 0;
64+
}

大話/note/code/Command

75.3 KB
Binary file not shown.

大話/note/code/Command.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
using namespace std;
5+
6+
//烤肉師傅
7+
class Barbucer {
8+
public:
9+
void MakeMutton() {
10+
cout << "烤羊肉" << endl;
11+
}
12+
void MakeChickenWing() {
13+
cout << "烤雞翅膀" << endl;
14+
}
15+
};
16+
17+
//抽象命令類
18+
class Command {
19+
protected:
20+
Barbucer* receiver;
21+
public:
22+
Command(Barbucer* temp) {
23+
receiver = temp;
24+
}
25+
virtual void ExecuteCmd() = 0;
26+
};
27+
28+
//烤羊肉命令
29+
class BakeMuttonCmd : public Command {
30+
public:
31+
BakeMuttonCmd(Barbucer* temp) : Command(temp) {}
32+
virtual void ExecuteCmd() {
33+
receiver->MakeMutton();
34+
}
35+
};
36+
37+
//烤雞翅
38+
class ChickenWingCmd : public Command {
39+
public:
40+
ChickenWingCmd(Barbucer* temp) : Command(temp) {}
41+
virtual void ExecuteCmd() {
42+
receiver->MakeChickenWing();
43+
}
44+
};
45+
46+
//服務員類
47+
class Waiter {
48+
protected:
49+
vector<Command*> m_commandList;
50+
public:
51+
void SetCmd(Command* temp) {
52+
m_commandList.push_back(temp);
53+
cout << "增加定單" << endl;
54+
}
55+
//通知執行
56+
void Notify() {
57+
vector<Command*>::iterator p = m_commandList.begin();
58+
59+
while (p != m_commandList.end()) {
60+
(*p)->ExecuteCmd();
61+
p++;
62+
}
63+
}
64+
};
65+
66+
//用戶端
67+
int main() {
68+
//店裡添加烤肉師傅、功能表、服務員等顧客
69+
Barbucer* barbucer = new Barbucer();
70+
Command* cmd = new BakeMuttonCmd(barbucer);
71+
Command* cmd2 = new ChickenWingCmd(barbucer);
72+
Waiter* girl = new Waiter();
73+
//點菜
74+
girl->SetCmd(cmd);
75+
girl->SetCmd(cmd2);
76+
//服務員通知
77+
girl->Notify();
78+
return 0;
79+
}

大話/note/code/Component

95.6 KB
Binary file not shown.
95.6 KB
Binary file not shown.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
using namespace std;
5+
6+
class Component {
7+
public:
8+
string m_strName;
9+
Component(string strName) {
10+
m_strName = strName;
11+
}
12+
virtual void Add(Component* com) = 0;
13+
virtual void Display(int nDepth) = 0;
14+
};
15+
16+
class Leaf : public Component {
17+
public:
18+
Leaf(string strName): Component(strName) {}
19+
virtual void Add(Component* com) {
20+
cout << "leaf can't add" << endl;
21+
}
22+
virtual void Display(int nDepth) {
23+
string strtemp;
24+
25+
for (int i = 0; i < nDepth; i++) {
26+
strtemp += "-";
27+
}
28+
29+
strtemp += m_strName;
30+
cout << strtemp << endl;
31+
}
32+
};
33+
34+
class Composite : public Component {
35+
private:
36+
vector<Component*> m_component;
37+
public:
38+
Composite(string strName) : Component(strName) {}
39+
virtual void Add(Component* com) {
40+
m_component.push_back(com);
41+
}
42+
virtual void Display(int nDepth) {
43+
string strtemp;
44+
45+
for (int i = 0; i < nDepth; i++) {
46+
strtemp += "-";
47+
}
48+
49+
strtemp += m_strName;
50+
cout << strtemp << endl;
51+
vector<Component*>::iterator p = m_component.begin();
52+
53+
while (p != m_component.end()) {
54+
(*p)->Display(nDepth + 2);
55+
p++;
56+
}
57+
}
58+
};
59+
60+
//用戶端
61+
int main() {
62+
Composite* p = new Composite("小王");
63+
p->Add(new Leaf("小李"));
64+
p->Add(new Leaf("小趙"));
65+
Composite* p1 = new Composite("小小五");
66+
p1->Add(new Leaf("大三"));
67+
p->Add(p1);
68+
p->Display(1);
69+
return 0;
70+
}
71+
96.1 KB
Binary file not shown.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
using namespace std;
5+
6+
class Company {
7+
protected:
8+
string m_strName;
9+
public:
10+
Company(string strName) {
11+
m_strName = strName;
12+
}
13+
virtual void Add(Company* c) = 0;
14+
virtual void Display(int nDepth) = 0;
15+
virtual void LineOfDuty() = 0;
16+
};
17+
18+
class ConcreteCompany: public Company {
19+
private:
20+
vector<Company*> m_company;
21+
public:
22+
ConcreteCompany(string strName): Company(strName) {}
23+
virtual void Add(Company* c) {
24+
m_company.push_back(c);
25+
}
26+
virtual void Display(int nDepth) {
27+
string strtemp;
28+
29+
for (int i = 0; i < nDepth; i++) {
30+
strtemp += "-";
31+
}
32+
33+
strtemp += m_strName;
34+
cout << strtemp << endl;
35+
vector<Company*>::iterator p = m_company.begin();
36+
37+
while (p != m_company.end()) {
38+
(*p)->Display(nDepth + 2);
39+
p++;
40+
}
41+
}
42+
virtual void LineOfDuty() {
43+
vector<Company*>::iterator p = m_company.begin();
44+
45+
while (p != m_company.end()) {
46+
(*p)->LineOfDuty();
47+
p++;
48+
}
49+
}
50+
};
51+
52+
class HrDepartment : public Company {
53+
public:
54+
HrDepartment(string strname) : Company(strname) {}
55+
virtual void Display(int nDepth) {
56+
string strtemp;
57+
58+
for (int i = 0; i < nDepth; i++) {
59+
strtemp += "-";
60+
}
61+
62+
strtemp += m_strName;
63+
cout << strtemp << endl;
64+
}
65+
virtual void Add(Company* c) {
66+
cout << "error" << endl;
67+
}
68+
virtual void LineOfDuty() {
69+
cout << m_strName << ":招聘人才" << endl;
70+
}
71+
};
72+
73+
//用戶端:
74+
int main() {
75+
ConcreteCompany* p = new ConcreteCompany("清華大學");
76+
p->Add(new HrDepartment("清華大學人才部"));
77+
ConcreteCompany* p1 = new ConcreteCompany("數學系");
78+
p1->Add(new HrDepartment("數學系人才部"));
79+
ConcreteCompany* p2 = new ConcreteCompany("物理系");
80+
p2->Add(new HrDepartment("物理系人才部"));
81+
p->Add(p1);
82+
p->Add(p2);
83+
p->Display(1);
84+
p->LineOfDuty();
85+
return 0;
86+
}

大話/note/code/Interpreter

95.8 KB
Binary file not shown.

0 commit comments

Comments
 (0)