我所理解的設計模式(C++實現)——中介者模式(Mediator Pattern)

來源:互聯網
上載者:User
概述:

    假設我們開發一個圖片處理軟體,裡面肯定包括很多相關功能,比如說剪下,旋轉,濾鏡,美化等等,而我們這些功能所要處理的對象是固定的,就是我們所顯示的那張圖片。但是我們不能把所有的功能羅列到一個tab上,雖然這樣處理方便但是不美觀。這是我們可以這樣子:用一個中介者類負責所有功能的初始化和具體執行,我們需要功能時直接調用中介者類即可。

    中介者模式就是定義一個中介對象來封裝系列對象之間的互動。中介者使各個對象不需要顯示地相互引用,從而使其耦合性鬆散,而且可以獨立地改變他們之間的互動。

類圖和執行個體

Mediator:抽象中介者,定義了同事對象互動的介面。

ConcreteMediator:具體中介者對象,實現抽象類別中的方法,此具體中介者對象需要知道所有具體同事類,並從具體同事接受訊息,向具體同事對象發送命令。

Colleague類:抽象同事類。

ConcreteColleague類:具體同事類,實現抽象同事類中的方法。每一個同時類需要知道中介者對象;每個具體同事類只需要瞭解自己的行為,而不需要瞭解其他同事類的情況。

#include <iostream>#include <vector>#include <string>using namespace std;class Colleage{private:string name;string content;public:Colleage(string n = " "):name(n){};void set_name(string name){this->name = name;}string get_name(){return this->name;}void set_content(string content){this->content = content;}string get_content(){if(content.size() != 0)return content;else return "Copy that";}virtual void talk(){};};class Monitor : public Colleage{public:Monitor(string n = ""):Colleage(n){};virtual void talk(){cout<<"班長 "<<get_name()<<" 說:"<<get_content()<<endl;}};class Secretary : public Colleage{public:Secretary(string n = ""):Colleage(n){};virtual void talk(){cout<<"團支書 "<<get_name()<<" 說:"<<get_content()<<endl;}};class StudentA : public Colleage{public:StudentA(string n = ""):Colleage(n){};virtual void talk(){cout<<"學生 A "<<get_name()<<" 說:"<<get_content()<<endl;}};class StudentB : public Colleage{public:StudentB(string n = ""):Colleage(n){};virtual void talk(){cout<<"學生 B "<<get_name()<<" 說:"<<get_content()<<endl;}};class Mediator{public:vector<Colleage*> studentList;virtual void add_student(Colleage *student){studentList.push_back(student);};virtual void notify(Colleage *student){};    };class QQMediator : public Mediator{public:virtual void notify(Colleage *student){student->talk();for(int i = 0 ; i < studentList.size() ; ++i){            if(student != studentList[i]){studentList[i]->talk();}}};};int main(){QQMediator qq;Monitor *studentMonitor = new Monitor("Foxx");Secretary *studentSecretary = new Secretary("TC");StudentA *studentA = new StudentA("Jack");StudentB *studentB = new StudentB("Frank");        qq.add_student(studentSecretary);qq.add_student(studentA);qq.add_student(studentB);     studentMonitor->set_content("明天開始放假!");qq.notify(studentMonitor);   return 0;}

適用性:

1.一組對象以定義良好但是複雜的方式進行通訊。產生的相互依賴關係結構混亂且難以理解。

2.一個對象引用其他很多個物件並且直接與這些對象通訊,導致難以複用該對象。

3.想定製一個分布在多個類中的行為,而又不想產生太多的子類。

優缺點:

使用中介者模式的優點:

  1.降低了系統對象之間的耦合性,使得對象易於獨立的被複用。

  2.提高系統的靈活性,使得系統易於擴充和維護。

使用中介者模式的缺點:

  由於我們這個“中介“承擔了較多的責任,所以一旦這個中介對象出現了問題,那麼整個系統就會受到重大的影響。


LCL_data原創於CSDN.NET【http://blog.csdn.net/lcl_data/article/details/9529427】

其他設計模式請參看:我所理解的設計模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.