design_model(14)Mediator

1.中介者模式

允许我们公开一个统一的接口,系统的不同部分可以通过该接口进行通信,而不需要显示的相互作用

2.实例

public interface Mediator {
	
	void register(String dname,Department d);

	void command(String dname);
	
}

public class President implements Mediator {
	
	private Map<String,Department> map = new HashMap<String , Department>();
	
	@Override
	public void command(String dname) {
		map.get(dname).selfAction();
	}

	@Override
	public void register(String dname, Department d) {
		map.put(dname, d);
	}

}

public interface Department {

	void selfAction(); 

	void outAction();  
}

public class Development implements Department {

	private Mediator m;  
	
	public Development(Mediator m) {
		super();
		this.m = m;
		m.register("development", this);
	}

	@Override
	public void outAction() {
		System.out.println("123");
	}

	@Override
	public void selfAction() {
		System.out.println("456");
	}

}

public class Client {
	public static void main(String[] args) {
		Mediator m = new President();		
		Market   market = new Market(m);				
		market.selfAction();
		market.outAction();
		
	}
}
原文地址:https://www.cnblogs.com/gg128/p/9570973.html