《大话设计模式》 之 第25章 世界需要和平——中介者模式 小结

发布时间:2017-2-26 18:05:47 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"《大话设计模式》 之 第25章 世界需要和平——中介者模式 小结",主要涉及到《大话设计模式》 之 第25章 世界需要和平——中介者模式 小结方面的内容,对于《大话设计模式》 之 第25章 世界需要和平——中介者模式 小结感兴趣的同学可以参考一下。

/************************************************************* * 中介者链模式 ************************************************************* * ——使用一个中介对象来封装对象间的交互。 * 中介者使得各个对象间不需要显式地相互引用,从而使其耦合松散,而且可以 * 独立地改变他们之间的交互。 *************************************************************///中介者基类 class Mediator{ public: Mediator(void){}; virtual ~Mediator(void){}; //封装对象间的交互行为,需要子类实现 virtual void SendMessage(Colleague* colleague, std::string message) = 0; }; //同事基类 class Colleague{ public: Colleague(void){}; Colleague(Mediator* mediator){m_mediator = mediator;} virtual ~Colleague(void){}; protected: //中介者对象,对象间交互的前提是愿意接受一个中介者的“调度” Mediator* m_mediator; }; //具体中介者 class ConcreteMediator : public Mediator{ public: ConcreteMediator(void){}; ~ConcreteMediator(void){}; void setOne(ConcreteColleagueOne* concreteColleagueOne){m_concreteColleagueOne = concreteColleagueOne;} void setTwo(ConcreteColleagueTwo* concreteColleagueTwo){m_concreteColleagueTwo = concreteColleagueTwo;} //封装的交互行为 void SendMessage(Colleague* colleague, std::string message); private: //中介者需要知道“被调度的”对象们 ConcreteColleagueOne* m_concreteColleagueOne; ConcreteColleagueTwo* m_concreteColleagueTwo; }; void ConcreteMediator::SendMessage(Colleague *colleague, std::string message){ if (colleague == m_concreteColleagueOne) { m_concreteColleagueTwo->ReceiveMessage(message); } else if (colleague == m_concreteColleagueTwo){ m_concreteColleagueOne->ReceiveMessage(message); } } //具体同事类1 class ConcreteColleagueOne : public Colleague{ public: ConcreteColleagueOne(void){}; ConcreteColleagueOne(Mediator* mediator):Colleague(mediator){}; ~ConcreteColleagueOne(void){}; void SendMessage(std::string message); void ReceiveMessage(std::string message); }; void ConcreteColleagueOne::SendMessage(std::string message){ std::cout << "ConcreteColleagueOne : send message-" << message << "\n"; m_mediator->SendMessage(this, message); } void ConcreteColleagueOne::ReceiveMessage(std::string message){ std::cout << "ConcreteColleagueOne : receive message-" << message << "\n"; } //具体同事类2 class ConcreteColleagueTwo : public Colleague{ public: ConcreteColleagueTwo(void){}; ConcreteColleagueTwo(Mediator* mediator):Colleague(mediator){}; ~ConcreteColleagueTwo(void){}; void SendMessage(std::string message); void ReceiveMessage(std::string message); }; void ConcreteColleagueTwo::SendMessage(std::string message){ std::cout << "ConcreteColleagueTwo : send message-" << message << "\n"; m_mediator->SendMessage(this, message); } void ConcreteColleagueTwo::ReceiveMessage(std::string message){ std::cout << "ConcreteColleagueTwo : receive message-" << message << "\n"; } 【main】 int main(int argc, const char * argv[]) { ConcreteMediator* concreteMediator = new ConcreteMediator(); ConcreteColleagueOne* concreteColleagueOne = new ConcreteColleagueOne(concreteMediator); ConcreteColleagueTwo* concreteColleagueTwo = new ConcreteColleagueTwo(concreteMediator); concreteMediator->setOne(concreteColleagueOne); concreteMediator->setTwo(concreteColleagueTwo); concreteColleagueOne->SendMessage("I am One! I am One! One! One! One!"); std::cout << "====华丽的分割线====\n"; concreteColleagueTwo->SendMessage("Oh! Damn it! I am Two!"); delete concreteMediator; concreteMediator = NULL; delete concreteColleagueOne; concreteColleagueOne = NULL; delete concreteColleagueTwo; concreteColleagueTwo = NULL; return 0; } 【结果】 ConcreteColleagueOne : send message-I am One! I am One! One! One! One! ConcreteColleagueTwo : receive message-I am One! I am One! One! One! One! ====华丽的分割线==== ConcreteColleagueTwo : send message-Oh! Damn it! I am Two! ConcreteColleagueOne : receive message-Oh! Damn it! I am Two!

上一篇:分割视图的一个问题
下一篇:String和Date、Timestamp之间的转换

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。