目录
1.基本介绍
2.动机
3.类图
4.案例
5.总结
假设传统依赖关系如下:
图1:
中介者模式的依赖关系如下:
图2:
智能家庭项目:
传统设计方案:
分析:
当各电器对象有多种状态改变时,相互之间的调用关系会比较复杂
各个电器对象彼此联系,你中有我,我中有你,不利于松耦合.
各个电器对象之间所传递的消息(参数),容易混乱
当系统增加一个新的电器对象时,或者执行流程改变时,代码的可维护性、扩展性都不理想
中介者模式实现:
代码:
package cn.cqu.Mediator;
//抽象同事类
public abstract class Colleague {
private Mediator mediator;
private String name;
public Colleague(Mediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
public Mediator getMediator() {
return mediator;
}
public abstract void sendMessage(int stateChange);
}
package cn.cqu.Mediator;
//具体的同事类
public class Alarm extends Colleague{
public Alarm(Mediator mediator, String name) {
super(mediator, name);
//在创建Alarm对象时,将自己放入到ConcreteMediator对象中
mediator.register(name,this);
}
@Override
public void sendMessage(int stateChange) {
//调用中介者对象的getMessage
this.getMediator().getMessage(stateChange,this.getName());
}
public void sendAlarm(int stateChange){
sendMessage(stateChange);
}
}
package cn.cqu.Mediator;
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.getName());
}
public void StartTv() {
System.out.println("It's time to StartTv!");
}
public void StopTv() {
System.out.println("StopTv!");
}
}
package cn.cqu.Mediator;
public 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.getName());
}
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 cn.cqu.Mediator;
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.getName());
}
public void UpCurtains() {
System.out.println("I am holding Up Curtains!");
}
}
package cn.cqu.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 cn.cqu.Mediator;
import java.util.HashMap;
//具体的中介者类
public class ConcreteMediator extends Mediator {
//集合,放入所有的同事对象
private HashMap colleagueMap;
private HashMap interMap;
public ConcreteMediator() {
colleagueMap = new HashMap();
interMap = new HashMap();
}
@Override
public void register(String colleagueName, Colleague colleague) {
colleagueMap.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);
} else if (colleague instanceof Curtains) {
interMap.put("Curtains", colleagueName);
}
}
//具体中介者的核心方法
//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发现消息
} else if (colleagueMap.get(colleagueName) instanceof Curtains) {
//如果是以窗帘发出的消息,这里处理...
}
}
@Override
public void sendMessage() {
}
}
package cn.cqu.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.FinishCoffee();
alarm.sendAlarm(1);
}
}