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

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

相关推荐
前端大白话2 分钟前
震惊!90%前端工程师都踩过的坑!Vue中`$el`和`$refs`获取DOM元素的10大区别与实战指南
前端·vue.js·设计模式
前端大白话9 分钟前
惊!10行代码实现fetch并发请求!前端工程师必看的错误处理神操作
前端·javascript·设计模式
ApeAssistant21 分钟前
Spring + 设计模式 (十六) 行为型 - 状态模式
spring·设计模式
ApeAssistant23 分钟前
Spring + 设计模式 (十五) 行为型 - 模板方法模式
spring·设计模式
lybugproducer5 小时前
创建型设计模式之:简单工厂模式、工厂方法模式、抽象工厂模式、建造者模式和原型模式
java·设计模式·建造者模式·简单工厂模式·工厂方法模式·抽象工厂模式·面向对象
cooldream200917 小时前
深入理解MVP架构:让UI层与业务逻辑完美分离的设计模式
ui·设计模式·架构·系统架构师
摘星编程20 小时前
并发设计模式实战系列(3):工作队列
设计模式·并发编程
Pasregret20 小时前
访问者模式:分离数据结构与操作的设计模式
数据结构·设计模式·访问者模式
Aniugel1 天前
JavaScript高级面试题
javascript·设计模式·面试
不当菜虚困1 天前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式