第二节:中介者模式——原理&应用

一、中介者模式基本介绍

  1、基本介绍

    (1)中介者模式(Mediator Pattern),用一个中介对象来封装一系列的对象交互。中介者使各个对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互;

    (2)中介者模式属于行为型代码,使代码易于维护;

    (3)比如 MVC 模式,C(Controller)是 M(Model模型)和 V(View视图)的中介者,在前后端交互时起到了中间人的作用;

  2、中介者模式的原理类图

    第二节:中介者模式——原理&应用

 

   对原理类图的说明——即中介者模式的角色及职责

  (1)Mediator 就是抽象中介者,定义了同事对象到中介者对象的接口;

  (2)Colleague 是抽象同事类;

  (3)ConcreteMediator 具体的中介者对象,实现抽象方法,它需要知道所有的具体的同事类,即以一个集合来管理 HashMap,并接受某个同事对象消息,完成相应的任务;

  (4)ConcreteColleague 具体的同事类,会有很多,每个同事只知道自己的行为,而不了解其他同事类的行为(方法),但他们都依赖中介者对象

 

二、中介者模式应用案例—智能家庭管理

  1、应用实例要求

    完成上一节的智能家庭的项目,使用中介者模式:

  2、思路分析和图解(类图)

    第二节:中介者模式——原理&应用

 

  3、代码实现

    抽象同事类Colleague:

 1 /**
 2  * ͬ同事抽象类
 3  */
 4 public abstract class Colleague {
 5     private Mediator mediator;
 6     public String name;
 7 
 8     public Colleague(Mediator mediator, String name) {
 9         this.mediator = mediator;
10         this.name = name;
11     }
12 
13     public Mediator GetMediator() {
14         return this.mediator;
15     }
16 
17     public abstract void SendMessage(int stateChange);
18 }

 

    具体的同事类:

 1 /**
 2  *闹钟-具体的同事类
 3  */
 4 public class Alarm extends Colleague {
 5 
 6     //构造器
 7     public Alarm(Mediator mediator, String name) {
 8         super(mediator, name);
 9         //在创建 Alarm 同事对象时,将自己放入到 ConcreteMediator 对象中【集合】
10         mediator.Register(name, this);
11     }
12 
13     public void SendAlarm(int stateChange) {
14         SendMessage(stateChange);
15     }
16 
17     @Override
18     public void SendMessage(int stateChange) {
19         //得到中介者,调用中介者的 getMessage()
20         this.GetMediator().GetMessage(stateChange, this.name);
21     }
22 
23 }
24 -------------------------------------------------------------------
25 public class TV extends Colleague {
26 
27     public TV(Mediator mediator, String name) {
28         super(mediator, name);
29         mediator.Register(name, this);
30     }
31 
32     @Override
33     public void SendMessage(int stateChange) {
34         this.GetMediator().GetMessage(stateChange, this.name);
35     }
36 
37     public void StartTv() {
38         System.out.println("It's time to StartTv!");
39     }
40 
41     public void StopTv() {
42         System.out.println("StopTv!");
43     }
44 }
45 -------------------------------------------------------------------
46 public class Curtains extends Colleague {
47 
48     public Curtains(Mediator mediator, String name) {
49         super(mediator, name);
50         mediator.Register(name, this);
51     }
52 
53     @Override
54     public void SendMessage(int stateChange) {
55         this.GetMediator().GetMessage(stateChange, this.name);
56     }
57 
58     public void UpCurtains() {
59         System.out.println("I am holding Up Curtains!");
60     }
61 
62 }
63 -------------------------------------------------------------------
64 public class CoffeeMachine extends Colleague {
65 
66     public CoffeeMachine(Mediator mediator, String name) {
67         super(mediator, name);
68         mediator.Register(name, this);
69     }
70 
71     @Override
72     public void SendMessage(int stateChange) {
73         this.GetMediator().GetMessage(stateChange, this.name);
74     }
75 
76     public void StartCoffee() {
77         System.out.println("It's time to startcoffee!");
78     }
79 
80     public void FinishCoffee() {
81 
82         System.out.println("After 5 minutes!");
83         System.out.println("Coffee is ok!");
84         SendMessage(0);
85     }
86 }

 

    抽象中介者Mediator:

