Skip to content

Commit b4cc3e5

Browse files
author
jason_yao
committed
update
1 parent 5dec106 commit b4cc3e5

12 files changed

Lines changed: 409 additions & 0 deletions

大話/note/code/AbstractFactory

35.9 KB
Binary file not shown.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <vector>
4+
using namespace std;
5+
//使用者抽象介面
6+
class IUser {
7+
public :
8+
virtual void GetUser() = 0;
9+
virtual void InsertUser() = 0;
10+
};
11+
12+
//部門抽象介面
13+
class IDepartment {
14+
public:
15+
virtual void GetDepartment() = 0;
16+
virtual void InsertDepartment() = 0;
17+
};
18+
19+
//ACCESS 用戶
20+
class CAccessUser : public IUser {
21+
public:
22+
virtual void GetUser() {
23+
cout << "Access GetUser" << endl;
24+
}
25+
virtual void InsertUser() {
26+
cout << "Access InsertUser" << endl;
27+
}
28+
};
29+
30+
//ACCESS 部門
31+
class CAccessDepartment : public IDepartment {
32+
public:
33+
virtual void GetDepartment() {
34+
cout << "Access GetDepartment" << endl;
35+
}
36+
virtual void InsertDepartment() {
37+
cout << "Access InsertDepartment" << endl;
38+
}
39+
};
40+
41+
//SQL 用戶
42+
class CSqlUser : public IUser {
43+
public:
44+
virtual void GetUser() {
45+
cout << "Sql User" << endl;
46+
}
47+
virtual void InsertUser() {
48+
cout << "Sql User" << endl;
49+
}
50+
};
51+
52+
//SQL 部門類
53+
class CSqlDepartment: public IDepartment {
54+
public:
55+
virtual void GetDepartment() {
56+
cout << "sql getDepartment" << endl;
57+
}
58+
virtual void InsertDepartment() {
59+
cout << "sql insertdepartment" << endl;
60+
}
61+
};
62+
63+
//抽象工廠
64+
class IFactory {
65+
public:
66+
virtual IUser* CreateUser() = 0;
67+
virtual IDepartment* CreateDepartment() = 0;
68+
};
69+
70+
//ACCESS 工廠
71+
class AccessFactory : public IFactory {
72+
public:
73+
virtual IUser* CreateUser() {
74+
return new CAccessUser();
75+
}
76+
virtual IDepartment* CreateDepartment() {
77+
return new CAccessDepartment();
78+
}
79+
};
80+
81+
//SQL 工廠
82+
class SqlFactory : public IFactory {
83+
public:
84+
virtual IUser* CreateUser() {
85+
return new CSqlUser();
86+
}
87+
virtual IDepartment* CreateDepartment() {
88+
return new CSqlDepartment();
89+
}
90+
};
91+
92+
// 用戶端:
93+
int main() {
94+
IFactory* factory = new SqlFactory();
95+
IUser* user = factory->CreateUser();
96+
IDepartment* depart = factory->CreateDepartment();
97+
user->GetUser();
98+
depart->GetDepartment();
99+
return 0;
100+
}

大話/note/code/AbstractObserver

97.8 KB
Binary file not shown.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <string>
2+
#include <iostream>
3+
#include <vector>
4+
using namespace std;
5+
6+
class SecretaryBase;
7+
//抽象觀察者
8+
class CObserverBase {
9+
protected:
10+
string name;
11+
SecretaryBase* sub;
12+
public:
13+
CObserverBase(string strname, SecretaryBase* strsub) {
14+
name = strname;
15+
sub = strsub;
16+
}
17+
virtual void Update() = 0;
18+
};
19+
20+
//具體的觀察者,看股票的
21+
class StockObserver : public CObserverBase {
22+
public:
23+
StockObserver(string strname, SecretaryBase* strsub) : CObserverBase(strname, strsub) {
24+
}
25+
virtual void Update();
26+
};
27+
28+
//具體觀察者,看 NBA 的
29+
class NBAObserver : public CObserverBase {
30+
public:
31+
NBAObserver(string strname, SecretaryBase* strsub) : CObserverBase(strname, strsub) {
32+
}
33+
virtual void Update();
34+
};
35+
36+
//抽象通知者
37+
class SecretaryBase {
38+
public:
39+
string action;
40+
vector<CObserverBase*> observers;
41+
public:
42+
virtual void Attach(CObserverBase* observer) = 0;
43+
virtual void Notify() = 0;
44+
};
45+
46+
//具體通知者
47+
class Secretary : public SecretaryBase {
48+
public:
49+
void Attach(CObserverBase* ob) {
50+
observers.push_back(ob);
51+
}
52+
void Notify() {
53+
vector<CObserverBase*>::iterator p = observers.begin();
54+
55+
while (p != observers.end()) {
56+
(*p)->Update();
57+
p++;
58+
}
59+
}
60+
};
61+
62+
void StockObserver::Update() {
63+
cout << name << ":" << sub->action << ",不要玩股票了,要開始工作了" << endl;
64+
}
65+
66+
void NBAObserver::Update() {
67+
cout << name << ":" << sub->action << ",不要看 NBA 了,老闆來了" << endl;
68+
}
69+
70+
// 用戶端:
71+
int main() {
72+
SecretaryBase* p = new Secretary(); //創建觀察者
73+
//被觀察的對象
74+
CObserverBase* s1 = new NBAObserver("小李", p);
75+
CObserverBase* s2 = new StockObserver("小趙", p);
76+
//加入觀察佇列
77+
p->Attach(s1);
78+
p->Attach(s2);
79+
//事件
80+
p->action = "老闆來了";
81+
//通知
82+
p->Notify();
83+
return 0;
84+
}
85+

