青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

posts - 183,  comments - 10,  trackbacks - 0
 

設計模式的分類:
·創建型
·結構型
·行為型

1.創建型模式
·簡單工廠模式 (Simple Factory)
·工廠方法模式 (Factory Method)
·抽象工廠模式 (Abstract Factory)
·建造者模式      (Builder)
·原型模式          (Prototype)
·單例模式          (Singleton)

2.結構型模式
·外觀模式          (Facade)
·適配器模式      (Adapter)
·代理模式          (Proxy)
·裝飾模式          (Decorator)
·橋接模式          (Bridge)
·組合模式          (Composite)
·享元模式          (Flyweight)

3.行為型模式
·模板方法模式 (Template Method)
·觀察者模式      (Observer)
·狀態模式          (State)
·策略模式          (Strategy)
·職責鏈模式      (Chain of Responsibility)
·命令模式          (Command)
·訪問者模式      (Visitor)
·調停者模式      (Mediator)
·備忘錄模式      (Memento)
·迭代器模式      (Iterator)
·解釋器模式      (Interpreter)

posted @ 2011-05-01 18:55 unixfy 閱讀(192) | 評論 (0)編輯 收藏

搜索引擎會通過日志文件把用戶每次檢索使用的所有檢索串都記錄下來,每個查詢串的長度為1-255字節。
假設目前有一千萬個記錄(這些查詢串的重復度比較高,雖然總數是1千萬,但如果除去重復后,不超過3百萬個。一個查詢串的重復度越高,說明查詢它的用戶越多,也就是越熱門。請你統計最熱門的10個查詢串,要求使用的內存不能超過1G。


先統計所有查詢的次數,所有查詢有 300 萬個,255 * 300 * 10000B = 765 MB,可以存入內存。這里使用 STL 中的 map。所得時間復雜度為 O(NlogM),N 為所有的查詢,包括重復的,M 為不重復的查詢。更好的方法是用散列。

然后遍歷 map,維護一個大小為 10 的集合,在遍歷 map 時,比較當前查詢的出現次數與集合中出現次數最小的查詢的出現此時比較,如果大于,將當前查詢替換到集合中。
這里的集合還是用的 map,時間復雜度為 O(MlogK),這里 K = 10。

總的時間復雜度為 O(NlogM) + O(MlogK)


也可以將這個過程合二為一。即每次在統計的過程中,查詢大小為 K 的集合。如果符合條件,則將當前查詢替換到集合中。但是還要考慮實時更新集合中的元素。
這種方法的時間復雜度為 O(N(logM + logK + K))。

由于第二種方法還得考慮實時更新。效率遠沒有第一種方案高。

實現:

 1 #include <iostream>
 2 #include <fstream>
 3 #include <map>
 4 #include <string>
 5 using namespace std;
 6 
 7 void statistics(map<stringint>& data, const string& query)
 8 {
 9     ++data[query];
10 }
11 
12 void findTopK(multimap<intstring>& topK, int k, const map<stringint>& data)
13 {
14     topK.clear();
15     for (map<stringint>::const_iterator cit = data.begin(); cit != data.end(); ++cit)
16     {
17         if (topK.size() < k)
18         {
19             topK.insert(make_pair(cit->second, cit->first));
20         }
21         else
22         {
23             if (cit->second > topK.begin()->first)
24             {
25                 topK.erase(topK.begin());
26                 topK.insert(make_pair(cit->second, cit->first));
27             }
28         }
29     }
30 }
31 
32 int main()
33 {
34     ifstream fin("queryfile.txt");
35     map<stringint> data;
36     multimap<intstring> top10;
37     string query;
38     while (getline(fin, query))
39     {
40         statistics(data, query);
41     }
42 
43     //for (map<string, int>::const_iterator cit = data.begin(); cit != data.end(); ++cit)
44     //{
45     //    cout << cit->first << '\t' << cit->second << endl;
46     //}
47 
48     //cout << endl;
49     findTopK(top10, 10, data);
50 
51     for (multimap<intstring>::const_reverse_iterator cit = top10.rbegin(); cit != top10.rend(); ++cit)
52     {
53         cout << cit->second << '\t' << cit->first << endl;
54     }
55 
56     return 0;
57 }

http://blog.donews.com/jiji262/2011/03/baidu_top_k_interview/
http://blog.redfox66.com/post/2010/09/23/top-k-algoriyhm-analysis.aspx
http://blog.csdn.net/jasonblog/archive/2010/08/19/5825026.aspx
posted @ 2011-04-30 18:06 unixfy 閱讀(210) | 評論 (0)編輯 收藏

來自于《大話設計模式》
訪問者模式(Visitor):
表示一個作用于某對象結構中的個元素的操作。它是你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。

行為型

UML 類圖:



代碼實現 C++:
  1 #include <iostream>
  2 #include <list>
  3 #include <algorithm>
  4 #include <string>
  5 using namespace std;
  6 
  7 class Action;
  8 
  9 class Person
 10 {
 11 protected:
 12     string name;
 13 public:
 14     Person(const string& s = "Person") : name(s) {}
 15     virtual void Accept(Action* visitor) = 0;
 16     virtual string getName()
 17     {
 18         return name;
 19     }
 20 };
 21 
 22 class Man;
 23 class Woman;
 24 
 25 class Action
 26 {
 27 protected:
 28     string name;
 29 public:
 30     Action(const string& s = "Action") : name(s) {}
 31     virtual void GetManConclusion(Man* m) = 0;
 32     virtual void GetWomanConclusion(Woman* w) = 0;
 33 };
 34 
 35 class Man : public Person
 36 {
 37 public:
 38     Man(const string& s = "Man") : Person(s) {}
 39     virtual void Accept(Action* visitor)
 40     {
 41         visitor->GetManConclusion(this);
 42     }
 43 };
 44 
 45 class Woman : public Person
 46 {
 47 public:
 48     Woman(const string& s = "Woman") : Person(s) {}
 49     virtual void Accept(Action* visitor)
 50     {
 51         visitor->GetWomanConclusion(this);
 52     }
 53 };
 54 
 55 class Success : public Action
 56 {
 57 public:
 58     Success(const string& s = "Success") : Action(s) {}
 59     virtual void GetManConclusion(Man* m)
 60     {
 61         cout << name << endl;
 62         cout << m->getName() << endl;
 63         cout << "1" << endl;
 64     }
 65     virtual void GetWomanConclusion(Woman* w)
 66     {
 67         cout << name << endl;
 68         cout << w->getName() << endl;
 69         cout << "2" << endl;
 70     }
 71 };
 72 
 73 class Failing : public Action
 74 {
 75 public:
 76     Failing(const string& s = "Failing") : Action(s) {}
 77     virtual void GetManConclusion(Man* m)
 78     {
 79         cout << name << endl;
 80         cout << m->getName() << endl;
 81         cout << "3" << endl;
 82     }
 83     virtual void GetWomanConclusion(Woman* w)
 84     {
 85         cout << name << endl;
 86         cout << w->getName() << endl;
 87         cout << "4" << endl;
 88     }
 89 };
 90 
 91 class Amativeness : public Action
 92 {
 93 public:
 94     Amativeness(const string& s = "Amativeness") : Action(s) {}
 95     virtual void GetManConclusion(Man* m)
 96     {
 97         cout << name << endl;
 98         cout << m->getName() << endl;
 99         cout << "5" << endl;
100     }
101     virtual void GetWomanConclusion(Woman* w)
102     {
103         cout << name << endl;
104         cout << w->getName() << endl;
105         cout << "6" << endl;
106     }
107 };
108 
109 class Marriage : public Action
110 {
111 public:
112     Marriage(const string& s = "Marriage") : Action(s) {}
113     virtual void GetManConclusion(Man* m)
114     {
115         cout << name << endl;
116         cout << m->getName() << endl;
117         cout << "7" << endl;
118     }
119     virtual void GetWomanConclusion(Woman* w)
120     {
121         cout << name << endl;
122         cout << w->getName() << endl;
123         cout << "8" << endl;
124     }
125 };
126 
127 class ObjectStructure
128 {
129 private:
130     list<Person*> elements;
131 public:
132     ObjectStructure() {}
133     ~ObjectStructure()
134     {
135         for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
136         {
137             delete (*iter);
138         }
139     }
140     void Attach(Person* element)
141     {
142         elements.push_back(element);
143     }
144     void Detach(Person* element)
145     {
146         list<Person*>::iterator iter = find(elements.begin(), elements.end(), element);
147         if (iter != elements.end())
148         {
149             elements.erase(iter);
150         }
151     }
152     void Display(Action* visitor)
153     {
154         for (list<Person*>::iterator iter = elements.begin(); iter != elements.end(); ++iter)
155         {
156             (*iter)->Accept(visitor);
157         }
158     }
159     int size()
160     {
161         return elements.size();
162     }
163 };
164 
165 int main()
166 {
167     ObjectStructure o;
168     o.Attach(new Man);
169     o.Attach(new Woman);
170 
171     cout << o.size() << endl;
172 
173     Success* v1 = new Success;
174     o.Display(v1);
175     Failing* v2 = new Failing;
176     o.Display(v2);
177     Amativeness* v3 = new Amativeness;
178     o.Display(v3);
179     Marriage* v4 = new Marriage;
180     o.Display(v4);
181 
182     delete v4;
183     delete v3;
184     delete v2;
185     delete v1;
186 
187     return 0;
188 }
posted @ 2011-04-30 15:21 unixfy 閱讀(466) | 評論 (0)編輯 收藏

來自于《大話設計模式》
解釋器模式(Interpreter):
給定一個語言,定義它的文法的一種表示,并定義一個解釋器,這個解釋器使用該表示來解釋語言中的句子。

行為型


UML 類圖:



代碼實現 C++:
 1 #include <iostream>
 2 #include <string>
 3 #include <list>
 4 using namespace std;
 5 
 6 class Context
 7 {
 8 private:
 9     string input;
10     string output;
11 public:
12     string getInput()
13     {
14         return input;
15     }
16     void setInput(const string& s)
17     {
18         input = s;
19     }
20     string getOutput()
21     {
22         return output;
23     }
24     void setOutput(const string& s)
25     {
26         output = s;
27     }
28 };
29 
30 class AbstractExpression
31 {
32 public:
33     virtual void Interpret(Context* context) = 0;
34 };
35 
36 class TerminalExpression : public AbstractExpression
37 {
38 public:
39     virtual void Interpret(Context* context)
40     {
41         context->setOutput("終端解釋器:" + context->getInput());
42         cout << context->getOutput() << endl;
43     }
44 };
45 
46 class NonterminalExpression : public AbstractExpression
47 {
48 public:
49     virtual void Interpret(Context* context)
50     {
51         context->setOutput("非終端解釋器:" + context->getInput());
52         cout << context->getOutput() << endl;
53     }
54 };
55 
56 int main()
57 {
58     Context* context = new Context;
59     context->setInput("abc");
60     list<AbstractExpression*> lae;
61 
62     lae.push_back(new TerminalExpression);
63     lae.push_back(new NonterminalExpression);
64     lae.push_back(new TerminalExpression);
65     lae.push_back(new TerminalExpression);
66 
67     for (list<AbstractExpression*>::iterator iter = lae.begin(); iter != lae.end(); ++iter)
68     {
69         (*iter)->Interpret(context);
70     }
71 
72     for (list<AbstractExpression*>::iterator iter = lae.begin(); iter != lae.end(); ++iter)
73     {
74         delete (*iter);
75     }
76     delete context;
77 
78     return 0;
79 }
posted @ 2011-04-30 14:32 unixfy 閱讀(949) | 評論 (2)編輯 收藏

來自于《大話設計模式》
享元模式(Flyweight):
運用共享技術有效地支持大量細粒度的對象。

結構型

UML 類圖:




代碼實現 C++:
  1 #include <iostream>
  2 #include <map>
  3 #include <string>
  4 using namespace std;
  5 
  6 class User
  7 {
  8 private:
  9     string name;
 10 public:
 11     User(const string& s) : name(s) {}
 12     string getName()
 13     {
 14         return name;
 15     }
 16 };
 17 
 18 class WebSite
 19 {
 20 public:
 21     WebSite() {}
 22     virtual ~WebSite() {}
 23     virtual void Use(User* user) = 0;
 24 };
 25 
 26 class ConcreteWebSite : public WebSite
 27 {
 28 private:
 29     string name;
 30 public:
 31     ConcreteWebSite() {}
 32     virtual ~ConcreteWebSite() {}
 33     ConcreteWebSite(const string& s) : name(s){}
 34     virtual void Use(User* user)
 35     {
 36         cout << "網站分類:" << name << " 用戶:" << user->getName() << endl;
 37     }
 38 };
 39 
 40 class WebSiteFactory
 41 {
 42 private:
 43     map<string, WebSite*> flyweights;
 44 public:
 45     ~WebSiteFactory()
 46     {
 47         for (map<string, WebSite*>::iterator iter = flyweights.begin(); iter != flyweights.end(); ++iter)
 48         {
 49             delete iter->second;
 50         }
 51     }
 52     WebSite* GetWebSiteCategory(const string& key)
 53     {
 54         map<string, WebSite*>::iterator iter = flyweights.find(key);
 55         if (iter != flyweights.end())
 56         {
 57             return iter->second;
 58         }
 59         else
 60         {
 61             flyweights.insert(make_pair(key, new ConcreteWebSite(key)));
 62             return flyweights[key];
 63         }
 64     }
 65     int GetWebSiteCount()
 66     {
 67         return flyweights.size();
 68     }
 69 };
 70 
 71 int main()
 72 {
 73     WebSiteFactory* f = new WebSiteFactory;
 74     cout << f->GetWebSiteCount() << endl;
 75     WebSite* fx = f->GetWebSiteCategory("產品展示");
 76     cout << f->GetWebSiteCount() << endl;
 77     User* user = new User("小菜");
 78     fx->Use(user);
 79     delete user;
 80 
 81     WebSite* fy = f->GetWebSiteCategory("產品展示");
 82     cout << f->GetWebSiteCount() << endl;
 83     user = new User("大鳥");
 84     fy->Use(user);
 85     delete user;
 86 
 87     WebSite* fz = f->GetWebSiteCategory("產品展示");
 88     cout << f->GetWebSiteCount() << endl;
 89     user = new User("嬌嬌");
 90     fz->Use(user);
 91     delete user;
 92 
 93     WebSite* fl = f->GetWebSiteCategory("博客");
 94     cout << f->GetWebSiteCount() << endl;
 95     user = new User("老頑童");
 96     fl->Use(user);
 97     delete user;
 98 
 99     WebSite* fm = f->GetWebSiteCategory("博客");
100     cout << f->GetWebSiteCount() << endl;
101     user = new User("桃谷六仙");
102     fm->Use(user);
103     delete user;
104 
105     WebSite* fn = f->GetWebSiteCategory("博客");
106     cout << f->GetWebSiteCount() << endl;
107     user = new User("南海鱷神");
108     fn->Use(user);
109     delete user;
110 
111     cout << "網站分類總數為:" << f->GetWebSiteCount() << endl;
112     delete f;
113     return 0;
114 }
posted @ 2011-04-30 14:09 unixfy 閱讀(245) | 評論 (0)編輯 收藏

來自于《大話設計模式》
終結者模式(Mediator):
用一個中介對象來封裝一系列的對象交互。中介者使各對象不需要顯式地相互作用,從而使其耦合松散,而且可以獨立地改變他們之間的交互。

行為型

UML 類圖:


代碼實現 C++:
 1 #include <iostream>
 2 #include <string>
 3 using namespace std;
 4 
 5 class Country;
 6 
 7 class UnitedNations
 8 {
 9 public:
10     virtual void Declare(const string& message, Country* colleague) = 0;
11     virtual void setColleague1(Country* c1) = 0;
12     virtual void setColleague2(Country* c2) = 0;
13 };
14 
15 class Country
16 {
17 protected:
18     UnitedNations* mediator;
19 public:
20     Country(UnitedNations* un) : mediator(un) {}
21     virtual void Declare(const string& message) = 0;
22     virtual void GetMessage(const string& message) = 0;
23 };
24 
25 class USA : public Country
26 {
27 public:
28     USA(UnitedNations* un) : Country(un) {}
29     void Declare(const string& message)
30     {
31         mediator->Declare(message, this);
32     }
33     void GetMessage(const string& message)
34     {
35         cout << "美國獲得對方信息:" << message << endl;
36     }
37 };
38 
39 class Iraq : public Country
40 {
41 public:
42     Iraq(UnitedNations* un) : Country(un) {}
43     void Declare(const string& message)
44     {
45         mediator->Declare(message, this);
46     }
47     void GetMessage(const string& message)
48     {
49         cout << "伊拉克獲得對方信息:" << message << endl;
50     }
51 };
52 
53 class UnitedNationsSecurityCouncil : public UnitedNations
54 {
55 private:
56     Country* colleague1;
57     Country* colleague2;
58 public:
59     void setColleague1(Country* u)
60     {
61         colleague1 = u;
62     }
63     void setColleague2(Country* i)
64     {
65         colleague2 = i;
66     }
67     virtual void Declare(const string& message, Country* colleague)
68     {
69         if (colleague == colleague1)
70         {
71             colleague2->GetMessage(message);
72         }
73         else
74         {
75             colleague1->GetMessage(message);
76         }
77     }
78 };
79 
80 int main()
81 {
82     UnitedNationsSecurityCouncil* UNSC = new UnitedNationsSecurityCouncil;
83     USA* c1 = new USA(UNSC);
84     Iraq* c2 = new Iraq(UNSC);
85 
86     UNSC->setColleague1(c1);
87     UNSC->setColleague2(c2);
88 
89     c1->Declare("不準研制核武器,否則要發動戰爭!");
90     c2->Declare("我們沒有核武器,也不怕侵略!");
91 
92     delete c2;
93     delete c1;
94     delete UNSC;
95 
96     return 0;
97 }
posted @ 2011-04-30 12:02 unixfy 閱讀(518) | 評論 (0)編輯 收藏

職責鏈模式(Chain of Responsibility):
使多個對象都有機會處理請求,從而避免請求的發送者和接受者之間的耦合關系。將這個對象連成一條鏈,并沿著這條鏈傳遞該請求,直到有一個對象處理它為止。

行為型

UML 類圖:



代碼實現 C++:

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 class Request
  6 {
  7 public:
  8     string requestType;
  9     string requestContent;
 10     int    number;
 11 };
 12 
 13 class Manager
 14 {
 15 protected:
 16     string name;
 17     Manager* superior;
 18 public:
 19     Manager(const string& s) : name(s), superior(0) {}
 20     void SetSuperior(Manager* m)
 21     {
 22         superior = m;
 23     }
 24     virtual void RequestApplications(Request* request) = 0;
 25 };
 26 
 27 class CommonManager : public Manager
 28 {
 29 public:
 30     CommonManager(const string& s) : Manager(s) {}
 31     virtual void RequestApplications(Request* request)
 32     {
 33         if (request->requestType == "請假" && request->number <= 2)
 34         {
 35             cout << name << "" << request->requestContent << " 數量 " << request->number << " 被批準!" << endl;
 36         }
 37         else if (superior != 0)
 38         {
 39             superior->RequestApplications(request);
 40         }
 41         else
 42         {
 43             cout << "不能得到處理!" << endl;
 44         }
 45     }
 46 };
 47 
 48 class Majordomo : public Manager
 49 {
 50 public:
 51     Majordomo(const string& s) : Manager(s) {}
 52     virtual void RequestApplications(Request* request)
 53     {
 54         if (request->requestType == "請假" && request->number <= 5)
 55         {
 56             cout << name << "" << request->requestContent << " 數量 " << request->number << " 被批準!" << endl;
 57         }
 58         else if (superior != 0)
 59         {
 60             superior->RequestApplications(request);
 61         }
 62         else
 63         {
 64             cout << "不能得到處理!" << endl;
 65         }
 66     }
 67 };
 68 
 69 class GeneralManager : public Manager
 70 {
 71 public:
 72     GeneralManager(const string& s) : Manager(s) {}
 73     virtual void RequestApplications(Request* request)
 74     {
 75         if (request->requestType == "請假")
 76         {
 77             cout << name << "" << request->requestContent << " 數量 " << request->number << " 被批準!" << endl;
 78         }
 79         else if (request->requestType == "加薪" && request->number <= 500)
 80         {
 81             cout << name << "" << request->requestContent << " 數量 " << request->number << " 被批準!" << endl;
 82         }
 83         else if (request->requestType == "加薪" && request->number > 500)
 84         {
 85             cout << name << "" << request->requestContent << " 數量 " << request->number << " 再說吧!" << endl;
 86         }
 87         else if (superior != 0)
 88         {
 89             superior->RequestApplications(request);
 90         }
 91         else
 92         {
 93             cout << "不能得到處理!" << endl;
 94         }
 95     }
 96 };
 97 
 98 int main()
 99 {
100     CommonManager* jinli    = new CommonManager("金利");
101     Majordomo*     zongjian = new Majordomo("宗劍");
102     GeneralManager* zhongjingli = new GeneralManager("鐘精勵");
103 
104     jinli->SetSuperior(zongjian);
105     zongjian->SetSuperior(zhongjingli);
106 
107     Request* request = new Request;
108     request->requestType = "請假";
109     request->requestContent = "小菜請假";
110     request->number = 1;
111     jinli->RequestApplications(request);
112 
113     Request* request2 = new Request;
114     request2->requestType = "請假";
115     request2->requestContent = "小菜請假";
116     request2->number = 4;
117     jinli->RequestApplications(request2);
118 
119     Request* request3 = new Request;
120     request3->requestType = "加薪";
121     request3->requestContent = "小菜請求加薪";
122     request3->number = 500;
123     jinli->RequestApplications(request3);
124 
125     Request* request4 = new Request;
126     request4->requestType = "加薪";
127     request4->requestContent = "小菜請求加薪";
128     request4->number = 1000;
129     jinli->RequestApplications(request4);
130 
131     return 0;
132 }
posted @ 2011-04-30 11:07 unixfy 閱讀(203) | 評論 (0)編輯 收藏

來自于《大話設計模式》
命令模式(Command):將一個請求封裝為一個對象,從而使你可用不同的請求對客戶進行參數化;對請求排隊或記錄請求日志,以及支持可撤銷的操作。

行為型

UML 類圖:


代碼實現 C++:
  1 #include <iostream>
  2 #include <list>
  3 #include <algorithm>
  4 using namespace std;
  5 
  6 class Barbecurer
  7 {
  8 public:
  9     void BakeMutton()
 10     {
 11         cout << "烤羊肉串!" << endl;
 12     }
 13     void BakeChickenWing()
 14     {
 15         cout << "烤雞翅!"  << endl;
 16     }
 17 };
 18 
 19 class Command
 20 {
 21 protected:
 22     Barbecurer* receiver;
 23 public:
 24     Command(Barbecurer* b)
 25     {
 26         receiver = b;
 27     }
 28     virtual void ExcuteCommand() = 0;
 29 };
 30 
 31 class BakeMuttonCommand : public Command
 32 {
 33 public:
 34     BakeMuttonCommand(Barbecurer* b) : Command(b) {}
 35     virtual void ExcuteCommand()
 36     {
 37         receiver->BakeMutton();
 38     }
 39 };
 40 
 41 class BakeChickenWingCommand : public Command
 42 {
 43 public:
 44     BakeChickenWingCommand(Barbecurer* b) : Command(b) {}
 45     virtual void ExcuteCommand()
 46     {
 47         receiver->BakeChickenWing();
 48     }
 49 };
 50 
 51 class Waiter
 52 {
 53 private:
 54     list<Command*> orders;
 55 public:
 56     void SetOrder(Command* order)
 57     {
 58         cout << "增加訂單!" << endl;
 59         orders.push_back(order);
 60     }
 61     void CancelOrder(Command* order)
 62     {
 63         list<Command*>::iterator iter = find(orders.begin(), orders.end(), order);
 64         if (iter != orders.end())
 65         {
 66             cout << "取消訂單!" << endl;
 67             orders.erase(iter);
 68         }
 69     }
 70     void Notify()
 71     {
 72         for (list<Command*>::iterator iter = orders.begin(); iter != orders.end(); ++iter)
 73         {
 74             (*iter)->ExcuteCommand();
 75         }
 76     }
 77 };
 78 
 79 int main()
 80 {
 81     Barbecurer* boy = new Barbecurer;
 82 
 83     Command* bakeMuttonCommand1 = new BakeMuttonCommand(boy);
 84     Command* bakeMuttonCommand2 = new BakeMuttonCommand(boy);
 85     Command* bakeChickenWingCommand1 = new BakeChickenWingCommand(boy);
 86 
 87     Waiter* girl = new Waiter;
 88 
 89     girl->SetOrder(bakeMuttonCommand1);
 90     girl->SetOrder(bakeMuttonCommand2);
 91     girl->SetOrder(bakeChickenWingCommand1);
 92 
 93     girl->Notify();
 94 
 95 
 96     girl->CancelOrder(bakeMuttonCommand2);
 97     girl->Notify();
 98 
 99     return 0;
100 }
posted @ 2011-04-29 21:15 unixfy 閱讀(380) | 評論 (0)編輯 收藏

來自于《大話設計模式》
橋接模式(Bridge):將抽象部分與它的實現部分分離,使它們都可以獨立地變化。

結構型

UML 類圖:



代碼實現 C++:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class HandsetSoft
 5 {
 6 public:
 7     virtual void Run() = 0;
 8 };
 9 
10 class HandsetGame : public HandsetSoft
11 {
12 public:
13     virtual void Run()
14     {
15         cout << "運行手機游戲!" << endl;
16     }
17 };
18 
19 class HandsetAddressList : public HandsetSoft
20 {
21 public:
22     virtual void Run()
23     {
24         cout << "運行手機通訊錄!" << endl;
25     }
26 };
27 
28 class HandsetBrand
29 {
30 protected:
31     HandsetSoft* hs;
32 public:
33     HandsetBrand() : hs(0) {}
34     virtual ~HandsetBrand()
35     {
36         delete hs;
37     }
38     void SetHandsetSoft(HandsetSoft* soft)
39     {
40         delete hs;
41         hs = soft;
42     }
43     virtual void Run() = 0;
44 };
45 
46 class HandsetBrandN : public HandsetBrand
47 {
48 public:
49     virtual void Run()
50     {
51         hs->Run();
52     }
53 };
54 
55 class HandsetBrandM : public HandsetBrand
56 {
57 public:
58     virtual void Run()
59     {
60         hs->Run();
61     }
62 };
63 
64 int main()
65 {
66     HandsetBrand* ab = new HandsetBrandN;
67 
68     ab->SetHandsetSoft(new HandsetGame);
69     ab->Run();
70 
71     ab->SetHandsetSoft(new HandsetAddressList);
72     ab->Run();
73 
74     delete ab;
75 
76     ab = new HandsetBrandM;
77 
78     ab->SetHandsetSoft(new HandsetGame);
79     ab->Run();
80 
81     ab->SetHandsetSoft(new HandsetAddressList);
82     ab->Run();
83 
84     delete ab;
85 
86     return 0;
87 }
posted @ 2011-04-29 20:42 unixfy 閱讀(333) | 評論 (0)編輯 收藏
來自于《大話設計模式》
單例模式(Singleton):保證一個類僅有一個實例,并提供訪問它的全局訪問點。類的實例化有類本身管理。

UML 類圖:


代碼實現 C++:
 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Singleton
 5 {
 6 private:
 7     static Singleton* instance;
 8     Singleton() {}
 9 public:
10     static Singleton*  GetInstance()
11     {
12         if (instance == 0)
13         {
14             instance = new Singleton;
15         }
16         return instance;
17     }
18     ~Singleton()
19     {
20         delete instance;
21     }
22 };
23 
24 Singleton* Singleton::instance = 0;
25 
26 int main()
27 {
28     Singleton* s1 = Singleton::GetInstance();
29     Singleton* s2 = Singleton::GetInstance();
30 
31     cout << s1 << endl;
32     cout << s2 << endl;
33 
34     return 0;
35 }
posted @ 2011-04-29 17:09 unixfy 閱讀(236) | 評論 (0)編輯 收藏
僅列出標題
共19頁: First 10 11 12 13 14 15 16 17 18 Last 
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            欧美一区亚洲| 欧美日韩精品欧美日韩精品一| 亚洲午夜激情| 亚洲免费观看高清完整版在线观看熊| 国产伦精品一区二区三区视频黑人 | 亚洲二区在线观看| 久久精品视频网| 久久久最新网址| 久久在线视频| 欧美日韩一区二区在线观看视频| 欧美激情视频一区二区三区在线播放 | 午夜久久99| 亚洲欧美日韩国产综合精品二区| 午夜性色一区二区三区免费视频 | 欧美刺激午夜性久久久久久久| 欧美成年人视频| 国产精品自拍三区| 亚洲国产精品嫩草影院| 99精品福利视频| 久久久久久久尹人综合网亚洲| 玖玖玖国产精品| 日韩午夜电影av| 欧美一区二区精品在线| 欧美精品97| 久久综合久久综合九色| 一本一本久久a久久精品综合妖精 一本一本久久a久久精品综合麻豆 | 午夜伦欧美伦电影理论片| 久久久精品动漫| 午夜精品成人在线视频| 久久高清国产| 久久精彩免费视频| 亚洲精品在线观看免费| 玖玖国产精品视频| 国产一区二区成人| 久久色在线观看| 欧美在线观看一区二区三区| 国产精品一区二区久久国产| 亚洲免费一在线| 亚洲一级电影| 国内精品国产成人| 久久伊伊香蕉| 噜噜噜久久亚洲精品国产品小说| 好吊妞这里只有精品| 欧美大色视频| 欧美亚洲成人网| 毛片av中文字幕一区二区| 久久久久国色av免费观看性色| 亚洲激情二区| 亚洲在线一区二区三区| 在线欧美亚洲| 99精品视频免费全部在线| 国产精品一区二区三区成人| 麻豆免费精品视频| 国产精品爽黄69| 亚洲第一中文字幕在线观看| 欧美视频二区| 亚洲电影专区| 怡红院精品视频| 亚洲一区二区免费| a91a精品视频在线观看| 性色av香蕉一区二区| 亚洲欧洲在线观看| 久久久久国产精品www| 99re66热这里只有精品4| 亚洲欧美在线一区| 亚洲一级在线观看| 久久蜜桃av一区精品变态类天堂| 亚洲一区影院| 亚洲精品一区二区三区在线观看 | 亚洲理伦在线| 女女同性精品视频| 久久中文在线| 国产精品一区二区三区免费观看| 99re6这里只有精品视频在线观看| 一区在线影院| 久久久精品五月天| 久久五月天婷婷| 在线国产精品一区| 快she精品国产999| 亚洲第一精品影视| 亚洲最新中文字幕| 欧美私人网站| 欧美一区二区三区免费观看| 久久久精品一区| 在线视频国产日韩| 欧美激情亚洲国产| 一区二区三区你懂的| 久久久一二三| 一区二区三区 在线观看视| 欧美色大人视频| 欧美一激情一区二区三区| 久久婷婷av| 亚洲女人天堂av| 亚洲精品在线电影| 国产色综合天天综合网| 免费视频一区| 欧美在线观看视频在线| 亚洲精品一区中文| 欧美国产视频日韩| 久久久精品一区| 亚洲欧美激情四射在线日 | 可以免费看不卡的av网站| 欧美激情成人在线视频| 香蕉久久夜色精品国产使用方法| 亚洲二区在线视频| 国内一区二区三区在线视频| 欧美日韩免费观看一区=区三区 | 亚洲国产精品激情在线观看| 久久成人国产| 亚洲欧美综合网| 亚洲一区二区毛片| 夜夜嗨av一区二区三区网站四季av| 伊大人香蕉综合8在线视| 国产一区二区高清视频| 国产亚洲欧美日韩美女| 国产综合色在线视频区| 国产亚洲欧美一区二区| 在线观看的日韩av| aⅴ色国产欧美| 亚洲综合国产激情另类一区| 亚洲视频在线观看免费| 久久亚洲视频| 亚洲麻豆av| 91久久黄色| 欧美亚洲一区| 亚洲精品资源| 久久福利毛片| 国产精品jvid在线观看蜜臀| 国产一区免费视频| av成人免费在线观看| 久久国产精彩视频| 日韩亚洲成人av在线| 欧美永久精品| 国产日本欧美一区二区三区| 亚洲人成小说网站色在线| 久久视频免费观看| 欧美激情成人在线视频| 亚洲综合首页| 欧美成人久久| 国产亚洲一区二区在线观看 | 亚洲高清激情| 欧美在现视频| 亚洲综合精品四区| 欧美午夜激情在线| 亚洲免费电影在线| 欧美成人黄色小视频| 亚洲一区中文| 国产精品久久97| 亚洲欧美日本在线| 亚洲精品一区二区在线观看| 欧美大片一区| 夜夜夜久久久| 亚洲视频精选| 国内久久精品| 久久一区视频| 欧美高清在线| 亚洲免费在线观看| 亚洲午夜一区二区| 狠狠色丁香久久婷婷综合_中| 久久久久久久性| 女人天堂亚洲aⅴ在线观看| 亚洲国产一成人久久精品| 亚洲欧洲日夜超级视频| 国产精品午夜久久| 麻豆av一区二区三区久久| 欧美国产日韩a欧美在线观看| 亚洲精品亚洲人成人网| 欧美亚洲自偷自偷| 亚洲片在线观看| 欧美呦呦网站| 亚洲视屏在线播放| 男男成人高潮片免费网站| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 亚洲在线网站| 久久久久久夜| 日韩午夜中文字幕| 欧美一区二区黄| 亚洲视频免费在线| 六月婷婷久久| 麻豆九一精品爱看视频在线观看免费| 欧美韩日一区二区| 欧美激情一区在线| 国产综合久久久久影院| 欧美日韩亚洲一区二区三区在线观看| 亚洲免费在线视频| 在线综合欧美| 欧美午夜美女看片| 亚洲精品在线免费| 一区二区91| 欧美网站在线| 午夜欧美精品| 久久免费黄色| 亚洲黄页一区| 欧美日韩在线不卡一区| 亚洲激情在线观看| 亚洲一区在线观看免费观看电影高清| 欧美成人综合| 亚洲在线视频观看| 免费观看成人鲁鲁鲁鲁鲁视频| 国产主播一区二区|