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

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

相关推荐
Yu_Lijing17 小时前
基于C++的《Head First设计模式》笔记——生成器模式
c++·笔记·设计模式
sg_knight18 小时前
设计模式实战:策略模式(Strategy)
java·开发语言·python·设计模式·重构·架构·策略模式
吐个泡泡v19 小时前
Python 开发“设计模式”指南
python·设计模式
一个儒雅随和的男子20 小时前
复杂业务的解决之道,如何使用“中介者模式(Mediator Pattern)”解决复杂业务场景
microsoft·中介者模式
程序员小寒20 小时前
JavaScript设计模式(一):单例模式实现与应用
javascript·单例模式·设计模式
砍光二叉树20 小时前
【设计模式】创建型-原型模式
设计模式·原型模式
helloworddm21 小时前
第一篇:设计模式在 Android 视频播放器中的实战应用
android·设计模式·音视频
砍光二叉树21 小时前
【设计模式】创建型-建造者模式
设计模式·建造者模式
szm02252 天前
设计模式-
设计模式