一、什么是中介者模式

  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. Cocos Creator的小点

    声明的时候,变量如此:但用的时候就变成了border,找了很久的问题,一直没找到啊,后来就发现命名的时候和内置的一定不要太相似否则后悔的只能是自己: cc.Class({ extends: cc.Co ...

  2. nginx编译安装on mac

    一.编译安装模块 如果是原有包中就有的模块,编译时, ./configure --with-xxx 就可以, 如果是第三方模块,可使用 --add-module, 如果有多个模块的话,只需要多次使用- ...

  3. ASP.NET WebAPI构建API接口服务实战演练

    一.课程介绍 一.王小二和他领导的第一次故事 有一天王小二和往常一下去上早班,刚吃完早餐刚一打开电脑没一会儿.王小二的领导宋大宝走到他的面前,我们现在的系统需要提供服务给其他内部业务系统,我看你平时喜 ...

  4. 七周七语言之Ruby

    1.安装 Ubuntu 14.04 sudo apt-get install ruby version 1.9.1 2.命令行运行: irb 3.文挡查看:man RDoc 4.猜数字 2.2.7程序 ...

  5. SQL与MySQL基本

    一:概念辨析 数据库(database):是一种保存有组织的数据的容器. 数据库软件(DBMS):使用DBMS操作数据库.访问数据库. SQL:结构化查询语言,专门用来与数据库通信的语言.几乎所有DB ...

  6. 卸载系统自动jdk

    执行下面的代码可以看到当前各种JDK版本和配置: sudo update-alternatives --config java   卸载系统自动jdk [root@localhost soft]# r ...

  7. String的split方法支持正则表达式

    String的split方法支持正则表达式: 1. 正则表达式\s表示匹配任何空白字符 2. +表示匹配一次或多次

  8. C#-MVC开发微信应用(2)--微信消息的处理和应答

    微信应用使用场景和商机很多,所以这也是一个技术的方向,因此,有空研究下.学习下微信的相关开发,也就成为SNF完善的必要条件了.本系列文章希望从一个循序渐进的角度上,全面介绍微信的相关开发过程和相关经验 ...

  9. 【转】css3实现文字闪烁,改变透明度

    <style> @-webkit-keyframes shake{ 0%{ opacity: 1; } 50%{ opacity: 0.5; } 100%{ opacity: 1; } } ...

  10. Oracle分割字符串 REGEXP_SUBSTR用法

    分割字符串中所有指定字符,然后成多行参数说明,参数1: 待分割字符串参数2:正则表达式参数3:起始位置,从第几个字符开始正则表达式匹配(默认为1)参数4:标识第几个匹配组,默认为1参数5:模式('i' ...