利用中介者模式模拟两个对象对话 C++
来源:互联网 发布:网络借贷不还会坐牢吗 编辑:程序博客网 时间:2024/06/10 15:56
1. 中介者模式
用一个中介对象来封装一系列的对象的交互, 中介者使得各个对象不需要显示调用, 从而降低他们之间的耦合, 并且可以独立的改变他们之间的交互。
一般而言, 通过将一个系统划分为多个对象, 通常可以增加他的复用性, 但是对象之间相互连接的激增, 又会降低他的复用性。
我们可以这么理解, 由于大量的连接使得一个对象需要依赖其他对象才能工作, 系统表现成为一个不可分割的整体, 这时候, 再想对系统的行为做较大的改动都显得比较困难了。
中介者模式, 实现的是集中化控制, 他把各个对象之间的交互工作全部转移到了中介者身上, 于是中介者就变得相对比较复杂了。
如果绘制拓扑结构, 一般是一个星形结构。
集中化控制既是这个模式的优点,也是这个模式的缺点。
ps: 中介者模式, 一般应用于一组对象定义良好, 但是需要复杂的方式进行通信的场合。
2. UML
3. run
4. code
Mediator.h
#ifndef _MEDIATOR_H_#define _MEDIATOR_H_#include <string>using std::string;class Country;class UnitedNations{public: virtual void Declared(string message, Country * colleague) = 0;};#endif // _MEDIATOR_H_
colleague.h
#ifndef _COLLEAGUE_H_#define _COLLEAGUE_H_class UnitedNations;class Country{public: Country(UnitedNations * mediator) : mediator(mediator){}protected: UnitedNations * mediator;};#endif // _COLLEAGUE_H_
mainmediator.h
#ifndef _MAINMEDIATOR_H_#define _MAINMEDIATOR_H_#include "Colleague.h"#include "Mediator.h"class UnitedNationsSecurityCouncil : public UnitedNations{public: void Declared(string message, Country * colleague){ if (colleague == colleague1) colleaque2->GetMessage(message); else colleague1->GetMessage(message); } void Colleague1(USA * val) { colleague1 = val; } void Colleaque2(Iraq * val) { colleaque2 = val; }private: USA * colleague1; Iraq * colleaque2;};#endif // _MAINMEDIATOR_H_
maincolleague.h
#ifndef _MAINCOLLEAGUE_H_#define _MAINCOLLEAGUE_H_#include "Colleague.h"#include "Mediator.h"#include <iostream>using std::cout;using std::endl;class USA : public Country{public: USA(UnitedNations * mediator) : Country(mediator){} void Declare(string message){ mediator->Declared(message, this); } void GetMessage(string message){ cout << "美国获得对方消息: " << message << endl; }};class Iraq : public Country{public: Iraq(UnitedNations * mediator) : Country(mediator){} void Declare(string message){ mediator->Declared(message, this); } void GetMessage(string message){ cout << "伊拉克获得对方消息: " << message << endl; }};#endif // _MAINCOLLEAGUE_H_
main.cpp
#include "MainColleague.h"#include "MainMediator.h"#include <iostream>#include <string>int main(){ auto unsc = new UnitedNationsSecurityCouncil(); auto c1 = new USA(unsc); auto c2 = new Iraq(unsc); unsc->Colleague1(c1); unsc->Colleaque2(c2); c1->Declare("放下武器, 赶紧投降!!"); c2->Declare("就不投降, 你咬我呀!!"); system("pause"); return 0;}
0 0
- 利用中介者模式模拟两个对象对话 C++
- 香格里拉对话——中介者模式有想法
- 【C++】Chapter21:中介者模式
- Objective C--中介者模式
- Objective C中介者模式
- Objective C--中介者模式
- 中介者模式-对象行为模式
- 中介者——对象行为模式
- 面向对象编程思想-中介者模式
- 设计模式 场景模拟 Mediator pattern 中介者模式
- Objective-C中的中介者模式
- 对象行为模式——中介者模式(Mediator)
- 设计模式【9】:中介者模式【对象去耦】
- 中介者模式——对象行为型模式
- java设计模式--中介者模式(对象行为型)
- 中介者模式(Mediator)(对象行为型模式)
- Mediator 中介者模式 --对象行为型模式
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
- Android SDK Manager中options设置解决下载太慢问题
- 第五周上机实践项目——项目4-长方柱类
- 关于false aliasing的一些看法
- ios学习 多线程NSThread 基本属性
- 安卓开发的时候需要注意的点和坑!
- 利用中介者模式模拟两个对象对话 C++
- shell脚本--部署应用到tomcat并启动tomcat
- GCD的使用
- SpringMVC与Struts2区别与比较总结
- 书到用时方嫌少,多读些书
- poj 2136 Vertical Histogram
- Team Queue的解题报告
- Day1--3.28
- using tetgen to generate tetrahedra