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

使用中介者模式来说明联合国的作用,要求绘制相应的类图并分析每个类的作用(注:可以将联合国定义为抽象中介者类,联合国下属机构如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);
	}
}

结果:
在这里插入图片描述

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值