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

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

相关推荐
颜酱6 小时前
理解编程范式(前端角度)
设计模式
将编程培养成爱好8 小时前
C++ 设计模式《账本事故:当备份被删光那天》
开发语言·c++·设计模式·备忘录模式
FogLetter10 小时前
设计模式奇幻漂流:从单例孤岛到工厂流水线
前端·设计模式
guangzan15 小时前
常用设计模式:代理模式
设计模式
西幻凌云16 小时前
认识设计模式——单例模式
c++·单例模式·设计模式·线程安全·饿汉和懒汉
爱吃烤鸡翅的酸菜鱼17 小时前
【Java】基于策略模式 + 工厂模式多设计模式下:重构租房系统核心之城市房源列表缓存与高性能筛选
java·redis·后端·缓存·设计模式·重构·策略模式
在未来等你1 天前
AI Agent设计模式 Day 5:Reflexion模式:自我反思与持续改进
设计模式·llm·react·ai agent·plan-and-execute
程序员三藏1 天前
快速弄懂POM设计模式
自动化测试·软件测试·python·selenium·测试工具·设计模式·职场和发展
Lei_3359671 天前
[設計模式]設計模式的作用
设计模式
将编程培养成爱好1 天前
C++ 设计模式《统计辅助功能》
开发语言·c++·设计模式·访问者模式