[design mode]<8>. C + + and Broker mode (mediator pattern)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.