设计模式之《中介者模式》

介绍

  • Mediator 抽象中介者角色
    抽象中介者角色定义统一的接口, 用于各同事角色之间的通信。
  • Concrete Mediator 具体中介者角色
    具体中介者角色通过协调各同事角色实现协作行为, 因此它必须依赖于各个同事角色。
  • Colleague 同事角色
    每一个同事角色都知道中介者角色, 而且与其他的同事角色通信的时候, 一定要通过中介者角色协作。

每个同事类的行为分为两种:

  • 自发行为(Self-Method):同事本身的行为, 比如改变对象本身的状态, 处理自己的行为等,与其他的同事类或中介者没有任何的依赖
  • 依赖方法(DepMethod):必须依赖中介者才能完成的行为

优点: 一对多的依赖变成了一对一,降低耦合

缺点: 同事类越多, 中介者的逻辑就越复杂

应用:机场调度中心、MVC框架、媒体网关、中介服务

UML:

简单代码:

#ifndef SIMPLE_MEDIATOR_H
#define SIMPLE_MEDIATOR_H

#include <iostream>
using namespace std;

class Colleague;
/**
 * @brief The Mediator class
 * 抽象中介者类
 */
class Mediator
{
public:
    virtual void send(string message , Colleague *col) = 0;
};



/**
 * @brief The Colleague class
 * 抽象同事类
 */
class Colleague
{
public:
    Colleague(Mediator *med)
    {
        m_mediator = med;//构造方法,得到中介者对象
    }
protected:
    Mediator *m_mediator;

};

/**
 * @brief The ConcreteColleague1 class
 * 具体同事1的对象
 */
class ConcreteColleague1 : public Colleague
{
public:
    ConcreteColleague1(Mediator *me)
        :Colleague(me)
    {

    }

    void send(string message)
    {
        m_mediator->send(message , this);
    }

    void notify(string message)
    {
        cout<<"同事 [1] 得到信息 :"<<message<<endl;
    }
};


/**
 * @brief The ConcreteColleague2 class
 * 具体同事2的对象
 */
class ConcreteColleague2 : public Colleague
{
public:
    ConcreteColleague2(Mediator *me)
        :Colleague(me)
    {

    }

    void send(string message)
    {
        m_mediator->send(message , this);
    }

    void notify(string message)
    {
        cout<<"同事 [2] 得到信息 :"<<message<<endl;
    }
};


/**
 * @brief The ConcreteMediator class
 * 具体中介者类
 */
class ConcreteMediator : public Mediator
{
public:
    ConcreteColleague1 *setCol1(ConcreteColleague1 *col1)
    {
        m_col1 = col1;
    }

    ConcreteColleague2 *setCol2(ConcreteColleague2 *col2)
    {
        m_col2 = col2;
    }

    void send(string message , Colleague *coll) override
    {
        if( coll == m_col1 )
        {
            m_col2->notify(message);
        }
        else {
            m_col1->notify(message);
        }
    }
private:
    ConcreteColleague1 *m_col1;
    ConcreteColleague2 *m_col2;
};

#endif // SIMPLE_MEDIATOR_H

国家与联合国之间的例子:

UML图:

代码:

#ifndef COUNTRY_MEDIATOR_H
#define COUNTRY_MEDIATOR_H

#include <iostream>

using namespace std;

class Country;

/**
 * @brief The UnitedNations class
 * 联合国机构
 */
class UnitedNations
{
public:
    virtual void declare(string message , Country *c) = 0;
};

/**
 * @brief The Country class
 * 国家
 */
class Country
{
public:
    Country(UnitedNations *me)
    {
        m_mediator = me;
    }
protected:
    UnitedNations *m_mediator;
};

class USA : public Country
{
public:
    USA(UnitedNations *med)
        :Country(med)
    {

    }

    void declare(string message)
    {
        m_mediator->declare(message , this );
    }

    void getMessage(string message)
    {
        cout<<"美国获得对方信息:"<<message<<endl;
    }
};

class Iraq : public Country
{
public:
    Iraq(UnitedNations *med)
        :Country(med)
    {

    }

    void declare(string message)
    {
        m_mediator->declare(message , this );
    }

    void getMessage(string message)
    {
        cout<<"伊拉克获得对方信息:"<<message<<endl;
    }
};

//联合国安理会
class UnitedNSC: public UnitedNations
{
public:
    void setUSA(USA *u)
    {
        m_usa = u;
    }

    void setIraq(Iraq *i)
    {
        m_iraq = i;
    }

    void declare(string message , Country *c) override
    {
        if( c == m_usa )
        {
            m_iraq->getMessage(message);
        }
        else
        {
          m_iraq->getMessage(message);
        }
    }
private:
    USA *m_usa;
    Iraq *m_iraq;
};


#endif // COUNTRY_MEDIATOR_H

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Liu-Eleven

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值