《大话设计模式》java实现之中介者模式
来源:互联网 发布:好玩的rpg游戏 知乎 编辑:程序博客网 时间:2024/05/19 05:02
中介者模式个人理解为中介者持有其他关联对象的引用,把原先各对象之间的复杂关系集合在中介者中去
UML
代码结构
public class Client { public static void main(String[] args) { UnitedNationsSecurityCouncil unsc = new UnitedNationsSecurityCouncil(); USA c1 = new USA(unsc); Iraq c2 = new Iraq(unsc); unsc.setColleague1(c1); unsc.setColleague2(c2); c1.declare("不准研制核武器,否则要发动战争!"); c2.declare("我们没有核武器,也不怕侵略"); }}
public abstract class Country { protected UnitedNations mediator; public Country(UnitedNations mediator) { super(); this.mediator = mediator; }}
public class Iraq extends Country { public Iraq(UnitedNations mediator) { super(mediator); } //声明 public void declare(String message) { mediator.declare(message, this); } //获得消息 public void getMessage(String message) { System.out.println("伊拉克获得对方消息:" + message); }}
public abstract class UnitedNations { public abstract void declare(String message, Country colleague);}
public class UnitedNationsSecurityCouncil extends UnitedNations { private USA colleague1; private Iraq colleague2; public USA getColleague1() { return colleague1; } public void setColleague1(USA colleague1) { this.colleague1 = colleague1; } public Iraq getColleague2() { return colleague2; } public void setColleague2(Iraq colleague2) { this.colleague2 = colleague2; } @Override public void declare(String message, Country colleague) { if(colleague == colleague1) { colleague2.getMessage(message); } else { colleague1.getMessage(message); } }}
public class USA extends Country { public USA(UnitedNations mediator) { super(mediator); } //声明 public void declare(String message) { mediator.declare(message, this); } //获得消息 public void getMessage(String message) { System.out.println("美国获得对方消息:" + message); }}
运行结果
阅读全文
0 0
- 《大话设计模式》java实现之中介者模式
- 大话设计模式之中介者模式
- 大话设计模式之中介者模式
- 大话设计模式--中介者模式 Mediator --C++实现实例
- 大话设计模式读书笔记之中介者模式
- 《大话》之 中介者模式
- 大话设计模式-中介者模式
- 大话设计模式_中介者模式
- 大话设计模式 中介者模式
- 大话设计模式—中介者模式
- 大话设计模式-中介者模式
- 大话设计模式25----中介者模式
- JAVA-设计模式之中介者模式
- java设计模式之中介者模式
- Java 设计模式之中介者模式
- Java 设计模式之中介者模式
- Java 设计模式之中介者模式
- java设计模式之中介者模式
- 【java8新特性】
- userCF算法
- 7.1
- 上传图片,实时预览
- Trafodion Troubleshooting-initialize trafodion Killed
- 《大话设计模式》java实现之中介者模式
- 使用XWFDocument生成word并导出
- 链表实现队列结构
- android studio引入最新espresso3.0.1
- 安卓指纹对称加密及登录功能的实现
- docker交流总结 (一)
- jQuery_1
- C#操作符??和?:
- Cache是什么?