深入理解设计模式之中介者模式

发布于:2025-05-30 ⋅ 阅读:(20) ⋅ 点赞:(0)

下面是一篇关于设计模式之中介者模式(Mediator Pattern)的详细博客,并附有 Java 实现代码示例。


深入理解设计模式之:中介者模式(Mediator Pattern)

一、什么是中介者模式?

中介者模式(Mediator Pattern)是一种行为型设计模式。它通过引入一个中介对象,来封装一组对象之间的交互,使对象之间不再直接引用彼此,而是通过中介者进行通信,从而降低对象之间的耦合度,便于系统的扩展和维护。

核心思想:
将对象之间复杂的网状关系变为星型结构,所有对象只与中介者交互。


二、应用场景

  • GUI 界面组件之间的交互(如按钮、文本框、下拉框等)
  • 聊天室、消息总线、事件总线
  • 机场调度、交通信号灯等需要统一协调的场景
  • Spring ApplicationContext、事件发布机制

三、模式结构

  • Mediator(抽象中介者):定义同事对象之间通信的接口
  • ConcreteMediator(具体中介者):实现协调各同事对象的通信
  • Colleague(同事类):各参与交互的对象,持有中介者引用

四、UML 类图

+-------------------+
|    Mediator       |<-------------------+
+-------------------+                    |
| +notify()         |                    |
+-------------------+                    |
        /_\                              |
         |                               |
+---------------------+         +---------------------+
| ConcreteMediator    |         |   Colleague         |
+---------------------+         +---------------------+
| +notify()           |         | mediator: Mediator  |
| +register()         |         | +send()             |
+---------------------+         | +receive()          |
                                +---------------------+
                                        /_\
                                         |
                        +----------------+----------------+
                        |                                 |
              +-------------------+             +-------------------+
              | ColleagueA        |             | ColleagueB        |
              +-------------------+             +-------------------+

五、Java 实现代码示例

1. 抽象中介者

public interface Mediator {
    void send(String message, Colleague colleague);
}

2. 抽象同事类

public abstract class Colleague {
    protected Mediator mediator;

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

3. 具体同事类

public class ColleagueA extends Colleague {
    public ColleagueA(Mediator mediator) {
        super(mediator);
    }

    public void send(String message) {
        System.out.println("ColleagueA 发送消息: " + message);
        mediator.send(message, this);
    }

    public void receive(String message) {
        System.out.println("ColleagueA 收到消息: " + message);
    }
}

public class ColleagueB extends Colleague {
    public ColleagueB(Mediator mediator) {
        super(mediator);
    }

    public void send(String message) {
        System.out.println("ColleagueB 发送消息: " + message);
        mediator.send(message, this);
    }

    public void receive(String message) {
        System.out.println("ColleagueB 收到消息: " + message);
    }
}

4. 具体中介者

public class ConcreteMediator implements Mediator {
    private ColleagueA colleagueA;
    private ColleagueB colleagueB;

    public void setColleagueA(ColleagueA colleagueA) {
        this.colleagueA = colleagueA;
    }

    public void setColleagueB(ColleagueB colleagueB) {
        this.colleagueB = colleagueB;
    }

    @Override
    public void send(String message, Colleague colleague) {
        if (colleague == colleagueA) {
            colleagueB.receive(message);
        } else if (colleague == colleagueB) {
            colleagueA.receive(message);
        }
    }
}

5. 客户端调用

public class Main {
    public static void main(String[] args) {
        ConcreteMediator mediator = new ConcreteMediator();
        ColleagueA a = new ColleagueA(mediator);
        ColleagueB b = new ColleagueB(mediator);

        mediator.setColleagueA(a);
        mediator.setColleagueB(b);

        a.send("你好,B!");
        b.send("你好,A!");
    }
}

输出:

ColleagueA 发送消息: 你好,B!
ColleagueB 收到消息: 你好,B!
ColleagueB 发送消息: 你好,A!
ColleagueA 收到消息: 你好,A!

六、优缺点

优点:

  • 降低对象之间的耦合度,便于扩展和维护
  • 集中管理对象间的交互,逻辑清晰
  • 易于增加新同事类和中介者

缺点:

  • 中介者可能变得过于复杂,成为“上帝类”
  • 过多的中介逻辑可能影响性能和可维护性

七、实际应用举例

  • 聊天室中的消息转发
  • GUI 组件事件协调
  • Spring 事件发布机制
  • 业务流程编排引擎

八、总结

中介者模式是解耦对象间复杂交互的有效手段,适合多对象协作、交互复杂的场景。掌握中介者模式有助于编写高内聚、低耦合的系统架构。

建议:

  • 适用于对象间交互复杂、变化频繁的系统
  • 注意中介者的复杂度,避免成为“上帝类”

如需更多设计模式讲解或源码分析,欢迎留言交流!


网站公告

今日签到

点亮在社区的每一天
去签到