【设计模式】—— 中介者模式Mediator
前言:【模式总览】——————————by xingoo
模式意图
使用一个中介的对象,封装一组对象之间的交互,这样这些对象就可以不用彼此耦合。
这个中介者常常起着中间桥梁的作用,使其他的对象可以利用中介者完成某些行为活动,因此它必须对所有的参与活动的对象了如指掌!
应用场景
1 当一组对象要进行沟通或者业务上的交互,但是其关系却又很复杂混乱时,可以采用此模式。
2 当一个对象与其他的对象要进行紧密的交互,但又想服用该对象而不依赖其他的对象时。
3 想创造一个运行于多个类之间的对象,又不想生成新的子类时。
模式结构

Mediator 抽象的中介者,定义中介的规范
interface Mediator{
public void colleagueChanged(Colleague c);
}
ConcreteMediator 具体的中介者,通常内部依赖于多个业务对象
class ConcreteMediator implements Mediator{
private Colleague1 col1;
private Colleague2 col2;
public void colleagueChanged(Colleague c) {
col1.action();
col2.action();
}
public void createConcreteMediator() {
col1 = new Colleague1(this);
col2 = new Colleague2(this);
}
private Colleague1 getCol1() {
return col1;
}
public Colleague2 getCol2() {
return col2;
}
}
Colleague 抽象的业务角色
abstract class Colleague{
private Mediator mediator;
public Colleague(Mediator mediator){
this.mediator = mediator;
}
public Mediator getMediator() {
return mediator;
}
public abstract void action();
public void change(){
mediator.colleagueChanged(this);
}
}
Colleague1 Colleague2 具体的业务角色
class Colleague1 extends Colleague{
public Colleague1(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague1");
}
}
class Colleague2 extends Colleague{
public Colleague2(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague2");
}
}
全部代码
package com.xingoo.test.design.mediator;
abstract class Colleague{
private Mediator mediator; public Colleague(Mediator mediator){
this.mediator = mediator;
} public Mediator getMediator() {
return mediator;
} public abstract void action(); public void change(){
mediator.colleagueChanged(this);
}
}
class Colleague1 extends Colleague{
public Colleague1(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague1");
}
}
class Colleague2 extends Colleague{
public Colleague2(Mediator m){
super(m);
}
public void action(){
System.out.println("this is an action from Colleague2");
}
}
interface Mediator{
public void colleagueChanged(Colleague c);
}
class ConcreteMediator implements Mediator{
private Colleague1 col1;
private Colleague2 col2; public void colleagueChanged(Colleague c) {
col1.action();
col2.action();
} public void createConcreteMediator() {
col1 = new Colleague1(this);
col2 = new Colleague2(this);
} private Colleague1 getCol1() {
return col1;
} public Colleague2 getCol2() {
return col2;
} } public class Client {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
mediator.createConcreteMediator();
Colleague1 col1 = new Colleague1(mediator);
// Colleague2 col2 = new Colleague2(mediator);
mediator.colleagueChanged(col1);
}
}
运行结果
this is an action from Colleague1
this is an action from Colleague2
生活中的设计模式

