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 < string>
2+ #include < iostream>
3+ using namespace std ;
4+
5+ // 實例基類,相當於 Product(為了方便,沒用抽象)
6+ class LeiFeng {
7+ public:
8+ virtual void Sweep () {
9+ cout << " 雷鋒掃地" << endl;
10+ }
11+
12+ virtual ~LeiFeng () {
13+ }
14+ };
15+
16+ // 學雷鋒的大學生,相當於 ConcreteProduct
17+ class Student : public LeiFeng {
18+ public:
19+ virtual void Sweep () {
20+ cout << " 大學生掃地" << endl;
21+ }
22+ };
23+
24+ // 學雷鋒的志願者,相當於 ConcreteProduct
25+ class Volenter : public LeiFeng {
26+ public :
27+ virtual void Sweep () {
28+ cout << " 志願者" << endl;
29+ }
30+ };
31+
32+ // 工廠基類 Creator
33+ class LeiFengFactory {
34+ public:
35+ virtual LeiFeng* CreateLeiFeng () {
36+ return new LeiFeng ();
37+ }
38+ virtual ~LeiFengFactory () {
39+ }
40+ };
41+
42+ // 工廠具體類
43+ class StudentFactory : public LeiFengFactory {
44+ public :
45+ virtual LeiFeng* CreateLeiFeng () {
46+ return new Student ();
47+ }
48+ };
49+
50+ class VolenterFactory : public LeiFengFactory {
51+ public:
52+ virtual LeiFeng* CreateLeiFeng () {
53+ return new Volenter ();
54+ }
55+ };
56+
57+ // 用戶端
58+ int main () {
59+ LeiFengFactory* sf = new LeiFengFactory ();
60+ LeiFeng* s = sf->CreateLeiFeng ();
61+ s->Sweep ();
62+ delete s;
63+ delete sf;
64+ return 0 ;
65+ }
66+
Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < iostream>
3+ #include < vector>
4+ using namespace std ;
5+
6+ class Secretary ;
7+ // 看股票的同事類(觀察對象,觀察者)
8+ class StockObserver {
9+ private:
10+ string name;
11+ Secretary* sub;
12+ public:
13+ StockObserver (string strname, Secretary* strsub) {
14+ name = strname;
15+ sub = strsub;
16+ }
17+ void Update ();
18+ };
19+
20+ // 秘書類(主題物件,通知者)
21+ class Secretary {
22+ private:
23+ vector<StockObserver> observers;
24+ public:
25+ string action;
26+ void Add (StockObserver ob) {
27+ observers.push_back (ob);
28+ }
29+ void Notify () {
30+ vector<StockObserver>::iterator p = observers.begin ();
31+
32+ while (p != observers.end ()) {
33+ (*p).Update ();
34+ p++;
35+ }
36+ }
37+ };
38+
39+ void StockObserver::Update () {
40+ cout << name << " :" << sub->action << " ,不要玩股票了,要開始工作了" << endl;
41+ }
42+
43+ // 用戶端
44+ int main () {
45+ Secretary* p = new Secretary ();
46+ // 創建通知者
47+ // 觀察者
48+ StockObserver* s1 = new StockObserver (" 小李" , p);
49+ StockObserver* s2 = new StockObserver (" 小趙" , p);
50+ // 加入通知佇列
51+ p->Add (*s1);
52+ p->Add (*s2);
53+ // 事件
54+ p->action = " 老闆來了" ;
55+ // 通知
56+ p->Notify ();
57+ return 0 ;
58+ }
59+
Original file line number Diff line number Diff line change 1+ #include < iostream>
2+ #include < vector>
3+ #include < string>
4+ using namespace std ;
5+
6+ class Prototype { // 抽象基類
7+ private:
8+ string m_strName;
9+ public:
10+ Prototype (string strName) {
11+ m_strName = strName;
12+ }
13+ Prototype () {
14+ m_strName = " " ;
15+ }
16+ void Show () {
17+ cout << m_strName << endl;
18+ }
19+ virtual Prototype* Clone () = 0 ;
20+ };
21+
22+ // class ConcretePrototype1
23+ class ConcretePrototype1 : public Prototype {
24+ public:
25+ ConcretePrototype1 (string strName) : Prototype(strName) {}
26+ ConcretePrototype1 () {}
27+ virtual Prototype* Clone () {
28+ ConcretePrototype1* p = new ConcretePrototype1 () ;
29+ *p = *this ; // 複製對象
30+ return p ;
31+ }
32+ };
33+
34+ // class ConcretePrototype2
35+ class ConcretePrototype2 : public Prototype {
36+ public:
37+ ConcretePrototype2 (string strName) : Prototype(strName) {}
38+ ConcretePrototype2 () {}
39+ virtual Prototype* Clone () {
40+ ConcretePrototype2* p = new ConcretePrototype2 () ;
41+ *p = *this ; // 複製對象
42+ return p ;
43+ }
44+ };
45+
46+ // 用戶端
47+ int main () {
48+ ConcretePrototype1* test = new ConcretePrototype1 (" 小王" );
49+ ConcretePrototype2* test2 = (ConcretePrototype2*)test->Clone ();
50+ test->Show ();
51+ test2->Show ();
52+ return 0 ;
53+ }
54+
Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < iostream>
3+ #include < vector>
4+ using namespace std ;
5+
6+ // 最終的產品類
7+ class Product {
8+ private:
9+ vector<string> m_product;
10+ public:
11+ void Add (string strtemp) {
12+ m_product.push_back (strtemp);
13+ }
14+ void Show () {
15+ vector<string>::iterator p = m_product.begin ();
16+
17+ while (p != m_product.end ()) {
18+ cout << *p << endl;
19+ p++;
20+ }
21+ }
22+ };
23+
24+ // 建造者基類
25+ class Builder {
26+ public:
27+ virtual void BuilderA () = 0;
28+ virtual void BuilderB () = 0;
29+ virtual Product* GetResult () = 0;
30+ };
31+ // 第一種建造方式
32+ class ConcreteBuilder1 : public Builder {
33+ private:
34+ Product* m_product;
35+ public:
36+ ConcreteBuilder1 () {
37+ m_product = new Product ();
38+ }
39+ virtual void BuilderA () {
40+ m_product->Add (" one" );
41+ }
42+ virtual void BuilderB () {
43+ m_product->Add (" two" );
44+ }
45+ virtual Product* GetResult () {
46+ return m_product;
47+ }
48+ };
49+
50+ // 第二種建造方式
51+ class ConcreteBuilder2 : public Builder {
52+ private:
53+ Product* m_product;
54+ public:
55+ ConcreteBuilder2 () {
56+ m_product = new Product ();
57+ }
58+ virtual void BuilderA () {
59+ m_product->Add (" A" );
60+ }
61+ virtual void BuilderB () {
62+ m_product->Add (" B" );
63+ }
64+ virtual Product* GetResult () {
65+ return m_product;
66+ }
67+ };
68+
69+ // 指揮者類
70+ class Direct {
71+ public:
72+ void Construct (Builder* temp) {
73+ temp->BuilderA ();
74+ temp->BuilderB ();
75+ }
76+ };
77+
78+ // 用戶端
79+ int main () {
80+ Direct* p = new Direct ();
81+ Builder* b1 = new ConcreteBuilder1 ();
82+ Builder* b2 = new ConcreteBuilder2 ();
83+ p->Construct (b1);
84+ // 調用第一種方式
85+ Product* pb1 = b1->GetResult ();
86+ pb1->Show ();
87+ p->Construct (b2);
88+ // 調用第二種方式
89+ Product* pb2 = b2->GetResult ();
90+ pb2->Show ();
91+ return 0 ;
92+ }
93+
Original file line number Diff line number Diff line change 1+ #include < string>
2+ #include < iostream>
3+ #include < vector>
4+ using namespace std ;
5+
6+ class Person {
7+ public:
8+ virtual void CreateHead () = 0;
9+ virtual void CreateHand () = 0;
10+ virtual void CreateBody () = 0;
11+ virtual void CreateFoot () = 0;
12+ virtual ~Person () {
13+ }
14+ };
15+
16+ class ThinPerson : public Person {
17+ public:
18+ virtual void CreateHead () {
19+ cout << " thin head" << endl;
20+ }
21+ virtual void CreateHand () {
22+ cout << " thin hand" << endl;
23+ }
24+ virtual void CreateBody () {
25+ cout << " thin body" << endl;
26+ }
27+ virtual void CreateFoot () {
28+ cout << " thin foot" << endl;
29+ }
30+ };
31+
32+ class ThickPerson : public Person {
33+ public:
34+ virtual void CreateHead () {
35+ cout << " ThickPerson head" << endl;
36+ }
37+ virtual void CreateHand () {
38+ cout << " ThickPerson hand" << endl;
39+ }
40+ virtual void CreateBody () {
41+ cout << " ThickPerson body" << endl;
42+ }
43+ virtual void CreateFoot () {
44+ cout << " ThickPerson foot" << endl;
45+ }
46+ };
47+
48+ // 指揮者類
49+ class Direct {
50+ private:
51+ Person* p;
52+ public:
53+ Direct (Person* temp) {
54+ p = temp;
55+ }
56+ void Create () {
57+ p->CreateHead ();
58+ p->CreateBody ();
59+ p->CreateHand ();
60+ p->CreateFoot ();
61+ }
62+ };
63+
64+ // 用戶端代碼:
65+ int main () {
66+ Person* p = new ThickPerson ();
67+ Direct* d = new Direct (p);
68+ d->Create ();
69+ delete d;
70+ delete p;
71+ return 0 ;
72+ }
73+
You can’t perform that action at this time.
0 commit comments