【GOF23设计模式】_中介者模式_同事协作类_内部类实现JAVA245
2016-06-26 17:18
876 查看
来源:http://www.bjsxt.com/
一、S03E245_01【GOF23设计模式】_中介者模式、同事协作类、内部类实现
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/11/f78cedb938bc5296f8c6229f0243436b)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/11/cc890abe618f9d6280ed7dfb25f8f4dc)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/11/b448a7d8fcb1b2707fb4480eaee64381)
![](https://oscdn.geek-share.com/Uploads/Images/Content/202012/11/070c86b7bbb3cc3ebbb706f32438f6ea)
一、S03E245_01【GOF23设计模式】_中介者模式、同事协作类、内部类实现
package com.test.mediator; /** * 同事类的接口 */ public interface Department { void selfAction();//做本部门的事情 void outAction();//向总经理发出申请 }
package com.test.mediator; public class Development implements Department{ private Mediator m;//持有中介者(总经理)的引用 public Development(Mediator m) { super(); this.m = m; m.register("development", this); } @Override public void selfAction() { System.out.println(getClass().getName()+"专心科研,开发项目!"); } @Override public void outAction() { System.out.println(getClass().getName()+"汇报工作!没钱了,需要资金支持!"); } }
package com.test.mediator; public class Finacial implements Department{ private Mediator m;//持有中介者(总经理)的引用 public Finacial(Mediator m) { super(); this.m = m; m.register("finacial", this); } @Override public void selfAction() { System.out.println(getClass().getName()+"数钱!"); } @Override public void outAction() { System.out.println(getClass().getName()+"汇报工作!没钱了,钱太多了!怎么花?"); } }
package com.test.mediator; public class Market implements Department{ private Mediator m;//持有中介者(总经理)的引用 public Market(Mediator m) { super(); this.m = m; m.register("market", this); } @Override public void selfAction() { System.out.println(getClass().getName()+"跑去接项目!"); } @Override public void outAction() { System.out.println(getClass().getName()+"汇报工作!项目承接的进度,需要资金支持!"); m.command("finacial"); } }
package com.test.mediator; public interface Mediator { void register(String dname,Department d); void command(String dname); }
package com.test.mediator; import java.util.HashMap; import java.util.Map; public class President implements Mediator{ private Map<String, Department> map = new HashMap<String, Department>(); @Override public void register(String dname, Department d) { map.put(dname, d); } @Override public void command(String dname) { map.get(dname).selfAction(); } }
package com.test.mediator; public class Client { public static void main(String[] args) { Mediator m = new President(); Market market = new Market(m); Development devp = new Development(m); Finacial f = new Finacial(m); market.selfAction(); market.outAction(); } }
控制台输出: com.test.mediator.Market跑去接项目! com.test.mediator.Market汇报工作!项目承接的进度,需要资金支持! com.test.mediator.Finacial数钱!
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树