1)智能家庭包括各种设备,闹钟、咖啡机、电视机、窗帘等
2)主人要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流程为 :闹铃响起 - 》咖啡机开始做咖啡 -》窗帘自动落下 -》电视机开始播放
1)中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互。
中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。
2)中介者模式属于行为型模式,使代码易于维护。
3)比如MVC模式,C(Controller控制器)是M(Model模型)和V(View视图)的中介者,在前后端交互时起到来中间人的作用。
创建ConcreMediator 对象
创建各个同事类对象,比如 :Alarm、CoffeeMachine、TV
在创建同事类对象的时候,就直接通过构造器,加入到colleagueMap
同事类对象,可以调用sendMessage,最终会去调用ConcreteMediator的getMessage方法
getMessage 会根据接收到的同事对象发出的消息来协调调用其他的同事对象,完成任务
可以看到getMessage 是核心方法,完成相应任务
package com.example.demo.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 com.example.demo.mediator; 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.example.demo.mediator; public class Alarm extends Colleague { public Alarm(Mediator mediator, String name) { super(mediator, name); // TODO Auto-generated constructor stub //在创建 Alarm 同事对象时,将自己放入到 ConcreteMediator 对象中[集合] mediator.Register(name, this); } public void SendAlarm(int stateChange) { SendMessage(stateChange); } @Override public void SendMessage(int stateChange) { // TODO Auto-generated method stub //调用的中介者对象的 getMessage this.GetMediator().GetMessage(stateChange, this.name); } } package com.example.demo.mediator; public class CoffeeMachine extends Colleague { public CoffeeMachine(Mediator mediator, String name) { super(mediator, name); // TODO Auto-generated constructor stub mediator.Register(name, this); } @Override public void SendMessage(int stateChange) { // TODO Auto-generated method stub 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); } } package com.example.demo.mediator; public class Curtains extends Colleague { public Curtains(Mediator mediator, String name) { super(mediator, name); // TODO Auto-generated constructor stub mediator.Register(name, this); } @Override public void SendMessage(int stateChange) { // TODO Auto-generated method stub this.GetMediator().GetMessage(stateChange, this.name); } public void UpCurtains() { System.out.println("I am holding Up Curtains!"); } } package com.example.demo.mediator; public class TV extends Colleague { public TV(Mediator mediator, String name) { super(mediator, name); // TODO Auto-generated constructor stub mediator.Register(name, this); } @Override public void SendMessage(int stateChange) { // TODO Auto-generated method stub this.GetMediator().GetMessage(stateChange, this.name); } public void StartTv() { // TODO Auto-generated method stub System.out.println("It's time to StartTv!"); } public void StopTv() { // TODO Auto-generated method stub System.out.println("StopTv!"); } } package com.example.demo.mediator; import java.util.HashMap; public class ConcreteMediator extends Mediator { //集合,放入所有的同事对象 private HashMap<String, Colleague> colleagueMap; 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); // TODO Auto-generated method stub 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); } } //具体中介者的核心方法 //1. 根据得到消息,完成对应任务 //2. 中介者在这个方法,协调各个具体的同事对象,完成任务 @Override public void GetMessage(int stateChange, String colleagueName) { // TODO Auto-generated method stub //处理闹钟发出的消息 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) { //如果是以窗帘发出的消息,这里处理... } } @Override public void SendMessage() { // TODO Auto-generated method stub } } package com.example.demo.mediator; import java.util.HashMap; public class Client { public static void main(String[] args) { // TODO Auto-generated method stub //创建一个中介者对象 Mediator mediator = new ConcreteMediator(); //创建 Alarm 并且加入到 ConcreteMediator 对象的 HashMap Alarm alarm = new Alarm(mediator, "alarm"); //创建了 CoffeeMachine 对象,并 且加入到 ConcreteMediator 对象的 HashMap CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,"coffeeMachine"); //创建 Curtains , 并 且加入到 ConcreteMediator 对象的 HashMap Curtains curtains = new Curtains(mediator, "curtains"); TV tV = new TV(mediator, "TV"); //让闹钟发出消息 alarm.SendAlarm(0); coffeeMachine.FinishCoffee(); alarm.SendAlarm(1); } }
1)多个类相互耦合,会形成网站结构,使用中介者模式将网状结构分离为星型结构,进行解耦。
2)减少类间依赖,降低类耦合,符合迪米特法则。
3)中介者承担类较多的责任,一旦中介者出现了问题,整个系统就会受到影响。
4)如果设计不当,中介者对象本身变得过于复杂,这点在实际使用时,要特别注意。
联系客服