JAVA设计模式-中介者模式

1、例子

智能家庭项目:
1) 智能家庭包括各种设备,闹钟、咖啡机、电视机、窗帘 等
2) 主人要看电视时,各个设备可以协同工作,自动完成看电视的准备工作,比如流
程为:闹铃响起->咖啡机开始做咖啡->窗帘自动落下->电视机开始播放
传统解决:
流程不清晰
JAVA设计模式-中介者模式_第1张图片
1) 当各电器对象有多种状态改变时,相互之间的调用关系会比较复杂
2) 各个电器对象彼此联系, 你中有我,我中有你,不利于松耦合 .
3) 各个电器对象之间所传递的消息 ( 参数 ) ,容易混乱
4) 当系统增加一个新的电器对象时,或者执行流程改变时,代码的可维护性、扩展性
都不理想 考虑 中介者模

中介者:

JAVA设计模式-中介者模式_第2张图片

2、基本定义

1) 中介者模式( Mediator Pattern ,用一个中介对象来封装一系列的对象交互。
中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立
地改变它们之间的交互
2) 中介者模式属于行为型模式,使代码易于维护
3) 比如 MVC 模式, C Controller 控制器)是 M Model 模型)和 V View 视图)的中
介者,在前后端交互时起到了中间人的作用
3、代码
public class Alarm extends Colleague {


	public Alarm(Mediator mediator, String name) {
		super(mediator, name);
		// TODO Auto-generated constructor stub

		mediator.Register(name, this);
	}

	public void SendAlarm(int stateChange) {
		SendMessage(stateChange);
	}

	@Override
	public void SendMessage(int stateChange) {
		// TODO Auto-generated method stub

		this.GetMediator().GetMessage(stateChange, this.name);
	}

}
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);
    }
}

 

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);
}
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) {
        // TODO Auto-generated method stub
        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

    }
}
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!");
    }

}
public abstract class Mediator {
    //将给中介者对象,加入到集合中
    public abstract void Register(String colleagueName, Colleague colleague);

    //接收消息, 具体的同事对象发出
    public abstract void GetMessage(int stateChange, String colleagueName);

    public abstract void SendMessage();
}

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!");
    }
}

总结:创建由下至上。底层各个获取资源的 各自为类,没有关联。调用逻辑有中介类实现

你可能感兴趣的:(原理,设计模式,中介者模式)