美文网首页
c++中介者模式

c++中介者模式

作者: 一路向后 | 来源:发表于2021-02-23 21:23 被阅读0次

    1.中介者模式简介

       中介者模式(Mediator Pattern)是用来降低多个对象和类之间的通信复杂性。这种模式提供了一个中介类,该类通常处理不同类之间的通信,并支持松耦合,使代码易于维护。

    2.源码实现

    #include <iostream>
    #include <string>
    
    using namespace std;
    
    #define SAFE_DELETE(p) if(p) {delete p; p = NULL}
    
    class Mediator;
    
    class Colleague
    {
    public:
        Colleague(Mediator *pMediator) : m_pMediator(pMediator){};
        virtual void Send(char *message) = 0;
    
    protected:
        Mediator *m_pMediator;
    };
    
    class ConcreteColleague1 : public Colleague
    {
    public:
        ConcreteColleague1(Mediator *pMediator) : Colleague(pMediator){};
    
        virtual void Send(char *message);
    
        void Notify(char *message)
        {
            cout << message << endl;
        }
    };
    
    class ConcreteColleague2 : public Colleague
    {
    public:
        ConcreteColleague2(Mediator *pMediator) : Colleague(pMediator){};
    
        virtual void Send(char *message);
    
        void Notify(char *message)
        {
            cout << "ConcreteColleague2 is handling the message." << endl;
            cout << message << endl;
        }
    };
    
    class Mediator
    {
    public:
        virtual void Sent(char *message, Colleague *pColleague) = 0;
    };
    
    void ConcreteColleague1::Send(char *message)
    {
        m_pMediator->Sent(message, this);
    }
    
    void ConcreteColleague2::Send(char *message)
    {
        m_pMediator->Sent(message, this);
    }
    
    class ConcreteMediator : public Mediator
    {
    public:
        void Sent(char *message, Colleague *pColleague)
        {
            ConcreteColleague1 *pConcreteColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);
    
            if(pConcreteColleague1)
            {
                cout << "The message is from ConcreteColleague1" << endl;
    
                if(m_pColleague2)
                {
                    m_pColleague2->Notify(message);
                }
            }
            else
            {
                if(m_pColleague1)
                {
                    m_pColleague1->Notify(message);
                }
            }
        }
    
        void setColleague1(Colleague *pColleague)
        {
            m_pColleague1 = dynamic_cast<ConcreteColleague1 *>(pColleague);
        }
    
        void setColleague2(Colleague *pColleague)
        {
            m_pColleague2 = dynamic_cast<ConcreteColleague2 *>(pColleague);
        }
    
    private:
         ConcreteColleague1 *m_pColleague1;
         ConcreteColleague2 *m_pColleague2;
    };
    
    int main(int argc, char **argv)
    {
        Mediator *pMediator = new ConcreteMediator();
    
        Colleague *pColleague1 = new ConcreteColleague1(pMediator);
        Colleague *pColleague2 = new ConcreteColleague2(pMediator);
    
        ConcreteMediator *pConcreteMediator = dynamic_cast<ConcreteMediator *>(pMediator);
        pConcreteMediator->setColleague1(pColleague1);
        pConcreteMediator->setColleague2(pColleague2);
    
        char message[128] = "Where are you from?";
    
        pColleague1->Send(message);
    
        delete pColleague1;
        delete pColleague2;
        delete pMediator;
    
        return 0;
    }
    

    3.编译源码

    $ g++ -o example example.cpp
    

    4.运行及其结果

    $ ./example
    The message is from ConcreteColleague1
    ConcreteColleague2 is handling the message.
    Where are you from?
    

    相关文章

      网友评论

          本文标题:c++中介者模式

          本文链接:https://www.haomeiwen.com/subject/zomdfltx.html