大話/note/code/Adapter_exmpale1

33.3 KB
Binary file not shown.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
class Target {
5+
public:
6+
virtual void Request() {
7+
cout << "普通的請求" << endl;
8+
}
9+
virtual ~Target() {
10+
11+
}
12+
};
13+
14+
class Adaptee {
15+
public:
16+
void SpecificalRequest() {
17+
cout << "特殊請求" << endl;
18+
}
19+
};
20+
21+
class Adapter : public Target {
22+
private:
23+
Adaptee* ada;
24+
public:
25+
virtual void Request() {
26+
ada->SpecificalRequest();
27+
Target::Request();
28+
}
29+
Adapter() {
30+
ada = new Adaptee();
31+
}
32+
~Adapter() {
33+
delete ada;
34+
}
35+
};
36+
37+
// 用戶端:
38+
int main() {
39+
Adapter* ada = new Adapter();
40+
ada->Request();
41+
delete ada;
42+
return 0;
43+
}
44+

大話/note/code/Adapter_exmpale2

50.4 KB
Binary file not shown.
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
class Player {
6+
protected:
7+
string name;
8+
public:
9+
Player(string strName) {
10+
name = strName;
11+
}
12+
virtual void Attack() = 0;
13+
virtual void Defense() = 0;
14+
};
15+
16+
class Forwards : public Player {
17+
public:
18+
Forwards(string strName): Player(strName) {}
19+
public:
20+
virtual void Attack() {
21+
cout << name << "前鋒進攻" << endl;
22+
}
23+
virtual void Defense() {
24+
cout << name << "前鋒防守" << endl;
25+
}
26+
};
27+
28+
class Center : public Player {
29+
public:
30+
Center(string strName): Player(strName) {}
31+
public:
32+
virtual void Attack() {
33+
cout << name << "中場進攻" << endl;
34+
}
35+
virtual void Defense() {
36+
cout << name << "中場防守" << endl;
37+
}
38+
};
39+
40+
//為中場翻譯
41+
class TransLater: public Player {
42+
private:
43+
Center* player;
44+
public:
45+
TransLater(string strName): Player(strName) {
46+
player = new Center(strName);
47+
}
48+
virtual void Attack() {
49+
player->Attack();
50+
}
51+
virtual void Defense() {
52+
player->Defense();
53+
}
54+
};
55+
56+
// 用戶端
57+
int main() {
58+
Player* p = new TransLater("小李");
59+
p->Attack();
60+
return 0;
61+
}
62+

大話/note/code/Memento

48.3 KB
Binary file not shown.

大話/note/code/Memento.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
class Memo;
6+
//發起人類
7+
class Originator {
8+
public:
9+
string state;
10+
Memo* CreateMemo();
11+
void SetMemo(Memo* memo);
12+
void Show() {
13+
cout << "狀態:" << state << endl;
14+
}
15+
};
16+
17+
//備忘錄類
18+
class Memo {
19+
public:
20+
string state;
21+
Memo(string strState) {
22+
state = strState;
23+
}
24+
};
25+
26+
Memo* Originator::CreateMemo() {
27+
return new Memo(state);
28+
}
29+
30+
void Originator::SetMemo(Memo* memo) {
31+
state = memo->state;
32+
}
33+
34+
//管理者類
35+
class Caretaker {
36+
public:
37+
Memo* memo;
38+
};
39+
40+
// 用戶端:
41+
int main() {
42+
Originator* on = new Originator();
43+
on->state = "on";
44+
on->Show();
45+
Caretaker* c = new Caretaker();
46+
c->memo = on->CreateMemo();
47+
on->state = "off";
48+
on->Show();
49+
on->SetMemo(c->memo);
50+
on->Show();
51+
return 0;
52+
}

0 commit comments

Comments
 (0)