中介者模式
智能家庭项目
传统方案解决智能家庭项目
传统解决方式存在的问题
中介者模式基本介绍
中介者模式原理类图
中介者模式应用实例-智能家庭管理
1)应用实例要求
完成前面的智能家庭项目,使用中介者模式
2)思路分析和图解,使用中介者模式
代码实现
package com.sky.mediator; // 同事抽象类 public abstract class Colleague { private Mediator mediator; protected 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 stateChage); }
package com.sky.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 stateChage){ sendMessage(stateChage); } @Override public void sendMessage(int stateChage) { this.getMediator().getMessage(stateChage,this.name); } }
package com.sky.mediator; // 具体的同事类 咖啡机 public class CoffeeMachine extends Colleague { public CoffeeMachine(Mediator mediator, String name) { super(mediator, name); // 在创建Coffee 同事对象时,将自己放入到ConcreteMediator 对象【集合】中 mediator.register(name,this); } public void startCoffee(){ System.out.println("it is to ready make coffee "); } public void finish(){ System.out.println("after 5 minutes"); System.out.println("coffee is ok "); sendMessage(0); } @Override public void sendMessage(int stateChage) { this.getMediator().getMessage(stateChage,name); } }
package com.sky.mediator; // 具体的同事类 窗帘 public class Curtains extends Colleague{ public Curtains(Mediator mediator, String name) { super(mediator, name); // 在创建Curtains 同事对象时,将自己放入到ConcreteMediator 对象【集合】中 mediator.register(name,this); } public void startCurtains(){ System.out.println("Curtains is already down "); } public void finish(){ System.out.println("coffee is already up "); } @Override public void sendMessage(int stateChage) { this.getMediator().getMessage(stateChage,name); } }
package com.sky.mediator; import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; public class TV extends Colleague { public TV(Mediator mediator, String name) { super(mediator, name); // 像中介者中添加电器 mediator.register(name,this); } public void startTV(){ System.out.println("TV is already started "); } public void finishedTV(){ System.out.println("TV is already closed "); } // 给中介者发送消息 @Override public void sendMessage(int stateChage) { // 中介者接收消息 getMediator().getMessage(stateChage,name); } }
package com.sky.mediator; // 中介者 抽象类 public abstract class Mediator { // 将具体的中介者对象,加入到集合中 public abstract void register(String colleagueName, Colleague college); // 接收消息,具体的同事对象发出 public abstract void getMessage(int stateChange,String colleagueName); // public abstract void sendMessage(); }
package com.sky.mediator; import java.util.HashMap; // 具体的中介者 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 college) { colleagueMap.put(colleagueName,college); if(college instanceof Alarm){ interMap.put("Alarm", colleagueName); }else if(college instanceof CoffeeMachine){ interMap.put("CoffeeMachine",colleagueName); }else if(college instanceof TV){ interMap.put("TV",colleagueName); }else if(college instanceof Curtains){ interMap.put("Curtains",colleagueName); } } /** * 具体中介者的核心方法 * * 1.根据得到的消息,完成对应的任务 * 2.中介者在这个方法,协调各个具体的同事对象,完成任务 * * @param stateChange * @param 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")))).finishedTV(); } }else if(colleagueMap.get(colleagueName) instanceof CoffeeMachine){ // 如果是咖啡机发出的消息,在这里处理 ((Curtains) (colleagueMap.get(interMap.get("Curtains")))).startCurtains(); }else if(colleagueMap.get(colleagueName) instanceof TV){ // 如果是电视机发出的消息,在这里处理 // TODO 待处理 }else if(colleagueMap.get(colleagueName) instanceof Curtains){ // 如果是窗帘发出的消息,在这里处理 // TODO 待处理 } } @Override public void sendMessage() { // TODO 待实现 } }
package com.sky.mediator; // 客户端 public class Client { public static void main(String[] args) { // 创建一个中介者对象 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.finish(); alarm.sendAlarm(1); } }
it is to ready make coffee
TV is already started
after 5 minutes
coffee is ok
Curtains is already down
TV is already closed
中介者模式的注意事项和细节
1)多个类相互耦合,会形成网状结构, 使用中介者模式将网状结构分离为星型结构,进行解耦
2)减少类间依赖,降低了耦合,符合迪米特原则
3)中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
4)如果设计不当,中介者对象本身变得过于复杂,这点在实际使用时,要特别注意