设计模式-中介者模式使用教程

中介者模式(Mediator Pattern)是行为型设计模式的一种,用于减少多个对象或类之间的通信复杂性。这种模式提供了一个中介对象,该中介对象通常封装了一系列对象间的交互关系。中介者使得各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。

以下是在Java中实现中介者模式的步骤:

步骤 1: 创建中介者接口

首先,你需要创建一个中介者接口,定义所有具体中介者共有的方法。

复制代码
1public interface Mediator {
2    void send(String message, Colleague colleague);
3}

步骤 2: 创建具体中介者

接着,实现中介者接口,创建具体中介者类。这个类将协调各个同事类(Colleague)之间的交互。

复制代码
1public class ConcreteMediator implements Mediator {
2    private Colleague1 colleague1;
3    private Colleague2 colleague2;
4
5    public void setColleague1(Colleague1 colleague1) {
6        this.colleague1 = colleague1;
7    }
8
9    public void setColleague2(Colleague2 colleague2) {
10        this.colleague2 = colleague2;
11    }
12
13    @Override
14    public void send(String message, Colleague colleague) {
15        if (colleague == colleague1) {
16            colleague2.notify(message);
17        } else {
18            colleague1.notify(message);
19        }
20    }
21}

步骤 3: 创建同事类

创建抽象同事类,包含指向中介者对象的引用,所有的具体同事类都将继承自这个类。

复制代码
1public abstract class Colleague {
2    protected Mediator mediator;
3
4    public Colleague(Mediator mediator) {
5        this.mediator = mediator;
6    }
7
8    public abstract void send(String message);
9    public abstract void notify(String message);
10}

然后,实现具体的同事类。

复制代码
1public class Colleague1 extends Colleague {
2    public Colleague1(Mediator mediator) {
3        super(mediator);
4    }
5
6    @Override
7    public void send(String message) {
8        mediator.send(message, this);
9    }
10
11    @Override
12    public void notify(String message) {
13        System.out.println("Colleague1 gets message: " + message);
14    }
15}
16
17public class Colleague2 extends Colleague {
18    public Colleague2(Mediator mediator) {
19        super(mediator);
20    }
21
22    @Override
23    public void send(String message) {
24        mediator.send(message, this);
25    }
26
27    @Override
28    public void notify(String message) {
29        System.out.println("Colleague2 gets message: " + message);
30    }
31}

步骤 4: 使用中介者

最后,在客户端代码中,实例化中介者和同事对象,设置它们的关系,并使用中介者来协调它们之间的交云。

复制代码
1public class MediatorPatternDemo {
2    public static void main(String[] args) {
3        ConcreteMediator mediator = new ConcreteMediator();
4
5        Colleague1 colleague1 = new Colleague1(mediator);
6        Colleague2 colleague2 = new Colleague2(mediator);
7
8        mediator.setColleague1(colleague1);
9        mediator.setColleague2(colleague2);
10
11        colleague1.send("Hi there!");
12        colleague2.send("Hello!");
13    }
14}

在本示例中,Colleague1 和 Colleague2 是协作的同事类,它们通过中介者 ConcreteMediator 来进行通信。这样,同事类之间不需要直接通信,而是通过中介者来进行,这降低了类之间的耦合度。

中介者模式在实现复杂的GUI、聊天应用程序或者航空调度等领域中非常有用,其中很多对象需要相互通信,但直接通信又会导致系统的耦合度过高。通过中介者,这些交互可以被集中化管理和协调。

相关推荐
GISer_Jing2 小时前
AI Agent 人类参与HITL与知识检索RAG
人工智能·设计模式·aigc
Tiny_React7 小时前
Claude Code Skills 自优化架构设计
人工智能·设计模式
胖虎111 小时前
iOS中的设计模式(十)- 中介者模式(从播放器场景理解中介者模式)
设计模式·中介者模式·解耦·ios中的设计模式
Geoking.11 小时前
【设计模式】组合模式(Composite)详解
java·设计模式·组合模式
刀法孜然11 小时前
23种设计模式 3 行为型模式 之3.6 mediator 中介者模式
设计模式·中介者模式
Yu_Lijing11 小时前
基于C++的《Head First设计模式》笔记——单件模式
c++·笔记·设计模式
Geoking.11 小时前
【设计模式】外观模式(Facade)详解
java·设计模式·外观模式
点云SLAM12 小时前
C++设计模式之单例模式(Singleton)以及相关面试问题
c++·设计模式·面试·c++11·单例模式(singleton)
GISer_Jing1 天前
AI Agent 目标设定与异常处理
人工智能·设计模式·aigc
蔺太微1 天前
组合模式(Composite Pattern)
设计模式·组合模式