设计模式之Mediator模式(笔记)
中介者模式:用一个中介对象来封装一系列的对象交互。
中介者使各对象不须要显式的相互引用,从而使其耦合松散。并且能够独立的改变它们之间的交互。
使用场合:中介者模式一般应用于一组对象以定义良好可是复杂的方式进行通信的场合。以及想定制一个分布在多个类中的行为,而不是想生成太多的子类的场合。
首先定义一个中介者接口IMediator
public interface IMediator {
public void send(String message,Colleague colleague);
}
接着定义抽象同事类Colleague
public abstract class Colleague {
protected IMediator mediator;
public Colleague(IMediator mediator){
this.mediator=mediator;
}
}
接着定义详细的同事类继承Colleague 抽象同事类
public class ConcreteColleague1 extends Colleague{
public ConcreteColleague1(IMediator mediator) {
super(mediator);
}
public void send(String message){
mediator.send(message, this);
}
public void notify(String message){
System.out.println("同事1获得消息:"+message);
}
}
public class ConcreteColleague2 extends Colleague{
public ConcreteColleague2(IMediator mediator) {
super(mediator);
}
public void send(String message){
mediator.send(message, this);
}
public void notify(String message){
System.out.println("同事2获得消息:"+message);
}
}
然后定义一个详细的中介者对象ConcreteMediator继承IMediator
public class ConcreteMediator implements IMediator{
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2;
public void setColleague1(ConcreteColleague1 colleague1){
this.colleague1=colleague1;
}
public ConcreteColleague1 getColleague1(){
return colleague1;
}
public void setColleague2(ConcreteColleague2 colleague2){
this.colleague2=colleague2;
}
public ConcreteColleague2 getColleague2(){
return colleague2;
}
@Override
public void send(String message, Colleague colleague) {
if(colleague==colleague1){
colleague2.notify(message);
}
else{
colleague1.notify(message);
}
}
}
client代码
public static void main(String[] args) {
//中间者模式
ConcreteMediator mediator=new ConcreteMediator();
ConcreteColleague1 colleague1=new ConcreteColleague1(mediator);
ConcreteColleague2 colleague2=new ConcreteColleague2(mediator);
mediator.setColleague1(colleague1);
mediator.setColleague2(colleague2);
colleague1.send("你好,中国");
colleague2.send("你好,美国");
}
设计模式之Mediator模式(笔记)的更多相关文章
- 设计模式--中介(Mediator)模式
时隔很长一段时,现在又重温设计模式,上个星期学习<设计模式--代理(Proxy)模式>http://www.cnblogs.com/insus/p/4128814.html. 温故而知新, ...
- 设计模式之Composite模式(笔记)
组合模式:将对象组合成树形结构以表示"部分-总体"的层次结构. 组合模式使得用户对单个对象和组合对象的使用具有一致性. 适用场合:当需求中是体现部分与总体层次的结构时,以及希望用户 ...
- 设计模式:mediator模式
目的:解决多组件之间的通信问题,使得组件之间的通信变得简单 核心:提供一个管理类,用来处理组件之间的通信,所有的组件只和管理类通信,组件彼此之间不在单独通信 例子: class Mediator { ...
- 《图解设计模式》读书笔记7-2 Mediator模式
目录 Mediator模式简介 示例程序 示例程序类图 代码 Mediator模式角色和类图 角色 模式类图 思路拓展 简单化 角色复用 Mediator模式简介 Mediator模式即中介者模式,可 ...
- 设计模式---接口隔离模式之中介者模式(Mediator)
一:概念 在Mediator模式中,类之间的交互行为被统一放在Mediator的对象中,对象通过Mediator对象同其他对象交互.Mediator对象起到控制器的作用 二:动机 在软件构建的过程中, ...
- Java设计模式(16)中介模式(Mediator模式)
Mediator定义:用一个中介对象来封装一系列关于对象交互行为. 为何使用Mediator模式/中介模式 各个对象之间的交互操作非常多,每个对象的行为操作都依赖彼此对方,修改一个对象的行为,同时会涉 ...
- JavaScript设计模式之策略模式(学习笔记)
在网上搜索“为什么MVC不是一种设计模式呢?”其中有解答:MVC其实是三个经典设计模式的演变:观察者模式(Observer).策略模式(Strategy).组合模式(Composite).所以我今天选 ...
- 设计模式(17)--Mediator(中介者模式)行为型
作者QQ:1095737364 QQ群:123300273 欢迎加入! 1.模式定义: 用一个中介者对象封装一系列的对象交互,中介者使各对象不需要显示地相互作用,从而使耦合松散,而且可以 ...
- Mediator模式(仲裁者设计模式)
Mediator ? Mediator的意思是"仲裁者""中介者".一方面,当发生麻烦事情的时候,通知仲裁者:当发生涉及全体组员的事情时,也通知仲裁者.当仲裁者 ...
随机推荐
- elastic学习笔记
要点 不同工具之间版本匹配很重要由点及面,先实践起来再学细节的原理和使用 技术栈 laravel5.5框架+scout组件+elasticsearch6.3.0搜索引擎 辅助 elasticsearc ...
- PHP下的Oauth2.0尝试 - OpenID Connect
OpenID Connect OpenID Connect简介 OpenID Connect是基于OAuth 2.0规范族的可互操作的身份验证协议.它使用简单的REST / JSON消息流来实现,和之 ...
- js实现鼠标吸附线条效果
如图,箭头→为鼠标位置,鼠标会带有吸附着一些线条的效果,具体效果可在我的博客查看,当然,这也是可很受欢迎很常见的效果了=3= <script> !function(){ function ...
- 【Codeforces Round #476 (Div. 2) [Thanks, Telegram!] D】Single-use Stones
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设长度为L的所有区间里面,石头的个数的最小值为k 设取到k的区间为l,r 那么k就为最多能通过的青蛙个数. 假设k再大一点.比如为k ...
- 洛谷——P2483 [SDOI2010]魔法猪学院
https://www.luogu.org/problem/show?pid=2483 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的 ...
- string 简单实现
namespace ss{ class string { friend ostream& operator <<(ostream&, const string&); ...
- Restful技术
一.概述 Restful技术是一种架构风格(Representational State Transfer)表现层状态转化,而不是一种编程标准. 之前前后端混在一起,前端通过mapping映射找到后端 ...
- moveToThread的根本目的还是为了处理QObject的事件循环(如果为空指针的话,当前对象的所有的事件都不处理了),看官方说明就知道了
Changes the thread affinity for this object and its children. The object cannot be moved if it has a ...
- linux使用windows中编辑的文件,格式问题
参考:https://blog.csdn.net/yongan1006/article/details/8142527 运行脚本时出现了这样一个错误,打开之后并没有找到所谓的^M,查了之后才知道原来是 ...
- 2435: [Noi2011]道路修建(树上操作)
2435: [Noi2011]道路修建 题目:传送门 题解: 建完边之后以1为根建树,统计深度和各个点的子树大小(包括自己) 询问的时候:答案=长度*abs(n-深度大的点的子树大小*2) ans+= ...