中介者模式
智能家庭项目
1)智能家庭包括各种设备,闹钟,咖啡机,电视机,窗帘等等。
2)主人要看电视时,各个设备可以协同工作,自定完成看电视的准备工作,比如流程为:闹铃响起->咖啡机开始做咖啡->窗帘自动落下,电视机开始播放
传统方案解决智能家庭管理问题
传统的方式的问题分析
1)当各电视对象有多种状态改变时,互相之间的调用关系会比较复杂
2)各个电器对象彼此练习,你中有我,我中有你,不利于送耦合
3)各个电器对象之间所传递的消息(参数),容易混乱
3)当系统增加一个新的电器对象时,或者执行流程改变时,代码的可维护性、扩展性都不理想,考虑中介者模式
1、中介者模式
基本介绍
1)中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互。中介者使各个对象要显示地互相引用,从而使其耦合松散,而且可以独立地改变他们之间的交互
2)中介者模式属于行为型模式,使代码易于维护
3)比如MVC模式,C(Controller)M(Model)V(View)的中介者,在前后端交互时起到了中间人的作用
中介者模式原理图
1)Mediator就是抽象中介者,定义了同事对象到中介者对象的接口
2)Colleague是抽象同事类
3)Concrete Mediator具体的中介者对象,实现抽象方法,他需要知道所有的具体的同事类,即以一个集合来管理Hash Map,并接受某个同事对象消息,完成相应的任务
4)ConcreteColleague具体的同事类,会有很多,每个同事只知道自己的行为,而不了解其他同事类的行为(方法),
但是他们都依赖中介者对象
中介者模式应用实例-智能家庭管理
package designPatterns.mediator;
//同事抽象类
public abstract class Colleague {
private Mediator mediator;
public String name;
abstract protected void sendMessage(int stateChange);
public Colleague(Mediator mediator,String name){
this.mediator=mediator;
this.name=name;
}
public Mediator getMediator(){
return this.mediator;
}
}
package designPatterns.mediator;
//具体的同时类(报警器)
public class Alarm extends Colleague {
public Alarm(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm同事对象的时,将自己放入到ConcreteMediator对象中【集合】
mediator.register(name, this);
}
public void sendAlarm(int stateChange) {
sendMessage(stateChange);
}
@Override
protected void sendMessage(int stateChange) {
//调用中介者对象的getMessage
this.getMediator().getMessage(stateChange, this.name);
}
}
package designPatterns.mediator;
//咖啡机
public class CoffeeMachine extends Colleague{
public CoffeeMachine(Mediator mediator, String name) {
super(mediator, name);
mediator.register(name,this);
}
public void startCoffee(){
System.out.println("It's time to startCoffee!");
}
public void finishCoffee(){
System.out.println("After 5 minutes!");
System.out.println("Coffee is on");
sendMessage(0);
}
@Override
protected void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange,this.name);
}
}
package designPatterns.mediator;
//电视
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
protected void sendMessage(int stateChange) {
this.getMediator().getMessage(stateChange, this.name);
}
}
package designPatterns.mediator;
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 designPatterns.mediator;
import java.util.HashMap;
//具体的中介者
public class ConcreteMediator extends Mediator {
private HashMap<String, Colleague> colleagueHashMap;
private HashMap<String, String> interMap;
public ConcreteMediator() {
this.colleagueHashMap = new HashMap<>();
this.interMap = new HashMap<>();
}
@Override
public void register(String colleagueName, Colleague colleague) {
colleagueHashMap.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);
}
}
//根据得到的消息,完成对应的任务
@Override
public void getMessage(int stateChange, String colleagueName) {
//处理闹钟发出的消息
if (colleagueHashMap.get(colleagueName) instanceof Alarm) {
if (stateChange == 0) {
CoffeeMachine coffeeMachine = (CoffeeMachine) colleagueHashMap.get(interMap.get("CoffeeMachine"));
TV tv = (TV) colleagueHashMap.get(interMap.get("TV"));
coffeeMachine.startCoffee();
tv.startTV();
} else if (stateChange == 1) {
TV tv = (TV) colleagueHashMap.get(interMap.get("TV"));
tv.stopTV();
}
}
///......
}
@Override
public void sendMessage() {
}
}
package designPatterns.mediator;
public class TestMain {
public static void main(String[] args) {
//创建一个中介者对象
Mediator concreteMediator = new ConcreteMediator();
//创建Alarm并加入到concreteMediator对象的HashMap
Alarm alarm = new Alarm(concreteMediator,"Alarm");
//创建Coffeemachine对象,加入到HashMap
CoffeeMachine coffeeMachine = new CoffeeMachine(concreteMediator, "CoffeeMachine");
TV tv = new TV(concreteMediator, "TV");
//让闹钟发消息
alarm.sendMessage(0);
}
}
中介者模式的注意事项和细节
1)多个类相互解耦,会形成网状结构,使用中介者模式将网状结构分离为星型结构,进行解耦
2)减少类间依赖,降低了耦合,符合迪米特法则
3)中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
4)如果设计不当,中介者对象本身变得过于复杂,这点在实际使用时,要特别注意