您的当前位置:首页正文

设计模式--中介者模式

2024-11-11 来源:个人技术集锦

                     设计模式--中介者模式




中介者模式原理

智慧房屋项目遇到的问题
 

智慧房屋公司的产品:

闹钟、咖啡机、电视机、窗帘等

思考如何设计:

各对象有几种状态改变

相互作用如何

中介者模式原理

中介者模式:用一个中介对象来封装一系列的对象交互。

中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互
 

中介者模式优缺点

优点:

  通过将对象彼此解耦,可以增加对象的复用性

  通过将控制逻辑集中,可以简化系统维护

  可以让对象之间所传递的消息变得简单而且大幅减少

  提高系统的灵活性,使得系统易于扩展和维护

缺点:

  中介者承担了较多的责任,一旦中介者出现了问题,整个系统就会受到影响

  如果设计不当,中介者对象本身变得过于复杂

 

中介者模式示例代码讲解


Mediator

public interface Mediator {
	public abstract void Register(String colleagueName, Colleague colleague);

	public abstract void GetMessage(int stateChange, String colleagueName);

	public abstract void SendMessage();
}


ConcreteMediator 

import java.util.HashMap;

public class ConcreteMediator implements Mediator {
	private HashMap<String, Colleague> colleagueMap;
	private HashMap<String, String> interMap;

	public ConcreteMediator() {
		colleagueMap = new HashMap<String, Colleague>();
		interMap = new HashMap<String, String>();
	}

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

	}

	@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) {

		} else if (colleagueMap.get(colleagueName) instanceof Curtains) {

		}

	}

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

	}

}


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


Alarm 

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

}


CoffeeMachine 

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


Curtains 

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

}



TV 

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


MainTest 

public class MainTest {

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

}



中介者模式关键点

 

中介者模式原理

中介者模式:用一个中介对象来封装一系列的对象交互。

注意事项:

  中介者模式和外观模式

  中介者模式和观察者模式



中介者模式适用场合

适用场合:

  一组对象之间的通信方式比较复杂,导致相互依赖,结构混乱

  一个对象引用很多其他对象并直接与这些对象通信,导致难以复用该对象

显示全文