Intent: Encapsulates a series of object interactions with a mediator object. The mediator reduces the coupling by eliminating the need for explicit cross-referencing between objects.
The original: Silent Eeer
Address: http://www.cnblogs.com/hebaichuanyeah/p/6091506.html
The actual means is to use a mediator object to connect the operations of two objects. For example the requirement is that a object is updated/changed, and B objects are updated/changed accordingly, not by directly invoking the B object
An example, see note
#include <iostream>using namespacestd;classcolleague;//defines a mediator interface that contains the calling function required to change the object. classmediator{ Public : Virtual~Mediator () {}Virtual voidChanged (colleague *) =0;};//define the "colleague" class interface, initialize requires a mediator object, and through that class update another "colleague"classcolleague{ Public : //Initialize the Mediator class objectColleague (Mediator *mediator) { This->mediator =Mediator; } //update a different class Virtual voidchanged () {Mediator->changed ( This); }Private: Mediator*Mediator;};//specific colleague Class 1classConcreteColleague1: Publiccolleague{ Public: ConcreteColleague1 (Mediator*mediator): colleague (mediator) {}voidUpdate () {cout<<"update ConcreteColleague1 from ConcreteColleague2"<<Endl; }};//specific colleague Class 2classConcreteColleague2: Publiccolleague{ Public: ConcreteColleague2 (Mediator*mediator): colleague (mediator) {}voidUpdate () {cout<<"update ConcreteColleague2 from ConcreteColleague1"<<Endl; }};//the specific mediator class implements the update function changed. classConcretemediator: Publicmediator{Private: ConcreteColleague1*colleague1; ConcreteColleague2*colleague2; Public: voidSetColleague1 (ConcreteColleague1 *colleague) {colleague1=colleague; } voidSetColleague2 (ConcreteColleague2 *colleague) {colleague2=colleague; } concretemediator () {//colleague1 = new ConcreteColleague1 (this); } ~Concretemediator () {}Virtual voidChanged (colleague*colleague) { if(Colleague = =colleague1) {colleague2-update (); } Else if(Colleague = =colleague2) {colleague1-update (); } Else; }};main () {concretemediator concretemediator; ConcreteColleague1 colleague1 (&concretemediator); ConcreteColleague2 Colleague2 (&concretemediator); Concretemediator.setcolleague1 (&colleague1); Concretemediator.setcolleague2 (&colleague2); //"Colleague 1" updates "colleague 2" through intermediariescolleague1.changed (); //"Colleague 2" updates "colleague 1" through intermediariescolleague2.changed ();}
[design mode]<8>. C + + and Broker mode (mediator pattern)