Twenty-five chapters - intermediary model

Intermediary model (Mediator): with an object that encapsulates intermediary set of objects interact. Mediator displayed keeping objects refer to each other, so that it loosely coupled, and can be independently varied interaction between them.

image

Basic Code

#include <iostream>
#include<vector>
#include<string>
#include<ctime>

using namespace std;

class Colleague;

class Mediator
{
public:
    virtual void Send(string message, Colleague* colleagur) = 0;
};

class Colleague
{
protected:
    Mediator* mediator;
public:
    Colleague(Mediator* mediator_t)
    {
        mediator = mediator_t;
    }
};

class ConcreteColleague1 :public Colleague
{
public:
    ConcreteColleague1(Mediator* mediator)
        :Colleague(mediator)
    { }

    void Send(string message)
    {
        mediator->Send(message, this);
    }

    void Notify(string message)
    {
        cout << "同事1得到消息:" << message << endl;
    }
};

class ConcreteColleague2 :public Colleague
{
public:
    ConcreteColleague2(Mediator* mediator)
        :Colleague(mediator)
    { }

    void Send(string message)
    {
        mediator->Send(message, this);
    }

    void Notify(string message)
    {
        cout << "同事2得到消息:" << message << endl;
    }
};

class ConcreteMediator :public Mediator
{
private:
    ConcreteColleague1* colleague1;
    ConcreteColleague2* colleague2;

public:
    void setConcreteColleague1(ConcreteColleague1* colleague1_t) { colleague1 = colleague1_t; }
    void setConcreteColleague2(ConcreteColleague2* colleague2_t) { colleague2 = colleague2_t; }
    void Send(string message, Colleague* colleague)
    {
        if (colleague == colleague1)
            colleague2->Notify(message);
        else
            colleague1->Notify(message);
    }
};



int main()
{
    ConcreteMediator* m = new ConcreteMediator();

    ConcreteColleague1* c1 = new ConcreteColleague1(m);
    ConcreteColleague2* c2 = new ConcreteColleague2(m);

    m->setConcreteColleague1(c1);
    m->setConcreteColleague2(c2);

    c1->Send("吃过饭了么?");
    c2->Send("没有,你打算请客么?");


    system("pause");
    return 0;
}

Advantages and disadvantages of mediation pattern

Intermediary model is easy to use in the system, it is very easy to misuse the system. When the system of interactions in complex target group 'many to many', do not rush to use the intermediary model, and reflect on your first system in the design is not reasonable.
Advantage mediator Mediator appeared first mode reduces coupling Colleague of each, and makes it possible to independently vary each reuse Colleague and Mediator classes. Secondly, because of how the objects collaborate abstracts mediation as an independent concept and encapsulated in an object, the object of such attention shifts from their own behavior object to the interaction between them up, which is the station in a broader perspective on the system.
However, since the centralized control ConcreteMediator, then put the complexity of the interaction becomes the complexity of the Mediator, which makes any Mediator becomes larger than a ConcreteColleague are complicated.

Guess you like

Origin www.cnblogs.com/wfcg165/p/12053825.html