Home 中介者模式
Post
Cancel

中介者模式

Mediator Pattern 对象行为型模式

意图

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

结构

中介者模式

其中:

  • Mediator (中介者) 定义一个接口用于各同事 (Colleague) 对象通信
  • ConcreteMediator (具体中介中介) 通过协调各同事对象实现协作行为;了解并维护它的各个同事
  • Colleague class (同事类) 知道它的中介者对象;每一个同事类对象在需要与其他同事通信的时候与它的中介者通信

适用性

Mediator 模式适用于:

  • 一组对象以定义良好但是复杂的方式进行通信,产生的相互依赖关系结构混乱且难以理解
  • 一个对象引用其他很多对象并且直接与这些对象通信,导致难以复用该对象
  • 想定制一个分布在多个类中的行为,而又不想生成太多的子类

例子 1

在线支付是电子商务的一个重要环节,不同的电子商务平台提供了不同的支付接口。现在需要整合不同电子商务平台的支付接口,使得客户在不同平台上购物时,不需要关心具体的支付接口。拟采用中介者 (Mediator) 设计模式来实现该需求,类图如下所示

中介者模式-例子

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
interface WebServiceMediator {
    public void buy(double money, WebService service);
    public void SetAmazon(WebService amazon);
    public void SetEbay(WebService ebay);
}

abstract class WebService {
    protected WebServiceMediator mediator;
    public abstract void SetMediator(WebServiceMediator mediator);
    public abstract void buyService(double money);
    public abstract void search(double money);
}

class ConcreteServiceMediator implements WebServiceMediator {
    private WebService amazon;
    private WebService ebay;
    
    public ConcreteServiceMediator() {
        amazon = null;
        ebay = null;
    }
    
    public void SetAmazon(WebService amazon) {
        this.amazon = amazon;
    }
    
    public void SetEbay(WebService ebay) {
        this.ebay = ebay;
    }
    
    public void buy(double money, WebService service) {
        if (service == amazon)
            amazon.search(money);
        else
            ebay.search(money);
    }
}

class Amazon extends WebService {
    public void SetMediator(WebServiceMediator mediator) {
        this.mediator = mediator;
    }
    
    public void buyService(double money) {
        mediator.buy(money, this);
    }
    
    public void search(double money) {
        System.out.println("Amazon receive:" + money);
    }
}

class Ebay extends WebService {
    public void SetMediator(WebServiceMediator mediator) {
        this.mediator = mediator;
    }
    
    public void buyService(double money) {
        mediator.buy(money, this);
    }
    
    public void search(double money) {
        System.out.println("Ebay receive:" + money);
    }
}

例子 2

两个同事之间通信

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
public class MediatorPattern {
    public static void main(String[] args) {
        ConcreteMediator m = new ConcreteMediator();
        Colleague1 c1 = new Colleague1(m);
        Colleague2 c2 = new Colleague2(m);

        m.setC1(c1);
        m.setC2(c2);

        c1.sendMessage("hello");
        c2.sendMessage("hi");
    }
}

abstract class Colleague{
    protected Mediator mediator;
}

class Colleague1 extends Colleague{
    public Colleague1(Mediator mediator){
        this.mediator = mediator;
    }

    public void sendMessage(String message){
        mediator.sendMessage(message, this);
    }

    public void Notify(String message){
        System.out.println("Colleague1 received: " + message);
    }
}

class Colleague2 extends Colleague{
    public Colleague2(Mediator mediator){
        this.mediator = mediator;
    }

    public void sendMessage(String message){
        mediator.sendMessage(message, this);
    }

    public void Notify(String message){
        System.out.println("Colleague2 received: " + message);
    }
}

abstract class Mediator{
    public abstract void sendMessage(String message, Colleague c);
}

class ConcreteMediator extends Mediator{
    // 因为此例对象很少,所以直接定义
    private Colleague1 c1;
    private Colleague2 c2;
    // 如果同事很多
    // List<Colleague> list = new ArrayList<>();
    // public void Add(Colleague c){
    //     list.add(c);
    // }

    public void setC1(Colleague1 c1){
        this.c1 = c1;
    }
    public void setC2(Colleague2 c2){
        this.c2 = c2;
    }

    @Override
    public void sendMessage(String message, Colleague c){
        if(c == c1){
            // 让同事2收到消息
            c2.Notify(message);
        }else{
            // 让同事1收到消息
            c1.Notify(message);
        }
    }
}
This post is licensed under CC BY 4.0 by the author.

迭代器模式

Linux 学习 Index