Broker mode
Encapsulates a series of object interactions with a mediation object. The mediator makes the objects not need to explicitly reference each other, so that they are loosely coupled, and can independently change the interaction between them. There are many examples of intermediaries, from the United Nations Security Council to housing intermediaries, who have played a central role in coordinating the interests of all parties.
Example
This article to rent for example, if there is no housing intermediary, then tenants to find their own landlord, and the landlord also to find tenants, very inconvenient. With the housing agency is convenient, the landlord can be rented housing information to the intermediary agencies, and tenants can go to the intermediary agency consultation.
In software, there is a need for communication between multiple objects, if there is no intermediary, the object needs to know other objects, in the worst case, you may need to know all the other objects, and with the mediation object is more convenient, the object only need to communicate with the mediation object, not to know other objects. This is the mediator pattern.
#include <iostream>using namespacestd;classMediator; //Abstract ManclassPerson {protected: Mediator*m_mediator;//Intermediary Public: Virtual voidSetmediator (Mediator *mediator) {}//Set up mediation Virtual voidSendMessage (stringMessage) {}//send a message to an intermediary Virtual voidGetMessage (stringMessage) {}//get information from an intermediary}; //Abstract Intermediary AgenciesclassMediator { Public: Virtual voidSend (stringMessage, Person *Person ) {} Virtual voidSetA (person *a) {}//Set one side Virtual voidSETB (Person *B) {}}; //Renters 'classRenter: PublicPerson { Public: voidSetmediator (Mediator *mediator) {m_mediator =Mediator;} voidSendMessage (stringMessage) {m_mediator->send (message), This); } voidGetMessage (stringMessage) {cout<<"Tenants receive information"<<message;} }; //landlordclassLandlord: PublicPerson { Public: voidSetmediator (Mediator *mediator) {m_mediator =Mediator;} voidSendMessage (stringMessage) {m_mediator->send (message), This); } voidGetMessage (stringMessage) {cout<<"The landlord receives the message:"<<message;} }; //Housing AgencyclassHousemediator: PublicMediator {Private: person*m_a;//Renters 'Person *m_b;//landlord Public: Housemediator (): M_a (0), M_b (0) {} voidSetA (person *a) {m_a =A;} voidSETB (person *b) {M_b =B;} voidSend (stringMessage, Person *Person ) { if(person = = m_a)//renters send messages to landlordsM_b->getmessage (message);//The landlord received the message ElseM_a-GetMessage (message); } }; //Test CaseintMain () {mediator*mediator =NewHousemediator (); person*person1 =NewRenter ();//Renters 'Person *person2 =NewLandlord ();//landlordMediator->SetA (Person1); Mediator-Setb (Person2); Person1-Setmediator (mediator); Person2-Setmediator (mediator); Person1->sendmessage ("I want to rent a house near Nanjing Road, the price is 800 yuan one months \ n"); Person2->sendmessage ("Rental House: Nanjing Road No. 100th, 70 sqm, 1000 yuan one months \ n"); DeletePerson1;DeletePerson2;DeleteMediator; return 0; }
Reference:http://blog.csdn.net/wuzhekai1985
Design mode--(14) Broker mode