mediator 中介者模式

#include <iostream>
#include <string>

using namespace std;

class country;
class united_nation //中介类
{
public:
    virtual ~united_nation(){}
    virtual void declare(const string &message, country *colleague) =0;
};

class country
{
public:
    country(united_nation *nation):_nation(nation){}
protected:
    united_nation *_nation;
};

class USA : public country
{
public:
    USA(united_nation *nation):country(nation){}
    void declare(const string &message)
    {
        _nation->declare(message, this);
    }
    void get_message(const string &message)
    {
        cout << "USA get message : "<<message <<endl;
    }
};

class Iraq : public country
{
public:
    Iraq(united_nation *nation):country(nation){}
    void declare(const string &message)
    {
        _nation->declare(message, this);
    }
    void get_message(const string &message)
    {
        cout <<"Iraq get message : "<<message<<endl;
    }
};

class united_nation_security_council : public united_nation
{
public:
    united_nation_security_council():_usa(NULL),_iraq(NULL){}
    void setUSA(USA *usa)
    {
        _usa = usa;
    }
    void setIraq(Iraq *iraq)
    {
        _iraq = iraq;
    }
    void declare(const string &message, country *colleague)
    {
        if (_usa == colleague)
        {
            _iraq->get_message(message);
        }
        else if (_iraq == colleague)
        {
            _usa->get_message(message);
        }
        else
        {
        }
    }
private:
    USA *_usa;
    Iraq *_iraq;
};

int main()
{
    united_nation_security_council *UNSC = new united_nation_security_council;
    USA *usa = new USA(UNSC);
    Iraq *iraq = new Iraq(UNSC);
    UNSC->setUSA(usa);
    UNSC->setIraq(iraq);
    usa->declare("We are Americas!");
    iraq->declare("We are Iraqs!");
    delete UNSC;delete usa;delete iraq;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值