Mediator模式(C++實現)

來源:互聯網
上載者:User

標籤:

  主要參考《大話設計模式》和《設計模式:可複用物件導向軟體的基礎》兩本書。本文介紹中介者模式的實現。

           

       中介者模式:What it is:Define an object that encapsulates how a set of objects interact. Promotes loose coupling by keeping objects from referring to each other explicitly and it lets you vary their interactions independently.

     用一個中介對象來封裝一系列的對象互動。中介者使各對象不需要顯式地相互引用,從而使其耦合鬆散,而且可以獨立地改變它們之間的互動。中介者模式的例子很多,大到聯合國安理會,小到房屋中介,都扮演了中間者的角色,協調各方利益。

     中介者模式很容易在系統中應用,也很容易在系統中誤用。當系統出現了多對多互動複雜的對象群時,不要急於使用中介者模式,而要先反思你的系統在設計上是不是合理。   

     優點:Mediator的出現減少了各個Colleague的耦合,使得可以獨立地改變和複用各個Colleague類和Mediator;  由於把對象如何協作進行了抽象,將中介作為一個獨立的概念並將其封裝在一個對象中,這樣關注的對象就從對象各自本身的行為轉移到它們之間的互動上來,也就是站在一個更宏觀的角度去看待系統。

     缺點:由於ConcreteMediator控制了集中化。於是就把互動的複雜性變為了中介者的複雜性,使得中介者比任何一個ConcreteColleague都複雜。

 

     本文就以租房為例子,如果沒有房屋中介,那麼房客要自己找房東,而房東也要自己找房客,非常不方便。有了房屋中介機構就方便了,房東可以把要出租的房屋資訊放到中介機構,而房客可以去中介機構諮詢。在軟體中,就是多個對象之間需要通訊,如果沒有中介,對象就需要知道其他對象,最壞情況下,可能需要知道所有其他對象,而有了中介對象就方便多了,對象只需與中介對象通訊,而不用知道其他的對象。這就是中介者模式,下面以租房為例,給出中介者模式的UML圖。

 

C++代碼實現Mediator模式:

 1 #include <iostream>   2 #include <string> 3 using namespace std; 4  5 /*聲明抽象中介類*/ 6 class Mediator; 7  8 /*定義抽象人類*/ 9 class Person10 {11 public:12     virtual void SetMediator(Mediator *mediator){} 13     virtual void SendMessage(string message){}14     virtual void GetMessage(string message){}15 protected:16     Mediator *_mediator; //中介    17 };18 19 /*定義抽象中介類*/20 class Mediator21 {22 public:23     virtual void Send(string message, Person *person){}24     virtual void SetRenter(Person *renter){}25     virtual void SetLandlord(Person *landlord){}26     27 };28 29 /*定義租客類*/30 class Renter:public Person31 {32 public:33     void SetMediator(Mediator *mediator){_mediator = mediator;}34     void SendMessage(string message){ _mediator->Send(message,this);}35     void GetMessage(string message){cout<<"租房者收到房東發來的訊息:"<<message;}36 };37 38 /*定義房東類*/39 class Landlord:public Person40 {41 public:42     void SetMediator(Mediator *mediator){_mediator = mediator;}43     void SendMessage(string message){ _mediator->Send(message,this);}44     void GetMessage(string message){cout<<"房東收到租客發來的訊息:"<<message;}45 };46 47 /*定義具體中介類*/48 class HouseMediator:public Mediator49 {50 public:51     HouseMediator():_renter(NULL),_landlord(NULL){}52     void SetRenter(Person *renter){_renter = renter;}53     void SetLandlord(Person *landlord){_landlord = landlord;}54     void Send(string message, Person *person)55     {//接收訊息的對象為該對象的對應對象56         if(person == _renter)57             _landlord->GetMessage(message);58         else59             _renter->GetMessage(message);60     }61 private:62     Person *_renter;63     Person *_landlord;64 };65 int main(int argc, char *argv[])  66 {  67     Mediator *mediator = new HouseMediator();68     Person *person1 = new Renter();69     Person *person2 = new Landlord();70     mediator->SetRenter(person1);71     mediator->SetLandlord(person2);72     person1->SetMediator(mediator);73     person2->SetMediator(mediator);74     person1->SendMessage("我想在深圳北站附近租套房子,一室一廳\n");75     person2->SendMessage("我出租一條房子,一室一廳,深圳北站附近\n");76     delete person1;77     delete person2;78     delete mediator;79     return 0;  80 }

 

Mediator模式(C++實現)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.