一、什么是中介者模式

  Mediator模式也叫中介者模式,是由GoF提出的23种 软件设计模式的一种。Mediator模式是行为模式之一, 在Mediator模式中,类之间的交互行为被统一放在 Mediator的对象中,对象通过Mediator对象同其他对象 交互,Mediator对象起着控制器的作用。

二、中介者模式的结构

三、中介者模式的角色和职责

  mediator     中介者类的抽象父类。

  concreteMediator     具体的中介者类。

  colleague 关联类的抽象父类。

  concreteColleague 具体的关联类。

四、中介者模式的优点

  1,将系统按功能分割成更小的对象,符合类的最小设计原则

  2,对关联对象的集中控制

  3,减小类的耦合程度,明确类之间的相互关系:当类之间的关系过于复杂时,其中任何一个类的修改都会影响到其他类,不符合类的设计的开闭原则 ,而Mediator模式将原来相互依存的多对多的类之间的关系简化为Mediator控制类与其他关联类的一对多的关系,当其中一个类修改时,可以对其他关联类不产生影响(即使有修改,也集中在Mediator控制类)。

  4,有利于提高类的重用性

eg1:

 public abstract class Person {
private String name;
private int condition; public Person(String name, int condition) {
this.name = name;
this.condition = condition;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCondition() {
return condition;
} public void setCondition(int condition) {
this.condition = condition;
} public abstract void getPartner(Person person); }

男人

 public class Man extends Person {

     public Man(String name, int condition) {
super(name, condition);
} public void getPartner(Person person) {
if(person instanceof Man) {
System.out.println("汗,我不是同性恋!");
} else {
if(this.getCondition() == person.getCondition()) {
System.out.println(this.getName() + "和" + person.getName() + "绝配");
} else {
System.out.println(this.getName() + "和" + person.getName() + "不相配");
}
}
}
}

女人

 public class Woman extends Person {

     public Woman(String name, int condition) {
super(name, condition);
} public void getPartner(Person person) {
if(person instanceof Woman) {
System.out.println("汗,我不是同性恋!");
} else {
if(this.getCondition() == person.getCondition()) {
System.out.println(this.getName() + "和" + person.getName() + "绝配");
} else {
System.out.println(this.getName() + "和" + person.getName() + "不相配");
}
}
}
}

测试

 public class MainClass {
public static void main(String[] args) {
Person zhangsan = new Man("张三",5);
Person lisi = new Man("李四",6); Person xiaofang = new Woman("小芳", 6); zhangsan.getPartner(xiaofang);//获取同伴
lisi.getPartner(xiaofang);
zhangsan.getPartner(lisi); }
}

=========================================================================

eg2:

 public abstract class Person {
private String name;
private int condition; //条件
private Mediator mediator; //调解人,中介 public Person(String name, int condition, Mediator mediator) {
super();
this.name = name;
this.condition = condition;
this.mediator = mediator;
} public Mediator getMediator() {
return mediator;
} public void setMediator(Mediator mediator) {
this.mediator = mediator;
} public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getCondition() {
return condition;
} public void setCondition(int condition) {
this.condition = condition;
} public abstract void getPartner(Person person); }

中介物

 //调解人,中介物
public class Mediator {
private Man man;
private Woman woman; public void setMan(Man man) {
this.man = man;
}
public void setWoman(Woman woman) {
this.woman = woman;
} public void getPartner(Person person) {
//将搭档设置上
if(person instanceof Man) {
this.setMan((Man)person);
} else {
this.setWoman((Woman)person);
}
//判断条件
if(man == null || woman == null) {
System.out.println("汗,我不是同性恋!");
} else { if(man.getCondition() == woman.getCondition()) {
System.out.println(man.getName() + "和" + woman.getName() + "绝配");
} else {
System.out.println(man.getName() + "和" + woman.getName() + "不相配");
}
}
}
}

