17中介者模式Mediator
一、什么是中介者模式
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的更多相关文章
- [设计模式] 17 中介者模式 Mediator Pattern
在GOF的<设计模式:可复用面向对象软件的基础>一书中对中介者模式是这样说的:用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变 ...
- 【转】设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据"单一职责原则",我们应该尽量将对象细化,使其只负责或呈现单一的职 ...
- 二十四种设计模式:中介者模式(Mediator Pattern)
中介者模式(Mediator Pattern) 介绍用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 示例有一个Messa ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern)
原文:乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 中介者模式(Mediator Pattern) 作者:weba ...
- 中介者模式(Mediator Pattern)
用于减少多个对象或类之间的通信复杂性. 此模式提供了一个中介类,它通常处理不同类之间的所有通信,并支持通过松散耦合来维护代码.中介者模式属于行为模式类别. 实现实例 在这里通过一个聊天室的示例来演示中 ...
- 设计模式系列之中介者模式(Mediator Pattern)——协调多个对象之间的交互
说明:设计模式系列文章是读刘伟所著<设计模式的艺术之道(软件开发人员内功修炼之道)>一书的阅读笔记.个人感觉这本书讲的不错,有兴趣推荐读一读.详细内容也可以看看此书作者的博客https:/ ...
- 设计模式之中介者模式(Mediator)摘录
23种GOF设计模式一般分为三大类:创建型模式.结构型模式.行为模式. 创建型模式抽象了实例化过程.它们帮助一个系统独立于怎样创建.组合和表示它的那些对象.一个类创建型模式使用继承改变被实例化的类,而 ...
- 18.中介者模式(Mediator Pattern)
using System; namespace Test { class Program { /// <summary> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...
随机推荐
- World final 2017 题解
链接:https://pan.baidu.com/s/1kVQc9d9 Problem A: #include <cstdio> #include <algorithm> #i ...
- Qt打包成单独可执行的exe文件
1.将图标newIco.ico复制到工程目录下. 2.在工程目录下新建空白txt文档,添加以下内容. IDI_ICON1 ICON DISCARDABLE "newIco.ico" ...
- 【译】如何在 Android 5.0 上获取 SD卡 的读写权限
因为最近项目需要,涉及到 SD卡 的读写操作,然而申请 <!-- 读写权限 --> <uses-permission android:name="android.permi ...
- centos 终端界面代理设置
一.centos自带界面设置代理 1. 界面设置 squid默认代理端口3128. 2. firefox设置 设置 -> 局域网设置 -> ip:port / username:passw ...
- 5、Python文件类型
Python文件类型 源代码 Python源代码的文件以"py"为扩展名,由Python程序解释,不需要编译 字节代码 Python源文件经编译后生成的扩展名为"pyc& ...
- Session.Abandon和Session.Clear的实现和区别
我在网上找了一个比较贴切的描述: Session.Clear()就是把Session对象中的所有项目都删除了,Session对象里面啥都没有.但是Session对象还保留.Session.Abando ...
- inet_ntoa内存问题
最近写的一个程序,大致用到以下代码: WSADATA wsaData; WSAStartup (MAKEWORD( 2, 2 ),&wsaData); struct addrinfo *aiL ...
- Next Permutation leetcode java
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater per ...
- linux性能采用工具oprofile使用
1.先收藏几篇博文,先解决问题,周末继续. http://www.cnblogs.com/bangerlee/archive/2012/08/30/2659435.html http://blog.s ...
- React 中 keys 的作用是什么?
Keys 是 React 用于追踪哪些列表中元素被修改.被添加或者被移除的辅助标识. render () { return ( <ul> {this.state.todoItems.map ...