mediator 中介者模式
2013-12-21 11:52
190 查看
#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; }
相关文章推荐
- 中介者模式(Mediator)
- C++设计模式之十七:Mediator(中介者模式)
- 极速理解设计模式系列:15.中介者模式(Mediator Pattern)
- 中介者模式(Mediator)
- 行为型模式之中介者模式、调停者模式(Mediator)
- Mediator 中介者模式
- Java 实现中介者(Mediator)模式
- 设计模式——中介者模式(Mediator)
- 浅谈JAVA设计模式之——中介者模式(Mediator)
- 中介者模式(Mediator Pattern)
- 设计模式21 - 中介者模式【【Mediator Pattern】
- 设计模式(17)——中介者 Mediator
- 《GOF设计模式—中介者 (MEDIATOR)—Delphi源码示例:字体对话框
- GOF23设计模式之 中介者模式Mediator
- 中介者模式(Mediator Pattern)
- 中介者模式(Mediator pattern)
- 中介者模式-Mediator
- 设计模式之中介者模式(Mediator)摘录
- 23种设计模式-10.中介者模式(Mediator Pattern)
- HeadFirst 设计模式学习笔记18--中介者(Mediator)模式拾零