设计模式20-中介者模式

1.场景问题解决

1.1 场景描述

智慧房屋公司的产品:
闹钟、咖啡机、电视机、窗帘等

思考如何设计:
各对象有几种状态改变
相互作用如何

1.2 OO设计

1.3 需求变动

1.4 带来问题

2.用设计模式改进

2.1 分析

2.2 重新设计

[外链图片转存失败(img-ik9VrvMn-1568733708520)(https://raw.githubusercontent.com/bobshute/public/master/imgs/csdn/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/20%E4%B8%AD%E4%BB%8B%E8%80%85%E6%A8%A1%E5%BC%8F-2.png)]

2.3 源码

  • vo 该package为同僚相关对象
    • Colleague 同僚,抽象父类
    • Alarm 闹钟 继承Colleague
    • CoffeeMachine 咖啡机 继承Colleague
    • Curtains 窗帘 继承Colleague
    • TV 电视 继承Colleague
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 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("Coffee 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!");
	}
}

  • Mediator 中介者接口
    • ConcreteMediator 中介者实现类
public interface Mediator {
	//注册同僚
	public abstract void Register(String colleagueName, Colleague colleague);

	//得到消息后的中介者需要做的操作
	public abstract void GetMessage(int stateChange, String colleagueName);

	public abstract void SendMessage();
}




public class ConcreteMediator implements 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);
		}

	}

	//得到消息后的中介者需要做的操作
	@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) {
		} else if (colleagueMap.get(colleagueName) instanceof Curtains) {
		}
	}

	@Override
	public void SendMessage() {
	}
}
  • MediatorTest 测试类
public class MediatorTest {
	public static void main(String[] args) {
		Mediator mediator = new ConcreteMediator();

		Alarm mAlarm = new Alarm(mediator, "mAlarm");
		CoffeeMachine mCoffeeMachine = new CoffeeMachine(mediator,"mCoffeeMachine");
		Curtains mCurtains = new Curtains(mediator, "mCurtains");
		TV mTV = new TV(mediator, "mTV");

		mAlarm.SendAlarm(0);
		mCoffeeMachine.FinishCoffee();
		mAlarm.SendAlarm(1);
	}
}

3.设计模式总结

3.1 定义

中介者模式:用一个中介对象来封装一系列的对象交互。
中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互

3.2 分析思路

[外链图片转存失败(img-rUB9oc6p-1568733708521)(https://raw.githubusercontent.com/bobshute/public/master/imgs/csdn/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/20%E4%B8%AD%E4%BB%8B%E8%80%85%E6%A8%A1%E5%BC%8F-1.png)]

3.3 优缺点

  • 优点:
    通过将对象彼此解耦,可以增加对象的复用性
    通过将控制逻辑集中,可以简化系统维护
    可以让对象之间所传递的消息变得简单而且大幅减少
    提高系统的灵活性,使得系统易于扩展和维护
  • 缺点:
    中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响
    如果设计不当,中介者对象本身变得过于复杂

4. 设计模式使用场景及注意

4.1 适用场合:

一组对象之间的通信方式比较复杂,导致相互依赖,结构混乱
一个对象引用很多其他对象并直接与这些对象通信,导致难以复用该对象

5.参考文章

内容总计于HeadFirst设计模式及相关视频

你可能感兴趣的:(总结,架构设计)