共计 4621 个字符,预计需要花费 12 分钟才能阅读完成。
接下里介绍的是 Java 的设计模式之一:中介模式
咱们还是以一个问题进行开展,引入中介模式
有一个智能家居的我的项目,包含各种设施,闹钟、咖啡机、电视机、窗帘等
要求客人回家后看电视时,各个设施能够协同工作,主动实现看电视的筹备工作,比方流程为:闹铃响起 -> 咖啡机开始做咖啡 -> 窗帘主动落下 -> 电视机开始播放
那么你会怎么制作这个我的项目呢?
一、应用传统形式解决问题
传统的形式的问题剖析
当各电器对象有多种状态扭转时,相互之间的调用关系会比较复杂
各个电器对象彼此分割,你中有我,我中有你,不利于松耦合.
各个电器对象之间所传递的音讯(参数),容易凌乱
当零碎减少一个新的电器对象时,或者执行流程扭转时,代码的可维护性、扩展性都不现实,所以思考中介者模式
二、什么是中介者模式
中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互
中介者 使各个对象不须要显式地互相援用,从而使其耦合涣散,而且能够独立地扭转它们之间的交互
中介者模式属于行为型模式,使代码易于保护
在 MVC 模式中,C 是 M 和 V 的中介者,在前后端交互时起到了中间人的作用
Controller 控制器、Model 模型、View 视图
中介者原理类图
Mediator:形象中介者, 定义了共事对象到中介者对象的接口
Colleague:形象共事类
ConcreteMediator:具体的中介者对象, 实现形象办法, 他须要晓得所有的具体的共事类, 即以一个汇合来治理
HashMap:并承受某个共事对象音讯,实现相应的工作
ConcreteColleague:具体的共事类会有很多,每个共事只晓得本人的行为,而不理解其余共事类的行为 (办法)
,然而他们 都依赖中介者对象
三、应用中介者模式解决问题
接下来咱们依据思路创立中介者抽象类、同时抽象类
// 中介者抽象类 | |
abstract class Mediator { | |
// 将共事类注册给中介者对象,退出到汇合中 | |
public abstract void Register(String colleagueName, Colleague colleague); | |
// 接管音讯, 具体的共事对象收回 | |
public abstract void GetMessage(int stateChange, String colleagueName); | |
public abstract void SendMessage();} |
// 共事抽象类 | |
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); | |
} |
举例创立具体的同时类继承共事类接口
class TV extends Colleague {public TV(Mediator mediator, String name) {super(mediator, name); | |
// 结构器的同时,将本人增加进中介者的汇合中 | |
mediator.Register(name, this); | |
} | |
@Override | |
public void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name); | |
} | |
public void StartTv() {System.out.println("It's time to StartTv!"); | |
} | |
public void StopTv() {System.out.println("StopTv!"); | |
} | |
} |
class CoffeeMachine extends Colleague {public CoffeeMachine(Mediator mediator, String name) {super(mediator, name); | |
mediator.Register(name, this); | |
} | |
@Override | |
public void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name); | |
} | |
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 ok!"); | |
SendMessage(0); | |
} | |
} |
class Curtains extends Colleague {public Curtains(Mediator mediator, String name) {super(mediator, name); | |
mediator.Register(name, this); | |
} | |
@Override | |
public void SendMessage(int stateChange) {this.GetMediator().GetMessage(stateChange, this.name); | |
} | |
public void UpCurtains() {System.out.println("I am holding Up Curtains!"); | |
} | |
} |
接下里具体的中介者实现类,继承中介者模式
// 具体的中介者类 | |
class ConcreteMediator extends Mediator { | |
// 汇合,放入所有的共事类对象 | |
private HashMap<String, Colleague> colleagueMap; | |
// 执行程序 Map | |
private HashMap<String, String> interMap; | |
public ConcreteMediator() {colleagueMap = new HashMap<String, Colleague>(); | |
interMap = new HashMap<String, String>();} | |
@Override | |
public void Register(String colleagueName, Colleague colleague) {colleagueMap.put(colleagueName, colleague); | |
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); | |
} | |
} | |
// 具体中介者的外围办法 | |
//1. 依据失去音讯,实现对应工作 | |
//2. 中介者在这个办法,协调各个具体的共事对象,实现工作 | |
@Override | |
public void GetMessage(int stateChange, String colleagueName) { } | |
@Override | |
public void SendMessage() {} | |
} |
接下来让咱们应用 demo,看看是怎么将共事类操作进去的
public static void main(String[] args) { | |
// 创立一个中介者对象 | |
Mediator mediator = new ConcreteMediator(); | |
// 创立 CoffeeMachine 对象,并且退出到 ConcreteMediator 对象的 HashMap | |
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,"coffeeMachine"); | |
// 创立 Curtains 并且退出到 ConcreteMediator 对象的 HashMap | |
Curtains curtains = new Curtains(mediator, "curtains"); | |
// 创立 Tv 对象并且退出到 ConcreteMediator 对象的 HashMap | |
TV tV = new TV(mediator, "TV"); | |
} | |
运行后果如下:coffeeMachine 注册胜利!curtains 注册胜利!TV 注册胜利! |
那么咱们依照想要的流程程序去做呢?
比如说咱们依照流程:关上咖啡机再关上电视机
// 具体的中介者类 | |
class ConcreteMediator extends Mediator { | |
// 省略其余要害信息..... | |
// 具体中介者的外围办法 | |
//1. 依据失去音讯,实现对应工作 | |
//2. 中介者在这个办法,协调各个具体的共事对象,实现工作 | |
@Override | |
public void GetMessage(int stateChange, String colleagueName) { | |
// 解决咖啡收回的音讯 | |
if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {if (stateChange == 0) {((CoffeeMachine) (colleagueMap.get(interMap.get("CoffeeMachine")))).StartCoffee(); | |
((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();} | |
} | |
} | |
} |
这时咱们应用 demo,启动咖啡机看看
public static void main(String[] args) { | |
// 创立一个中介者对象 | |
Mediator mediator = new ConcreteMediator(); | |
// 创立 CoffeeMachine 对象,并且退出到 ConcreteMediator 对象的 HashMap | |
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,"coffeeMachine"); | |
// 创立 Curtains 并且退出到 ConcreteMediator 对象的 HashMap | |
Curtains curtains = new Curtains(mediator, "curtains"); | |
// 创立 Tv 对象并且退出到 ConcreteMediator 对象的 HashMap | |
TV tV = new TV(mediator, "TV"); | |
coffeeMachine.SendMessage(0); | |
} | |
运行后果如下:It's time to startcoffee! | |
It's time to StartTv! |
中介者模式的注意事项和细节
多个类互相耦合,会造成网状结构, 应用中介者模式将网状结构拆散为星型构造,进行解耦
缩小类间依赖,升高了耦合,合乎迪米特准则
中介者承当了较多的责任,一旦中介者呈现了问题,整个零碎就会受到影响
如果设计不当,中介者对象自身变得过于简单,这点在理论应用时,要特地留神
参考资料
尚硅谷:设计模式(韩顺平老师):中介模式
Refactoring.Guru:《深刻设计模式》