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

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

相关推荐
王嘉俊92523 分钟前
设计模式--适配器模式:优雅解决接口不兼容问题
java·设计模式·适配器模式
王嘉俊92524 分钟前
设计模式--组合模式:统一处理树形结构的优雅设计
java·设计模式·组合模式
rongqing201925 分钟前
Google 智能体设计模式:多智能体协作
设计模式
李广坤15 小时前
状态模式(State Pattern)
设计模式
李广坤17 小时前
观察者模式(Observer Pattern)
设计模式
李广坤18 小时前
中介者模式(Mediator Pattern)
设计模式
李广坤18 小时前
迭代器模式(Iterator Pattern)
设计模式
李广坤18 小时前
解释器模式(Interpreter Pattern)
设计模式
阿无,21 小时前
java23种设计模式之前言
设计模式
Asort1 天前
JavaScript设计模式(八):组合模式(Composite)——构建灵活可扩展的树形对象结构
前端·javascript·设计模式