/** * @Title: Country.java * @Package com.wangbiao.design.mediator * @Description: TODO * @author wangbiao * @date 2014-9-24 下午12:02:02 * @version V1.0 */ package com.wangbiao.design.mediator; /** * @Title: Country.java * @Package com.wangbiao.design.mediator * @Description: TODO * @author wangbiao * @date 2014-9-24 下午12:02:02 * @version V1.0 */ public abstract class Country { public UnitedNationsMediator mediator; public Country() { // TODO Auto-generated constructor stub } public Country(UnitedNationsMediator mediator) { this.mediator = mediator; } public abstract void declare(String message); public abstract void getMessage(String message); } class China extends Country{ public China(UnitedNationsMediator mediator) { super(mediator); } @Override public void getMessage(String message) { System.out.println("中国收到消息 : "+message); } @Override public void declare(String message) { mediator.declare(message, this); } } class USA extends Country{ public USA(UnitedNationsMediator mediator) { super(mediator); } @Override public void getMessage(String message) { System.out.println("美国收到消息 : "+message); } @Override public void declare(String message) { mediator.declare(message, this); } } package com.wangbiao.design.mediator; /** * * @Title: UnitedNationsMediator.java * @Package com.wangbiao.design.mediator * @Description: TODO * @author wangbiao * @date 2014-9-24 下午01:57:02 * @version V1.0 */ public abstract class UnitedNationsMediator { public abstract void declare(String message,Country country); } class UnitedNationSecurityCouncil extends UnitedNationsMediator{ private China china; private USA usa; public China getChina() { return china; } public void setChina(China china) { this.china = china; } public USA getUsa() { return usa; } public void setUsa(USA usa) { this.usa = usa; } @Override public void declare(String message, Country country) { if (country instanceof China) { usa.getMessage(message); }else { china.getMessage(message); } } } /** * @Title: Client.java * @Package com.wangbiao.design.mediator * @Description: TODO * @author wangbiao * @date 2014-9-24 下午01:56:55 * @version V1.0 */ package com.wangbiao.design.mediator; /** * @Title: Client.java * @Package com.wangbiao.design.mediator * @Description: TODO * @author wangbiao * @date 2014-9-24 下午01:56:55 * @version V1.0 */ public class Client { /** * @param args */ public static void main(String[] args) { UnitedNationSecurityCouncil mediator = new UnitedNationSecurityCouncil(); China country1 = new China(mediator); USA country2 = new USA(mediator); mediator.setChina(country1); mediator.setUsa(country2); country2.declare("为什么你每年的军费这么多?"); country1.declare("管你屁事"); } }
时间: 2024-11-17 18:02:58