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> /// 中介者模式,定义了一个中介对象来封装一系列对象之间的交互关 ...
随机推荐
- 喵哈哈村的魔法考试 Round #12 (Div.2) 题解
A 注意答案会超过int,考虑分l,r奇数和偶数来考虑即可. #include<bits/stdc++.h> using namespace std; long long l,r; int ...
- 解决qt提示:qt.network.ssl: QSslSocket: cannot call unresolved function DH_free
方法一(解决):把C:\Qt\Qt5.8.0\Tools\QtCreator\bin下的libeay32.dll和ssleay32.dll库复制到C:\Qt\Qt5.8.0\5.8\msvc2015_ ...
- C# 8.0中的模式匹配
C# 8.0中的模式匹配相对C# 7.0来说有了进一步的增强,对于如下类: class Point{ public int X { get; } public int Y { get; } ...
- Java项目收藏
数据库 MyCat:数据库中间件 IM.消息推送 t-io:不仅仅是百万级即时通讯框架 tio-im:基于t-io写的IM,主要目标降低即时通讯门槛,实现多端不同协议间的消息发送如http.webso ...
- android:ListView 的简单用法
首 先新 建 一个 ListViewTest 项 目, 并 让 ADT 自 动帮 我 们创 建 好 活动 . 然后 修 改 activity_main.xml 中的代码,如下所示: <Linea ...
- android_双击退出
/** * 设置高速双击退出程序 */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-g ...
- jni4net使用小结
网站首页 http://jni4net.com/ 一个简单的例子: 1) 从这里下载binaries,然后解压缩. https://sourceforge.net/projects/jni4 ...
- 谷歌、亚马逊相继宣布屏蔽 Flash 广告,又一个时代行将结束?【转载+整理】
原文地址 其实,如果你看一下乔布斯的传记,早在十几年以前,乔布斯就说过类似的话,他狠透了 Flash~ 据 InfoWorld 报道,谷歌已经正式宣布,在默认情况下,Chrome将不再自动播放Web页 ...
- Python:提取网页中的电子邮箱
import requests, re #regex = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"#这个正则表达式过滤 ...
- [Python设计模式] 第6章 衣服搭配系统——装饰模式
github地址:https://github.com/cheesezh/python_design_patterns 题目 设计一个控制台程序,可以给人搭配嘻哈风格(T恤,垮裤,运动鞋)或白领风格( ...