1 public abstract class Mediator {
2     // 将同事类对象,加入到集合中
3     public abstract void Register(String colleagueName, Colleague colleague);
4 
5     //接收消息,具体的 同事 对象发出的消息
6     public abstract void GetMessage(int stateChange, String colleagueName);
7 
8     public abstract void SendMessage();
9 }

 

    具体中介者ConcreteMediator:

 1 /**
 2  * 具体的中介者类
 3  */
 4 public class ConcreteMediator extends Mediator {
 5     //集合,放入所有的同事对象
 6     private HashMap<String, Colleague> colleagueMap;
 7     private HashMap<String, String> interMap;
 8 
 9     public ConcreteMediator() {
10         colleagueMap = new HashMap<String, Colleague>();
11         interMap = new HashMap<String, String>();
12     }
13 
14     @Override
15     public void Register(String colleagueName, Colleague colleague) {
16         colleagueMap.put(colleagueName, colleague);
17 
18 
19         if (colleague instanceof Alarm) {
20             interMap.put("Alarm", colleagueName);
21         } else if (colleague instanceof CoffeeMachine) {
22             interMap.put("CoffeeMachine", colleagueName);
23         } else if (colleague instanceof TV) {
24             interMap.put("TV", colleagueName);
25         } else if (colleague instanceof Curtains) {
26             interMap.put("Curtains", colleagueName);
27         }
28 
29     }
30 
31     //具体中介者的核心方法
32     //1.根据得到消息,完成对应任务
33     //2.中介者在这个方法中,协调各个具体的同事对象,完成任务
34     @Override
35     public void GetMessage(int stateChange, String colleagueName) {
36 
37         if (colleagueMap.get(colleagueName) instanceof Alarm) {
38             if (stateChange == 0) {
39                 ((CoffeeMachine) (colleagueMap.get(interMap
40                         .get("CoffeeMachine")))).StartCoffee();
41                 ((TV) (colleagueMap.get(interMap.get("TV")))).StartTv();
42             } else if (stateChange == 1) {
43                 ((TV) (colleagueMap.get(interMap.get("TV")))).StopTv();
44             }
45 
46         } else if (colleagueMap.get(colleagueName) instanceof CoffeeMachine) {
47             ((Curtains) (colleagueMap.get(interMap.get("Curtains"))))
48                     .UpCurtains();
49 
50         } else if (colleagueMap.get(colleagueName) instanceof TV) {//如果是TV发出消息,这里处理
51 
52         } else if (colleagueMap.get(colleagueName) instanceof Curtains) {
53             //如果是窗帘发出的消息,这里处理
54         }
55 
56     }
57 
58     @Override
59     public void SendMessage() {
60         
61     }
62 }

 

    客户端:

 1 public class ClientTest {
 2 
 3     public static void main(String[] args) {
 4         //创建一个中介者对象
 5         Mediator mediator = new ConcreteMediator();
 6         
 7         //创建 Alarm 并且加入到 concreteMediator 对象的 HashMap 中
 8         Alarm alarm = new Alarm(mediator, "alarm");
 9         
10         //创建 CoffeeMachine 并且加入到  ConcreteMediator 对象的 HashMap
11         CoffeeMachine coffeeMachine = new CoffeeMachine(mediator,
12                 "coffeeMachine");
13         
14         //创建 Curtains , 并且加入到 ConcreteMediator 对象的 HashMap
15         Curtains curtains = new Curtains(mediator, "curtains");
16         TV tV = new TV(mediator, "TV");
17         
18         //让闹钟发出消息
19         alarm.SendAlarm(0);
20         coffeeMachine.FinishCoffee();
21         alarm.SendAlarm(1);
22     }
23 
24 }

 

 

 

 

 

上一篇:设计模式之中介者模式


下一篇:设计模式-行为型10-中介者模式(Mediator)