对原理类图的说明-即(中介者模式的角色及职责)
Mediator 就是抽象中介者,定义了同事对象到中介者对象的接口Colleague 是抽象同事类ConcreteMediator 具体的中介者对象,实现抽象方法,他需要所有的具体的同事类,即以一个集合来管理HashMap,并接受某个同事对象的消息,完成相应的任务ConcreteCollegue 具体的同事类,会有很多,每个同事只知道自己的行为,而不了解其他同事类的行为(方法),但是他们都依赖中介者对象。中介者模式:智能家庭的操作流程
创建ConcreMediator 对象创建各个同事类对象,比如:Alarm、CoffeeMachine、Curtains、TV在创建同事类对象的同时,就直接通过构造器,加入到colleagueMap同事类对象,可以调用sendMessage,最终会调用ConcreteMediator的getMessage方法getMessage 会根据接收到的同事对象发出的消息来协调调用完成任务。可以看到getMessage是核心方法,完成相关任务中介者抽象类
public abstract class Mediator { //将一个中介者对象,加入到集合中。 public abstract void register(String colleagureName, Colleague colleague); //接收消息,具体的同事对象发出的消息 public abstract void getMessage(int stateChange, String colleagueName); //发送消息 public abstract void sendMessage(); }具体的中介者实现类,实现具体的操作
//具体的中介者类 public class ConcreteMediator extends Mediator { //集合,放入所有的同事对象 private HashMap<String, Colleague> colleagueMap; private HashMap<String, String> interMap; public ConcreteMediator() { colleagueMap = new HashMap<>(); interMap = new HashMap<>(); } @Override public void register(String colleagureName, Colleague colleague) { colleagueMap.put(colleagureName, colleague); if (colleague instanceof Alarm) { interMap.put("Alarm", colleagureName); } else if (colleague instanceof CoffeeMachine) { interMap.put("CoffeeMachine", colleagureName); } else if (colleague instanceof TV) { interMap.put("TV", colleagureName); } else if (colleague instanceof Curtains) { interMap.put("Curtains", colleagureName); } } //具体中介者的核心方法 //1. 根据得到的消息完成对应的任务。 //2. 中介者在这个方法,协调各个具体的同事对象,完成任务 @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) (colleagueMap.get(interMap.get("TV")))).startTv(); } else if (colleagueMap.get(colleagueName) instanceof Curtains) { ((Curtains) (colleagueMap.get(interMap.get(colleagueName)))).upCurtains(); } } @Override public void sendMessage() { } }同事抽象类,各个同事类通过继承并重写方法来完成与同事抽象类的消息传递
//同事抽象类 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); }通过mediator.register注册同事子类,通过发送消息与同事类进行消息传递
//具体的同事类 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 public void sendMessage(int stateChange) { //调用中介者对象的getMessage this.getMediator().getMessage(stateChange, this.name); } } public class CoffeeMachine extends Colleague { public CoffeeMachine(Mediator mediator, String name) { super(mediator, name); mediator.register(name, this); } public void startCoffee() { System.out.println("CoffeeMachine is making Coffee"); } public void finishedCoffee() { System.out.println("CoffeeMachine maked Coffee"); System.out.println("Coffee is ok!"); sendMessage(0); } @Override public void sendMessage(int stateChange) { this.getMediator().getMessage(stateChange, this.name); } } public 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"); } } public 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("It's time to stopTv"); } }客户端
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.finishedCoffee(); alarm.sendAlarm(1); } }