首页 > 代码库 > java设计模式_中介者模式
java设计模式_中介者模式
/** * @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("管你屁事"); } }
java设计模式_中介者模式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。