【GOF23设计模式】中介者模式

来源:http://www.bjsxt.com/ 
一、【GOF23设计模式】_中介者模式、同事协作类、内部类实现

场景

核心

中介者模式类图

1 package com.test.mediator;
2 /**
3  * 同事类的接口
4  */
5 public interface Department {
6     void selfAction();//做本部门的事情
7     void outAction();//向总经理发出申请
8 }
 1 package com.test.mediator;
 2 
 3 public class Development implements Department{
 4     private Mediator m;//持有中介者(总经理)的引用
 5 
 6     public Development(Mediator m) {
 7         super();
 8         this.m = m;
 9         m.register("development", this);
10     }
11 
12     @Override
13     public void selfAction() {
14         System.out.println(getClass().getName()+"专心科研,开发项目!");
15     }
16 
17     @Override
18     public void outAction() {
19         System.out.println(getClass().getName()+"汇报工作!没钱了,需要资金支持!");
20     }
21 }
 1 package com.test.mediator;
 2 
 3 public class Finacial implements Department{
 4     private Mediator m;//持有中介者(总经理)的引用
 5 
 6     public Finacial(Mediator m) {
 7         super();
 8         this.m = m;
 9         m.register("finacial", this);
10     }
11 
12     @Override
13     public void selfAction() {
14         System.out.println(getClass().getName()+"数钱!");
15     }
16 
17     @Override
18     public void outAction() {
19         System.out.println(getClass().getName()+"汇报工作!没钱了,钱太多了!怎么花?");
20     }
21 }
 1 package com.test.mediator;
 2 
 3 public class Market implements Department{
 4     private Mediator m;//持有中介者(总经理)的引用
 5 
 6     public Market(Mediator m) {
 7         super();
 8         this.m = m;
 9         m.register("market", this);
10     }
11 
12     @Override
13     public void selfAction() {
14         System.out.println(getClass().getName()+"跑去接项目!");
15     }
16 
17     @Override
18     public void outAction() {
19         System.out.println(getClass().getName()+"汇报工作!项目承接的进度,需要资金支持!");
20 
21         m.command("finacial");
22     }
23 }
1 package com.test.mediator;
2 
3 public interface Mediator {
4     void register(String dname,Department d);
5     void command(String dname);
6 }
 1 package com.test.mediator;
 2 
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 
 6 public class President implements Mediator{
 7     private Map<String, Department> map = new HashMap<String, Department>();
 8 
 9     @Override
10     public void register(String dname, Department d) {
11         map.put(dname, d);
12     }
13 
14     @Override
15     public void command(String dname) {
16         map.get(dname).selfAction();
17     }
18 }
 1 package com.test.mediator;
 2 
 3 public class Client {
 4     public static void main(String[] args) {
 5         Mediator m = new President();
 6 
 7         Market market = new Market(m);
 8         Development devp = new Development(m);
 9         Finacial f = new Finacial(m);
10 
11         market.selfAction();
12         market.outAction();
13     }
14 }
控制台输出:
com.test.mediator.Market跑去接项目!
com.test.mediator.Market汇报工作!项目承接的进度,需要资金支持!
com.test.mediator.Finacial数钱!

课堂代码类图

中介者模式的本质

原文地址:https://www.cnblogs.com/erbing/p/5802602.html