设计模式之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的意思是"仲裁者""中介者".一方面,当发生麻烦事情的时候,通知仲裁者:当发生涉及全体组员的事情时,也通知仲裁者.当仲裁者 ...
随机推荐
- tomcat使用及原理
1,Tomcat作为Servlet容器的基本功能 2,Tomcat的组成结构 Tomcat本身由一列的可配置的组件构成,其中核心组件是Servlet容器组件,它是所有其他Tomcat组件的顶层容器.T ...
- Redis windows版本的启停bat脚本命令
Reids windows版本安装 redis windows官网推荐:https://github.com/MicrosoftArchive/redis/releases 下载解压即可. 启停bat ...
- 关于android的设备管理器-DevicePolicyManager(一)
在Andorid的设置->安全里面有个设备管理器的选项,相信大部分android用户都不太会去注意这个东西.近期在安装了一个应用之后发现这个里面的东西变了.怎么回事呢,研究研究看看.</s ...
- leetcode第一刷_Reverse Linked List II
翻转链表绝对是终点项目,应该掌握的,这道题要求的是翻转一个区间内的节点.做法事实上非常相似,仅仅只是要注意判定開始是头的特殊情况,这样head要更新的,还有就是要把翻转之后的尾部下一个节点保存好,要么 ...
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- openssl之EVP系列之10---EVP_Sign系列函数介绍
openssl之EVP系列之10---EVP_Sign系列函数介绍 ---依据openssl doc/crypto/EVP_SignInit.pod翻译 (作者:DragonKing, ...
- 【翻译自mos文章】怎么正确的计算一个ip地址的subnet id?
怎么正确的计算一个ip地址的subnet id? 来源于: How to calculate the correct subnet for an interface (文档 ID 1059759.1) ...
- 杭电3501Calculation 2 欧拉函数
Calculation 2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
- JAVA设计模式之【桥接模式】
桥接模式 蜡笔中颜色和型号之间存在耦合 毛笔中,颜色和型号解耦了 如果软件系统中某个类存在两个独立变化的维度,桥接模式可以将两个维度分离出来 角色 抽象类 扩充抽象类 实现类接口 提供基本操作 抽象类 ...
- m_Orchestrate learning system---十四、数据表中字段命名规则
m_Orchestrate learning system---十四.数据表中字段命名规则 一.总结 一句话总结:a.保证唯一 b.见名知意 1.注意php中的数组类函数和字符串类函数的前缀? 数组类 ...