The design pattern of C + + is a simple knowledge of mediator mode

Source: Internet
Author: User
Mediator Mode (mediator): Uses a Mediation object to encapsulate a series of object interactions. The mediator makes the objects do not need to explicitly interact with the reference, which makes the coupling loose, and can independently change the interaction between them.

Four characters:

Abstract Mediator Mediator

Specific Intermediary object Concretemediator

Abstract class Abstraction

Concrete abstract class Concreteabstract

Broker Mode Advantages:

The advent of mediator reduces the coupling of each abstract, allowing individual abstract classes and mediator to be independently changed and reused.

Because of the abstraction of how objects work together, mediations are used as an independent concept and encapsulated in an object so that the objects of interest move from their own behavior to the interaction between them, which is a more objective view of the system.

Broker Mode Disadvantage:

Because Concretemediator controls centralization, it turns the interaction complexity into a mediator's complexity, which makes it more responsible for intermediaries than any one concreteabstract.

Pattern implementation:

[code]//Forward Declaration class abstract;//Mediator abstract class class mediator{public:virtual void Send (std::string message, abstract *abstract) {} };//abstract class class Abstract{protected:mediator *mediator;public://Construct Mediator abstract (mediator *mediator) {This-&gt    ; mediator = Mediator; }};//concrete abstract class 1class Concreteabstract1:public Abstract{public:concreteabstract1 (mediator *me): abstract (Me) {} void Send (std::string message)    {//Intermediaries send out mediator->send (message, this); } void Notify (Std::string message) {std::cout << "ConcreteAbstract1 Receive message:" << message &L    t;< "---(from ConcreteAbstract2)" << Std::endl; }};//concrete abstract class 2class Concreteabstract2:public abstract{public:concreteabstract2 (mediator *me): abstract (Me) {} void Send (std::string message)    {//Intermediaries send out mediator->send (message, this); } void Notify (Std::string message) {std::cout << ' ConcreteAbstract2 Receive message: ' << message & lt;< "---(from ConcreteAbstract1) "<< Std::endl;    }};//specific Mediator class Concretemediator:public Mediator{private://need to know the specific abstract class objects stored ConcreteAbstract1 *CA1; ConcreteAbstract2 *ca2;public:concretemediator () {} void Setconcretemediator (ConcreteAbstract1 *ca1, Concreteabstrac        T2 *ca2) {this->ca1 = CA1;    THIS-&GT;CA2 = CA2; } virtual void Send (std::string message, abstract *abstract) {if (abstract = = CA1) ca2->notify (mess        Age);    else ca1->notify (message); }};

Client:

[Code]//clientint Main () {    Concretemediator *m = new Concretemediator;    The concrete abstraction of this understanding of the specific mediator    ConcreteAbstract1 *CA1 = new ConcreteAbstract1 (m);    ConcreteAbstract2 *CA2 = new ConcreteAbstract2 (m);    Let the intermediary know two concrete abstract class    M->setconcretemediator (CA1, Ca2);    Ca1->send ("Hello");  Output:concreteabstract2 Receive Message:hello   ---(from ConcreteAbstract1)    ca2->send ("Hi");    Output:concreteabstract1 Receive Message:hi   ---(from ConcreteAbstract2)     if (m! = NULL) {        delete m;        m = NULL;    }    if (CA1! = NULL) {        delete ca1;        CA1 = NULL;    }    if (CA2! = NULL) {        delete Ca2;        CA2 = NULL;    }    return 0;}

The above is the C + + design mode of shallow knowledge of the intermediary model content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.