前言:【模式总览】——————————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的更多相关文章

  1. 设计模式-中介者模式(Mediator)

    场景分析: 众所周知,电脑有很多组成部分,如硬盘.内存.光驱.音频.键盘等,各个组件之间协同工作才能保证电脑的正常运行. 如果各个组件之间直接交互,可能会比较复杂,如下图: 将上面的各个组件抽象成类, ...

  2. 大话设计模式--中介者模式 Mediator --C++实现实例

    1. 中介者模式: 用一个中介对象来封装一系列的对象交互,中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立改变他们之间的交互. Mediator的出现减少了各个Colleague的耦 ...

  3. 23种设计模式--中介者模式-Mediator Pattern

    一.中介者模式的介绍     中介者模式第一下想到的就是中介,房子中介,婚姻中介啊等等,当然笔者也希望来个婚姻中介给我介绍一个哈哈哈,,回归正题中介者模式分成中介者类和用户类,根据接口编程的方式我们再 ...

  4. 深入浅出设计模式——中介者模式(Mediator Pattern)

    模式动机 在用户与用户直接聊天的设计方案中,用户对象之间存在很强的关联性,将导致系统出现如下问题: 系统结构复杂:对象之间存在大量的相互关联和调用,若有一个对象发生变化,则需要跟踪和该对象关联的其他 ...

  5. 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...

  6. 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)

    设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...

  7. 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)

    原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...

  8. 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互

    说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...

  9. 二十四种设计模式:中介者模式(Mediator Pattern)

    中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...

  10. JAVA 设计模式 中介者模式

    用途 中介者模式 (Mediator) 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 中介者模式是一种行为型模式. 结 ...

随机推荐

  1. C#中public与private与static

    现在静下心来想要重新细致的过一遍C#,因为自己做C#没有底气,, 闲话少说 先来一句话 public(共有的) 声明的方法和属性,可以被外部调用. private(私有的) 声明的方法和属性,只能在本 ...

  2. 【转】php容易犯错的10个地方

    原文地址: http://www.toptal.com/php/10-most-common-mistakes-php-programmers-make 译文地址:http://codecloud.n ...

  3. odoo明细表汇总数据

    一.在主表中#改动地方 总结算金额 求和:def _get_subtotal2(self, cr, uid, ids, field_name, arg, context=None): # 初始化 re ...

  4. 在ASP.NET非MVC环境中(WebForm中)构造MVC的URL参数,以及如何根据URL解析出匹配到MVC路由的Controller和Action

    目前项目中有个需求,需要在WebForm中去构造MVC的URL信息,这里写了一个帮助类可以在ASP.NET非MVC环境中(WebForm中)构造MVC的URL信息,主要就是借助当前Http上下文去构造 ...

  5. MariaDB数据库性能优化

    1. 硬件优化 1.1 内存(Memory) 内存是最重要的因素,因为它允许您调整服务器系统变量.更多的内存意味着可以将更大的密钥和表缓存存储在内存中,从而减少磁盘访问速度,降低一个数量级. 如果未将 ...

  6. c# 淘宝运单查询

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  7. 20155317王新玮《网络对抗技术》实验8 WEB基础实践

    20155317王新玮<网络对抗技术>实验8 WEB基础实践 一.实验准备 1.0 实验目标和内容 Web前端HTML.能正常安装.启停Apache.理解HTML,理解表单,理解GET与P ...

  8. 20155320《网络对抗》Exp2 后门原理与实践

    20155320<网络对抗>Exp2 后门原理与实践 [实验内容] (3.5分) (1)使用netcat获取主机操作Shell,cron启动 (2)使用socat获取主机操作Shell, ...

  9. 【转载】SSD断电保护原理

    异常掉电的隐患 若没有合理的掉电保护机制,而异常掉电的发生又不可避免,当发生异常掉电,会引发很多问题. (1)丢盘 异常掉电,会使得映射表来不及保存,丢失逻辑地址到物理地址的映射,待重新上电后,SSD ...

  10. unity2D背景移动补偿从而获得3d错觉效果

    2d平台跳跃游戏当相机移动的时候背景跟随进行微调移动,从而使得玩家获得3d的错觉 using System.Collections;using System.Collections.Generic;u ...