设计模式系列:中介者模式

概念

定义一个中介者对象, 封装一系列对象的交互关系, 使得各对象不必显示的相互引用,
从而使其耦合松散, 而且可以独立的改变它们的交互.

实现

以智能家居为例,小爱控制只能家居。

类图

图片

代码

  • SmartDevice 智能设备抽象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName SmartDevice
* @Author littlehui
* @Date 2020/4/9 18:12
* @Version 1.0
**/
public abstract class SmartDevice {

public abstract void operateDevice(String instruction, SmartMediator mediator);

public abstract void readyState(String instruction);

}
  • SmartMediator 中介者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName SmartMediator
* @Author littlehui
* @Date 2020/4/9 18:12
* @Version 1.0
**/
public abstract class SmartMediator {
SmartDevice bd;
SmartDevice md;
SmartDevice cd;
public SmartMediator(SmartDevice bd, SmartDevice md, SmartDevice cd) {
super();
this.bd = bd;
this.md = md;
this.cd = cd;
}

public abstract void music(String instruction);
public abstract void curtain(String instruction);
public abstract void bath(String instruction);
}
  • MusicDevice 智能播放器

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package com.littlehui.design.mediator;

    /**
    * @Description TODO
    * @ClassName MusicDevice
    * @Author littlehui
    * @Date 2020/4/9 18:15
    * @Version 1.0
    **/
    public class MusicDevice extends SmartDevice {

    @Override
    public void operateDevice(String instruction,SmartMediator mediator) {
    System.out.println("音乐设备"+instruction);
    mediator.music(instruction);
    }

    @Override
    public void readyState(String instruction) {
    System.out.println("音乐设备准备"+instruction);
    }
    }
  • CurtainDevice 智能窗帘

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName CurtainDevice
* @Author littlehui
* @Date 2020/4/9 18:14
* @Version 1.0
**/
public class CurtainDevice extends SmartDevice {

@Override
public void operateDevice(String instruction, SmartMediator mediator) {
System.out.println("窗帘已"+instruction);
mediator.curtain(instruction);
}

@Override
public void readyState(String instruction) {
System.out.println("窗帘设备准备"+instruction);
}
}
  • BathDevice 洗浴室
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName BathDevice
* @Author littlehui
* @Date 2020/4/9 18:16
* @Version 1.0
**/
public class BathDevice extends SmartDevice {


@Override
public void operateDevice(String instruction, SmartMediator mediator) {
System.out.println("洗浴设备"+instruction);
mediator.bath(instruction);
}

@Override
public void readyState(String instruction) {
System.out.println("洗浴设备正在准备"+instruction);
}

}
  • Xiaoai 具体中介者 小爱
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName Xiaoai
* @Author littlehui
* @Date 2020/4/9 18:13
* @Version 1.0
**/
public class Xiaoai extends SmartMediator {

public Xiaoai(SmartDevice bd, SmartDevice md, SmartDevice cd) {
super(bd, md, cd);
}

@Override
public void music(String instruction) {
System.out.println("小爱操作音乐音乐");
cd.readyState(instruction);
bd.readyState(instruction);
}

@Override
public void curtain(String instruction) {
System.out.println("小爱操作窗帘");
md.readyState(instruction);
bd.readyState(instruction);
}

@Override
public void bath(String instruction) {
System.out.println("小爱操作浴室");
cd.readyState(instruction);
md.readyState(instruction);
}
}
  • Client 客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.littlehui.design.mediator;

/**
* @Description TODO
* @ClassName Client
* @Author littlehui
* @Date 2020/4/9 18:18
* @Version 1.0
**/
public class Client {

public static void main(String[] args) {
SmartDevice bd = new BathDevice();
SmartDevice cd = new CurtainDevice();
SmartDevice md = new MusicDevice();
SmartMediator sm = new Xiaoai(bd, cd, md);
cd.operateDevice("open",sm);
md.operateDevice("close",sm);
}
}
  • 执行结果
1
2
3
4
5
6
7
8
9
10
Connected to the target VM, address: '127.0.0.1:60536', transport: 'socket'
窗帘已open
小爱操作窗帘
窗帘设备准备open
洗浴设备正在准备open
音乐设备已close
小爱操作音乐音乐
音乐设备准备close
洗浴设备正在准备close
Disconnected from the target VM, address: '127.0.0.1:60536', transport: 'socket'

场景

当有多个对象彼此间相互交互的时候,自然就会想到对象间的耦合度过高,解决办法就是封装对象间的交互行为,因此就能想到中介者模式就是干这行的。

总结

中介者的核心是抽离依赖关系。通过依赖关系来描述整个业务场景。使得复杂的逻辑可以内聚。

  • 中介者模式优点
    1. 通过让对象彼此解耦,增加对象的复用性
    2. 通过将控制逻辑集中,可以简化系统维护
      通过中介者使一对所变成了一堆一,便于理解
  • 缺点
    1. 如果设计不好,引入中介者会使程序变的复杂
    2. 中介者承担过多责任,维护不好会出大事