设计模式学习笔记(C++实现)(十八)--中介者模式

1.中介者模式简介

  • 英文名称
    Mediator
  • 主要目的
    用一个中介对象来封装一系列的交互。中介者使得各对象不需要显示地相互引用,从而使其耦合松散,而且可以改变它们之间的交互方式。
  • 使用场景
    1.一组对象定义良好,但是与其进行通信的方式很复杂;
    2.一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象;
    3.想定制一个分布在多个类中的行为,而又不想生成太多的子类。

2.中介者模式代码示例

  • 测试平台
    1.开发语言:C++
    2.开发工具:VS2015
    3.操作系统:Win7 X64
  • 代码说明
    1.Mediator–抽象中介类,用来定义基本接口;
    2.Participant–抽象参与者类,声明基本接口;
    3.HouseAgent–房屋中介类,具体中介者;
    3.Landlord、Tenant–具体参与者类,分别用来表示房东和租房者。

    注意:
    1.本例用中介者模式来模拟租房谈判过程;
    2.通过中介者模式,参与者一方发布消息,另一方就可以立即收到。

  • 具体代码

#include <iostream>
using namespace std;

//本例模拟租房谈判过程
//因为定义中介类时,需要用过参与者(房东、房客)的定义信息,所以提前声明参与者类
class Participant;

//抽象中介类,用来定义基本接口
class Mediator
{
public:
    //函数功能:声明抽象消息发布接口
    //参数:    string strMsg[IN]                           -- 消息内容
    //          Participant* pParticipant[IN]               -- 消息发布者
    //返回值:  无
    virtual void SendMessage(const string strMsg, Participant* pParticipant) = 0;

    //函数功能:设置参与者1
    //参数:    Participant* pParticipant[IN]               -- 参与者指针
    //返回值:  无
    void SetParticipant1(Participant* pParticipant)
    {
        this->m_pParticipant1 = pParticipant;
    }

    //函数功能:设置参与者2
    //参数:    Participant* pParticipant[IN]               -- 参与者指针
    //返回值:  无
    void SetParticipant2(Participant* pParticipant)
    {
        m_pParticipant2 = pParticipant;
    }
protected:
    Participant* m_pParticipant1;                                      //指向参与者1
    Participant* m_pParticipant2;                                      //执行参与者2
};

//抽象参与者类,声明基本接口
class Participant
{
public:
    //函数功能:参与者构造函数
    //参数:    Mediator* pMediator[IN]                     -- 中介者指针
    //返回值:  无
    Participant(Mediator* pMediator)
    {
        if (pMediator != NULL)
        {
            this->m_pMediator = pMediator;
        }
    }

    //函数功能:发布消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    virtual void SendMessage(const string strMsg) = 0;

    //函数功能:获得消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    virtual void GetMessage(const string strMsg) = 0;
protected:
    Mediator* m_pMediator;                                //与该参与者相关联的中介者指针
};

//房屋中介类,具体中介者
class HouseAgent :public Mediator
{
public:
    //函数功能:发布消息
    //参数:    string strMsg[IN]                           -- 消息内容
    //          Participant* pParticipant[IN]               -- 消息发布者
    //返回值:  无
    void SendMessage(const string strMsg, Participant* pParticipant)
    {
        if (pParticipant == m_pParticipant1)
        {
            m_pParticipant2->GetMessage(strMsg);
        }
        else
        {
            m_pParticipant1->GetMessage(strMsg);
        }
    }
};

//房东类,具体参与者类
class Landlord :public Participant
{
public:
    //函数功能:构造函数
    //参数:    Mediator* pMediator[IN]                     -- 中介者指针
    //返回值:  无
    Landlord(Mediator* pMediator):
        Participant(pMediator)
    {}

    //函数功能:发布消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    void SendMessage(const string strMsg)
    {
        if (this->m_pMediator != NULL)
        {
            this->m_pMediator->SendMessage(strMsg, this);
        }
    }

    //函数功能:获得消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    void GetMessage(const string strMsg)
    {
        cout << "租房者说:" << strMsg.c_str() << endl;
    }
};

//租房者类,具体参与者类
class Tenant :public Participant
{
public:
    //函数功能:构造函数
    //参数:    Mediator* pMediator[IN]                     -- 中介者指针
    //返回值:  无
    Tenant(Mediator* pMediator) :
        Participant(pMediator)
    {}

    //函数功能:发布消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    void SendMessage(const string strMsg)
    {
        if (this->m_pMediator != NULL)
        {
            this->m_pMediator->SendMessage(strMsg, this);
        }
    }

    //函数功能:获得消息
    //参数:    const string strMsg[IN]                     -- 消息内容
    //返回值:  无
    void GetMessage(const string strMsg)
    {
        cout << "房东说:" << strMsg.c_str() << endl;
    }
};

int main()
{
    //创建中介者
    Mediator* pAgent = new HouseAgent();

    //创建房东和租房者
    Participant* pLandlord = new Landlord(pAgent);
    Participant* pTenant = new Tenant(pAgent);

    //设置参与者
    pAgent->SetParticipant1(pLandlord);
    pAgent->SetParticipant2(pTenant);

    //开始谈判
    pLandlord->SendMessage("两室一厅,地铁站旁,精装修,每月4000元");
    pTenant->SendMessage("房子可以接受,价格太高,每月3000元才能接受");

    pLandlord->SendMessage("最低3500,,否则免谈");
    pTenant->SendMessage("好吧,那就3500吧,成交!");
    pLandlord->SendMessage("成交");

    delete pLandlord;
    pLandlord = NULL;

    delete pTenant;
    pTenant = NULL;

    delete pAgent;
    pAgent = NULL;

    getchar();
    return 0;
}
  • 输出结果
    这里写图片描述

栏目导航
上一篇:设计模式学习笔记(C++实现)(十七)–迭代器模式
下一篇:设计模式学习笔记(C++实现)(十九)–备忘录模式

参考文献:
1.《设计模式:可复用面向对象软件的基础》

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值