title: Day56-设计模式-中介者模式
date: 2021-03-26 17:52:56
author: Liu_zimo
设计模式
- 设计模式分为三种类型,共23种
- 创建型模式:单例模式、抽象工厂模式、原型模式、建造者模式、工厂模式。
- 结构型模式:适配器模式、桥接模式、装饰模式、组合模式、外观模式、享元模式、代理模式。
- 行为型模式:模版方法模式、命令模式、访问者模式、迭代器模式、观察者模式、中介者模式、备忘录模式、解释器模式(Interpreter模式)、状态模式、策略模式、职责链模式(责任链模式)。
中介者模式
- 智能家庭项目:
- 智能家庭包括各种设备,闹钟、咖啡机、电视机、窗帘等
- 主人要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为:
- 闹铃响起->咖啡机开始做咖啡->窗帘自动落下->电视机开始播放
- 传统方案
- 当各电器对象有多种状态改变时,相互之间的调用关系会比较复杂
- 各个电器对象彼此联系,你中有我,我中有你,不利于松耦合
- 各个电器对象之间所传递的消息(参数),容易混乱
- 当系统增加一个新的电器对象时,或者执行流程改变时,代码的可维护性、扩展性都不理想 → 考虑中介者模式
基本介绍
- 中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互
- 中介者模式属于行为型模式,使代码易于维护
- 比如MVC模式,C(Controller控制器)是M(ModeI模型)和V(View视图)的中介者,在前后端交互时起到了中间人的作用
- 对原理类图的说明-即(中介者模式的角色及职责)
- Mediator就是抽象中介者,定义了同事对象到中介者对象的接口
- Colleague是抽象同事类
- ConcreteMediator具体的中介者对象,实现抽象方法,他需要知道所有的具体的同事类,即以一个集合来管理HashMap,并接受某个同事对象消息,完成相应的任务
- ConcreteColleague具体的同事类,会有很多,每个同事只知道自己的行为,而不了解其他同事类的行为(方法),但是他们都依赖中介者对象
- 智能家居UML类图
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 中介抽象类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:24
*/
public abstract class Mediator {
public abstract void register(String colleagueName, Colleague colleague);
public abstract void getMessage(int stateChange, String colleagueName);
public abstract void sendMessage();
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 同事抽象类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:15
*/
public abstract class Colleague {
private Mediator mediator;
public String name;
public Colleague(Mediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public Mediator getMediator(){ return this.mediator; }
public abstract void sendMessage(int stateChange);
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 闹钟类 - 具体的同事类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:17
*/
public class Alarm extends Colleague{
public Alarm(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this); // 创建闹钟同事时,将自己放入到ConcreteMediator对象中[集合]
}
public void sendAlarm(int stateChange){ sendMessage(stateChange); }
@Override
public void sendMessage(int stateChange) { this.getMediator().getMessage(stateChange,this.name); }
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 咖啡机类 - 具体的同事类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 19:27
*/
public class CoffeeMachine extends Colleague{
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void startCoffee(){ System.out.println("咖啡机 -- 打开"); }
public void finishCoffee() {
System.out.println("咖啡机 -- 咖啡制作完成");
sendMessage(0);
}
@Override
public void sendMessage(int stateChange) { this.getMediator().getMessage(stateChange,this.name); }
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 窗帘类 - 具体的同事类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:21
*/
public class Curtains extends Colleague{
public Curtains(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void upCurtains(){ System.out.println("窗帘 -- 打开"); }
@Override
public void sendMessage(int stateChange) { this.getMediator().getMessage(stateChange,this.name); }
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
/**
* 设计模式 - 中介者模式:智能家居案例
* 电视类 - 具体的同事类
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:23
*/
public class TV extends Colleague{
public TV(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name, this);
}
public void startTV(){ System.out.println("电视 -- 打开"); }
public void stopTV(){ System.out.println("电视 -- 关闭"); }
@Override
public void sendMessage(int stateChange) { this.getMediator().getMessage(stateChange,this.name); }
}
--------------------------------------------------------------------
package com.zimo.设计模式.行为型模式.中介者模式;
import java.util.HashMap;
/**
* 描述
*
* @author Liu_zimo
* @version v0.1 by 2021/3/26 18:27
*/
public class ConcreteMediator extends Mediator{
private HashMap<String, Colleague> colleagueMap;
private HashMap<String, String> interMap;
public ConcreteMediator() {
this.colleagueMap = new HashMap<>();
this.interMap = new HashMap<>();
}
@Override
public void register(String colleagueName, Colleague colleague) {
this.colleagueMap.put(colleagueName, colleague);
if (colleague instanceof Alarm){
interMap.put("Alarm", colleagueName);
}else if (colleague instanceof CoffeeMachine){
interMap.put("CoffeeMachine", colleagueName);
}else if (colleague instanceof TV){
interMap.put("TV", colleagueName);
}else if (colleague instanceof Curtains){
interMap.put("Curtains", colleagueName);
}
}
@Override
public void getMessage(int stateChange, String colleagueName) {
if (colleagueMap.get(colleagueName) instanceof Alarm){
if (stateChange == 0){
((CoffeeMachine) (colleagueMap.get(interMap.get("CoffeeMachine")))).startCoffee();
((TV)(colleagueMap.get(interMap.get("TV")))).startTV();
}else if (stateChange == 1){
((TV)(colleagueMap.get(interMap.get("TV")))).stopTV();
}
}else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine){
((Curtains)(colleagueMap.get(interMap.get("Curtains")))).upCurtains();
}else if (colleagueMap.get(colleagueName) instanceof TV){ // 这里处理tv
}else if (colleagueMap.get(colleagueName) instanceof Curtains){ // 这里处理tv
}
}
@Override
public void sendMessage() { }
}
--------------------------------------------------------------------
public static void main(String[] args) {
// 创建一个中介者
Mediator mediator = new ConcreteMediator();
Alarm alarm = new Alarm(mediator, "alarm");
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine");
Curtains curtains = new Curtains(mediator, "curtains");
TV tv = new TV(mediator, "TV");
alarm.sendAlarm(0);
coffeeMachine.finishCoffee();
alarm.sendAlarm(1);
}
注意事项和细节
- 多个类相互耦合,会形成网状结构,使用中介者模式将网状结构分离为星型结构,进行解耦
- 减少类间依赖,降低了耦合,符合迪米特原则
- 中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
- 如果设计不当,中介者对象本身变得过于复杂,这点在实际使用时,要特别注意