毕业的同学们,第一个要解决的问题就是租房子,当白富美高富帅出没社会后,穷屌丝没了生存之地。但是只要勤劳,一样有饭吃有房住!
这里房屋中介好比是一个中介者,它知道每个租客的身份信息,当有房屋出租后,它会发送给每一个租客消息。
这样,租客们中有一个变化活动时,都会利用房屋中介,发送消息到其他的租客。下面就是模仿的一个过程。
房屋中介代码如下:
interface StateMediator{
public void sell(Tenant tenant);
}
class RealEstateAgents implements StateMediator{
private TenantA teA;
private TenantB teB;
private TenantC teC;
public void sell(Tenant tenant) {
System.out.println("海景洋房 已经租出去了!");
if(tenant instanceof TenantA){
teB.crying();
teC.crying();
}else if(tenant instanceof TenantB){
teA.crying();
teC.crying();
}else if(tenant instanceof TenantC){
teB.crying();
teA.crying();
}
}
public void createAgents(){
teA = new TenantA(this);
teB = new TenantB(this);
teC = new TenantC(this);
}
}
租客的代码如下:
abstract class Tenant{
private RealEstateAgents agent;
public Tenant(RealEstateAgents agent) {
this.agent = agent;
}
public abstract void crying();
public void renting(){
agent.sell(this);
}
}
class TenantA extends Tenant{
public TenantA(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是高富帅 TenantA!哎呀我想要!");
}
}
class TenantB extends Tenant{
public TenantB(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是白富美 TenantB!哎呀我想要!");
}
}
class TenantC extends Tenant{
public TenantC(RealEstateAgents agent) {
super(agent);
}
public void crying() {
System.out.println("我是穷屌丝 TenantC!哎呀我想要!");
}
}
产生的业务活动如下:
public class ClientTest {
public static void main(String[] args) {
RealEstateAgents agent = new RealEstateAgents();
agent.createAgents();
System.out.println("TeA 抢到了房子了!");
agent.sell(new TenantA(agent));
System.out.println("过了两个月 TeB 抢到了房子了!");
agent.sell(new TenantB(agent));
}
}
运行结果
TeA 抢到了房子了!
海景洋房 已经租出去了!
我是白富美 TenantB!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
过了两个月 TeB 抢到了房子了!
海景洋房 已经租出去了!
我是高富帅 TenantA!哎呀我想要!
我是穷屌丝 TenantC!哎呀我想要!
【设计模式】—— 中介者模式Mediator的更多相关文章
- 设计模式-中介者模式(Mediator)
场景分析: 众所周知,电脑有很多组成部分,如硬盘.内存.光驱.音频.键盘等,各个组件之间协同工作才能保证电脑的正常运行. 如果各个组件之间直接交互,可能会比较复杂,如下图: 将上面的各个组件抽象成类, ...
- 大话设计模式--中介者模式 Mediator --C++实现实例
1. 中介者模式: 用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立改变他们之间的交互. Mediator的出现减少了各个Colleague的耦 ...
- 23种设计模式--中介者模式-Mediator Pattern
一.中介者模式的介绍 中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...
- 深入浅出设计模式——中介者模式(Mediator Pattern)
模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- JAVA 设计模式 中介者模式
用途 中介者模式 (Mediator) 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式是一种行为型模式. 结 ...
随机推荐
- HDU 2709 Sumsets 经典简单线性dp
Sumsets Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
- OpenGL笔记(四) API参考
常见API glActiveTexture 选择活动纹理单元 glAttachShader 将一个着色器对象绑定到一个程序对象 void glAttachShader(GLuint program, ...
- iOS Swift WisdomScanKit图片浏览器功能SDK
iOS Swift WisdomScanKit图片浏览器功能SDK使用 一:简介 WisdomScanKit 由 Swift4.2版编写,完全兼容OC项目调用. WisdomScanKit的 ...
- # 2017-2018-2 20155319『网络对抗技术』Exp6:信息收集与漏洞扫描
2017-2018-2 20155319『网络对抗技术』Exp6:信息收集与漏洞扫描 实践内容 (1)各种搜索技巧的应用 (2)DNS IP注册信息的查询 (3)基本的扫描技术:主机发现.端口扫描.O ...
- # 2017-2018-2 20155319『网络对抗技术』Exp5:MSF基础应用
2017-2018-2 20155319『网络对抗技术』Exp5:MSF基础应用 基础问题回答 用自己的话解释什么是exploit,payload,encode exploit:使用者利用漏洞进行攻击 ...
- HTML5 本地存储的用法
HTML5 的本地存储 API 中的 localStorage 与 sessionStorage 在使用方法上是相同的,区别在于 sessionStorage 在关闭页面后即被清空,而 localSt ...
- 【转载】VS配置路径和宏
原文路径:http://blog.csdn.net/puttytree/article/details/7838419 在介绍项目配置之前,先说明两点 1. 项目配置中,最好不要使用相对路径,更不要使 ...
- 封装之property,多态,鸭子类型,classmethod与staticmethod
一.封装之Property prooerty是一种特殊的属性,访问时他会执行一段功能(函数)然后返回 '''BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法,如果我们将其做成一个属 ...
- R绘图 第十篇:绘制文本、注释和主题(ggplot2)
使用ggplot2包绘制时,为了更直观地向用户显示报表的内容和外观,需要使用geom_text()函数添加文本说明,使用annotate()添加注释,并通过theme()来调整非数据的外观. 一,文本 ...
- HTML 脚本 (Script) 实例
1.JavaScript 使 HTML 页面具有更强的动态和交互性.HTML script 元素<script> 标签用于定义客户端脚本,比如 JavaScript. script 元素既 ...