男人

 public class Man extends Person {

     public Man(String name, int condition,Mediator mediator) {
super(name, condition, mediator);
} public void getPartner(Person person) {
this.getMediator().setMan(this);
this.getMediator().getPartner(person);
}
}

女人

 public class Woman extends Person {

     public Woman(String name, int condition,Mediator mediator) {
super(name, condition, mediator);
} public void getPartner(Person person) {
this.getMediator().setWoman(this);
this.getMediator().getPartner(person);
}
}

测试

 public class MainClass {
public static void main(String[] args) {
Mediator mediator = new Mediator();
Person zhangsan = new Man("张三",7,mediator);
Person lisi = new Man("李四",7,mediator);
Person xiaofang = new Woman("小芳",7,mediator); zhangsan.getPartner(lisi); xiaofang.getPartner(lisi);
}
}

17中介者模式Mediator的更多相关文章

  1. [设计模式] 17 中介者模式 Mediator Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...

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

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

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

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

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

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

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

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

  6. 中介者模式(Mediator Pattern)

    用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...

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

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

  8. 设计模式之中介者模式(Mediator)摘录

    23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...

  9. 18.中介者模式(Mediator Pattern)

    using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...

随机推荐

  1. angular2项目关于动画的处理

    animations动画在angular2官网里面已经讲解很详细了,那么动画功能在实际项目中应该如何组织文件,动画文件放在哪个位置,如何来组织结构使得动画模块和其他模块之间运作调理清晰呢,下面参照Ni ...

  2. Reactor 3 学习笔记(2)

    接上篇继续学习各种方法: 4.9.reduce/reduceWith @Test public void reduceTest() { Flux.range(1, 10).reduce((x, y) ...

  3. Linux下批量修改文件名(rename)

    原文地址: http://blog.csdn.net/sea_shore/article/details/6102437 1.rename命令批量修改文件名, 其实linux下可以使用别的办法来批量修 ...

  4. Java全栈程序员之03:Ubuntu下安装idea

    JetBrains的产品我曾经用过很长一段时间,它们是resharper和dotcover.VS号称宇宙最强IDE,直到它遇到了resharper,我们才知道,原来vs可以更好.DotCover是一个 ...

  5. boolean和Boolean, char和Character , byte和Byte, short和Short, int和Integer , long和Long , float和Float, double和Double的区别 , String和StringBuffer的区别

    Java提供两种不同的类型:引用类型和原始类型(内置类型).Int是java的原始数据类型,Integer是java为int提供的封装类. Java为每个原始数据类型提供了封装类. 其中原始数据类型封 ...

  6. C# SpinWait 实现

    其实SpinWait的code 非常简单,以前看过很多遍,但是从来都没有整理过,整理也是再次学习吧. 我们先看看SpinWait的一些评论或者注意点吧:如果等待某个条件满足需要的时间很短,而且不希望发 ...

  7. [Functional Programming] Using Lens to update nested object

    For example, in React application, we have initial state; const data = { nextId: 4, todoFilter: 'SHO ...

  8. 微软BI 之SSIS 系列 - 使用 Multicast Task 将数据同时写入多个目标表,以及写入Audit 与增量处理信息

    开篇介绍 在 SSIS Data Flow 中有一个 Multicast 组件,它的作用和 Merge, Merge Join 或者 Union All 等合并数据流组件对比起来作用正好相反.非常直观 ...

  9. redhat7.0配置网卡

    1.切换到网卡配置目录: cd /etc/sysconfig/network-scripts 2.编辑网卡信息 如 vim ifcfg-enpos3 TYPE=Ethernet #设备类型 BOOTP ...

  10. [AaronYang]那天有个小孩跟我说Js正则

    按照自己的思路学习Node.Js 随心出发.突破正则冷门知识点,巧妙复习正则常用知识点 标签:AaronYang  茗洋  Node.Js 正则 Javascript 本篇博客地址:http://ww ...