二十三种设计模式之中介者模式
来源:互联网 发布:txt阅读器 for mac 编辑:程序博客网 时间:2024/06/07 03:34
中介者模式:用一个对象来封装一系列的对象交互
中介者使各个对象不需要显示的相互引用,从而使其耦合松散,而且可以独立的改变它们之间交互
使用场景 智能房屋
public interface Mediator {
public abstract void Register(String colleagueName,Colleague colleague);
public abstract void GetMessage(int sateChange,String colleagueName);
public abstract void sendMessage();
}
public abstract class Colleague {
private Mediator mediator;
public String name;
public Colleague(Mediator mediator, String name) {
super();
this.mediator = mediator;
this.name = name;
}
public Mediator GetMediator(){
return this.mediator;
}
public abstract void SendMessage(int stateChange);
}
public class Alarm extends Colleague{
public Alarm(Mediator mediator, String name) {
super(mediator, name);
mediator.Register(name, this);
}
public void SendAlarm(int stateChange){
SendMessage(stateChange);
}
@Override
public void SendMessage(int stateChange) {
this.GetMediator().GetMessage(stateChange, this.name);
}
}
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.name);
}
public void startCoffee(){
System.out.println("It's time to startcoffee!");
}
public void FinishCoffee(){
System.out.println("After 5 minutes!");
System.out.println("Coffe is Ok!");
SendMessage(0);
}
}
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("StopTv!");
}
}
public class ConcreteMediator implements Mediator{
private HashMap<String, Colleague> colleagueHashMap;
private HashMap<String, String> interMap;
public ConcreteMediator() {
colleagueHashMap = new HashMap<String, Colleague>();
interMap = new HashMap<String, String>();
}
@Override
public void Register(String colleagueName, Colleague colleague) {
colleagueHashMap.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);
}
}
@Override
public void GetMessage(int sateChange, String colleagueName) {
if(colleagueHashMap.get(colleagueName) instanceof Alarm){
if(sateChange == 0){
((CoffeeMachine)(colleagueHashMap.get(interMap.get("CoffeeMachine")))).startCoffee();
((TV)colleagueHashMap.get(interMap.get("TV"))).StartTV();
}else if(sateChange == 1){
((TV)colleagueHashMap.get(interMap.get("TV"))).StopTV();
}
}else if(colleagueHashMap.get(colleagueName) instanceof CoffeeMachine){
((Curtains)(colleagueHashMap.get(interMap.get("Curtains")))).UpCurtains();
}else if(colleagueHashMap.get(colleagueName) instanceof TV){
}else if(colleagueHashMap.get(colleagueName) instanceof Curtains){
}
}
@Override
public void sendMessage() {
}
}
public class MainTest {
public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
Alarm alarm = new Alarm(mediator, "alarm");
CoffeeMachine coffeeMachine = new CoffeeMachine(mediator, "coffeeMachine");
Curtains curtains = new Curtains(mediator, "curtains");
TV tv = new TV(mediator, "tv");
alarm.SendAlarm(0);
coffeeMachine.FinishCoffee();
alarm.SendAlarm(1);
}
}
- 二十三种设计模式之中介者模式
- 二十三种设计模式之行为型模式之中介者模式
- (八)二十三中设计模式之我见-------------------中介者模式(Meditor Pattern)
- 设计模式(十三)--中介者模式
- 二十三种设计模式之装饰者模式
- 设计模式之二十一:中介者模式(Mediator)
- 大话设计模式二十三章经(三) 中介者模式(Mediator Pattern)
- JAVA设计模式十三--Mediator(中介者模式)
- JAVA设计模式十三:--Mediator(中介者模式)
- 设计模式之中介者
- 设计模式 之 中介者
- 设计模式之中介者
- 二十三 设计模式之桥梁模式
- Java开发中的23种设计模式之二十二:中介者模式(Mediator)
- 二十三种设计模式之创建型模式之建造者模式
- 设计模式之Mediator中介者模式
- 设计模式之中介者模式
- 设计模式之中介者模式
- 数据类型转换之 int 与 LPCTSTR
- 1-html-基础
- hdu 6140 Hybrid Crystals
- 全排序dfs深搜回溯法
- 2017年8月12日提高组T2 YMW的数学题
- 二十三种设计模式之中介者模式
- I2C 小结
- PowerDesigner 导出表结构 到Excel
- 基于TCP的服务器与客户端的搭建
- 前端开发规范
- Navicat通过跳板机连接MySQL(2层跳转)
- HDU 6138 Fleet of the Eternal Throne (后缀数组+字典树, 2017 Multi-Univ Training Contest 8)
- kaldi学习第二天
- PHP入门(第五弹:变量)