设计模式:mediator模式
目的:解决多组件之间的通信问题,使得组件之间的通信变得简单
核心:提供一个管理类,用来处理组件之间的通信,所有的组件只和管理类通信,组件彼此之间不在单独通信
例子:
class Mediator
{
public:
virtual void Send(string &message, Person *person) = 0;
virtual void SetRenter(Person *renter) = 0;
virtual void SetLandlord(Person *landlord) = 0;
}; class Person
{
public:
virtual void SetMediator(Mediator *mediator) = 0;
virtual void SendMessage(string message) = 0;
virtual void GetMessage(string message) = 0;
protected:
Mediator *p_mediator;
};
class Renter: public Person
{
public:
void SetMediator(Mediator *mediator)
{
p_mediator = mediator;
}
void SendMessage(string message)
{
p_mediator->Send(message, this);
}
void GetMessage(string message)
{
cout << "租房者收到房东发来的消息:"<< message << endl;
}
}; class Landlord: public Person
{
public:
void SetMediator(Mediator *mediator)
{
p_mediator = mediator;
}
void SendMessage(string message)
{
p_mediator->Send(message,this);
}
void GetMessage(string message)
{
cout << "房东收到租客发来的消息:" << message << endl;
}
};
class HouseMediator: public Mediator
{
private:
Person *p_renter;
Person *p_landlord; public:
HouseMediator(): p_renter(NULL), p_landlord(NULL)
{
}
void SetRenter(Person *renter)
{
p_renter = renter;
}
void SetLandlord(Person *landlord)
{
p_landlord = landlord;
}
void Send(string &message, Person *person) //消息的处理函数
{
if(person == p_renter)
p_landlord->GetMessage(message);
else
p_renter->GetMessage(message);
}
};
int main()
{
Renter* r = new Renter();
Landlord* l = new Landlord();
HouseMediator* hm = new HouseMediator();
r->SetMediator(hm);
l->SetMediator(hm);
hm->SetRenter(r);
hm->SetLandlord(l); r->SendMessage(string("东西坏了,房东快来修理!"));
l->SendMessage(string("到时间了,该叫房租了!")); return 0;
}
设计模式:mediator模式的更多相关文章
- 设计模式--中介(Mediator)模式
时隔很长一段时,现在又重温设计模式,上个星期学习<设计模式--代理(Proxy)模式>http://www.cnblogs.com/insus/p/4128814.html. 温故而知新, ...
- Java设计模式(16)中介模式(Mediator模式)
Mediator定义:用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator模式/中介模式 各个对象之间的交互操作非常多,每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉 ...
- C++设计模式-Mediator中介者模式
Mediator中介者模式作用:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. UML如下: Colleage抽象同事类 ...
- Mediator模式(仲裁者设计模式)
Mediator ? Mediator的意思是"仲裁者""中介者".一方面,当发生麻烦事情的时候,通知仲裁者:当发生涉及全体组员的事情时,也通知仲裁者.当仲裁者 ...
- 设计模式(十六)Mediator模式
在实际的工作小组的交流过程是,组员向仲裁者报告,仲裁者向组员下达指示,组员之间不再互相询问和指示.Mediator模式是指,当发生麻烦事情的时候,通知仲裁者:当发生涉及全体组员的事情时,也通知仲裁者. ...
- 设计模式C++描述----18.中介者(Mediator)模式
一. 举例 比如,现在中图和日本在关于钓鱼岛问题上存在争端.这时,联合国就会站出来,做为调解者,其实也没什么好调解的,钓鱼岛本来就是中国的,这是不争的事实!联合国也就是个传话者.发言人. 结构图如下: ...
- 《图解设计模式》读书笔记7-2 Mediator模式
目录 Mediator模式简介 示例程序 示例程序类图 代码 Mediator模式角色和类图 角色 模式类图 思路拓展 简单化 角色复用 Mediator模式简介 Mediator模式即中介者模式,可 ...
- 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考
http://www.cnblogs.com/insus/p/4142264.html 重构if...else...或者switch程序块 为 中介者(Mediator)模式.的思考 首先普世的编程架 ...
- 设计模式——外观模式(Facade)
1. 概述 外观模式,我们通过外观的包装,使应用程序只能看到外观对象,而不会看到具体的细节对象,这样无疑会降低应用程序的复杂度,并且提高了程序的可维护性. 例子1:一个电源总开关可以控制四盏灯 ...
- linkin大话设计模式--常用模式总结
linkin大话设计模式--常用模式总结 一,常用设计模式定义 Abstract Factory(抽象工厂模式):提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们具体的类. Adapter( ...
随机推荐
- opencv C++ Mat构造函数
cv::Scalar scalar1(v); cv::Mat mat3(size,CV_8UC1,scalar1); std::cout<<mat3<<std::endl; s ...
- InnoDB 中 B+ 树索引的分裂
数据库中B+树索引的分裂并不总是从页的中间记录开始,这样可能会导致空间的浪费,例如下面的记录: 1, 2, 3, 4, 5, 6, 7, 8, 9 插入式根据自增顺序进行的,若这时插入10这条记录后需 ...
- Spring:如何实现注解的组合
Spring中存在很多注解组合的情况,例如@RestController @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @ ...
- springboot_自动配置原理
目录 1.1 @SpringBootApplication 2.1 @EnableAutoConfiguration 2.1.1 @AutoConfigurationPackage 2.1.2 @Im ...
- JAVA集合框架 - Collection
collection大致介绍 Collection是集合层次结构中的根接口. 集合表示一组对象.有些集合允许重复元素,有些则不允许.有些是有序的,有些是无序的. JDK没有提供此接口的任何直接实现:它 ...
- Postman之API测试使用全指南
Postman Postman是一个可扩展的API开发和测试协同平台工具,可以快速集成到CI/CD管道中.旨在简化测试和开发中的API工作流. Postman 工具有 Chrome 扩展和独立客户端, ...
- Markdown-it-latex2img
Markdown-it-latex2img LaTex plugin for markdown-it markdown parser,Server side MathJax Renderer. Bac ...
- Linux distributions 发布网站
Red Hat: http://www.redhat.com SuSE: https://www.suse.com Fedora: https://getfedora.org/ CentOS: htt ...
- Using mlock ulimits for SHM_HUGETLB is deprecated
Using mlock ulimits for SHM_HUGETLB is deprecated 前言 刚检查一个集群数据库的系统日志,发现如下信息: /var/log/message, [root ...
- composer更新命令及常用命令
原文地址:https://www.wjcms.net/archives/composer更新命令及常用命令 composer作为php的包依赖管理工具,开发者将开发的工具包上传,然后使用者能很方便的使 ...