软件设计模式-中介者模式

使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如WIO,WHO,WTO等作为具体者类,国家作为抽象同事类,而将中国,美国等国家作为具体同事类).

类图:

软件设计模式-中介者模式

代码:

import java.util.Hashtable;

public class Pro {
	public static void main(String[] args) {
		Country china, japan, us;
		china = new China();
		japan = new Japan();
		us = new America();

		Group wto, who, wio;
		wto = new WTO();
		wio = new WIO();
		who = new WHO();

		wto.register(china);
		wto.register(japan);

		who.register(china);
		who.register(us);

		wio.register(china);
		wio.register(japan);
		wio.register(us);

		china.sendTextByWHO(us.getCountryName(), "川普可安好");
		china.sendTextByWHO(japan.getCountryName(), "^_^");
		us.sendTextByWTO(china.getCountryName(), "不好");
		us.sendTextByWIO(china.getCountryName(), "no");
	}
}

class Country {
	protected String countryName;
	private Group wto, who, wio;

	public Country(String name) {
		this.countryName = name;
	}

	public void sendTextByWTO(String to, String msg) {
		if (wto != null) {
			wto.sendText(countryName, to, msg);
		} else {
			System.out.println(countryName + "未加入wto, 不能通过该组织发送消息");
		}
	}

	public void sendTextByWHO(String to, String msg) {
		if (who != null) {
			who.sendText(countryName, to, msg);
		} else {
			System.out.println(countryName + "未加入who, 不能通过该组织发送消息");
		}
	}

	public void sendTextByWIO(String to, String msg) {
		if (wio != null) {
			wio.sendText(countryName, to, msg);
		} else {
			System.out.println(countryName + "未加入wio, 不能通过该组织发送消息");
		}
	}

	public void recieveMessage(String from, String msg) {
		System.out.println(from + "发送消息给" + countryName + ":" + msg);
	}

	public void setWTO(Group wto) {
		this.wto = wto;
	}

	public void setWIO(Group wio) {
		this.wio = wio;
	}

	public void setWHO(Group who) {
		this.who = who;
	}

	public Group getWto() {
		return this.wto;
	}

	public Group getWho() {
		return this.who;
	}

	public Group getWio() {
		return this.wio;
	}

	public String getCountryName() {
		return countryName;
	}
}

class China extends Country {
	public China() {
		super("China");
	}
}

class Japan extends Country {
	public Japan() {
		super("Japan");
	}
}

class America extends Country {
	public America() {
		super("USA");
	}
}

abstract class Group {
	protected String groupName;
	protected Hashtable<String, Country> CountryList = new Hashtable<String, Country>();

	public Group(String name) {
		groupName = name;
	}

	public void register(Country country) {
		if (!CountryList.contains(country)) {
			CountryList.put(country.getCountryName(), country);
		}
	}

	public void sendText(String from, String to, String msg) {
		Country country = CountryList.get(to);
		if (country != null) {
			System.out.println(groupName + "传达" + from + "给" + to + "的消息");
			country.recieveMessage(from, msg);
		} else {
			System.out.println(to + "未加入" + groupName + ", 不能接收来自该组织的消息");
		}
	}
}

class WTO extends Group {
	public WTO() {
		super("WTO");
	}

	@Override
	public void register(Country country) {
		super.register(country);
		country.setWTO(this);
	}
}

class WHO extends Group {
	public WHO() {
		super("WHO");
	}

	@Override
	public void register(Country country) {
		super.register(country);
		country.setWHO(this);
	}
}

class WIO extends Group {
	public WIO() {
		super("WIO");
	}

	@Override
	public void register(Country country) {
		super.register(country);
		country.setWIO(this);
	}
}

结果:
软件设计模式-中介者模式

上一篇:谈笑间 樯橹"灰"飞烟灭


下一篇:制作网页黑白背景