中介者模式(Mediator Pattern)

中介者模式(Mediator Pattern)

定义

又称为调解者模式,或调停者模式。

中介者模式定义了一个中介对象来封装一系列对象之间的交互,使得这些对象不需要直接相互通信,而是通过与中介者进行通信。

通过中介者解耦系统各层次对象的直接耦合,层次对象的对外依赖通信统统交由中介者转发。

属于行为型模式。

特点:

  • 解耦关联对象:通过引入中介者来减少各个相关对象之间的直接依赖关系。
  • 集中控制逻辑:将复杂的交互逻辑集中在一个中介者类中管理和处理。
  • 促进可扩展性:新添加或修改现有相关对象时,只需修改或扩展中介者类而无需改变其他部分。
  • 降低耦合性:各个对象只需要知道中介者对象,而无需了解其他对象的具体细节。

适用场景:

  1. 系统中对象之间存在复杂的引用关系,产生的相互依赖关系结构混乱且难以理解。
  2. 交互的公共行为,如果需要改变行为则可以增加新的中介者类。

标准示例

  • 抽象中介者(Mediator):定义中介者的接口,用于封装对象之间的交互行为。它持有其他具体同事类的引用。
java 复制代码
/**
 * 抽象中介者
 */
public abstract class Mediator {
    protected ConcreteColleagueA colleagueA;
    protected ConcreteColleagueB colleagueB;

    public void setColleagueA(ConcreteColleagueA colleagueA){
        this.colleagueA = colleagueA;
    }

    public void setColleagueB(ConcreteColleagueB colleagueB){
        this.colleagueB = colleagueB;
    }

    //中介者业务方法
    public abstract void transferA();

    public abstract void transferB();

}
  • 具体中介者(ConcreteMediator):实现抽象中介者的接口,通过协调各个同事对象来实现协作。包括了具体的调用方向。
java 复制代码
public class ConcreteMediator extends Mediator{
    public void transferA() {
        //调用B的方法
        colleagueB.methodB();
    }

    public void transferB(){
        //调用A的方法
        colleagueA.methodA();
    }
}
  • 抽象同事类(Colleague):定义与中介者对象进行通信的接口。包含了对抽象中介者的引用,实际中是指向具体中介者。
java 复制代码
/**
 * 抽象同事类
 */
public abstract class Colleague {
    protected Mediator mediator;

    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
}
  • 具体同事类(ConcreteColleague):实现抽象同事类,并持有中介者的引用,通过中介者与其他同事对象进行通信。
java 复制代码
public class ConcreteColleagueA extends Colleague{
    public ConcreteColleagueA(Mediator mediator) {
        super(mediator);
        this.mediator.setColleagueA(this);
    }

    public void methodA(){
        System.out.println(String.format("The method of %s ",this.getClass().getSimpleName()));
    }

    public void dependentMethodA(){
        System.out.println(String.format("The dependent method of %s ",this.getClass().getSimpleName()));
        this.mediator.transferA();
    }

}
java 复制代码
public class ConcreteColleagueB extends Colleague{
    public ConcreteColleagueB(Mediator mediator) {
        super(mediator);
        this.mediator.setColleagueB(this);
    }

    public void methodB(){
        System.out.println(String.format("The method of %s ",this.getClass().getSimpleName()));
    }

    public void dependentMethodB(){
        System.out.println(String.format("The dependent method of %s ",this.getClass().getSimpleName()));
        this.mediator.transferA();
    }
}

客户端代码:

java 复制代码
public class ClientTest {
    public static void main(String[] args) {
        Mediator mediator = new ConcreteMediator();
        ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);
        ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);

        colleagueA.dependentMethodA();
        System.out.println();
        colleagueB.dependentMethodB();
    }
}

执行结果:

shell 复制代码
The dependent method of ConcreteColleagueA 
The method of ConcreteColleagueB 

The dependent method of ConcreteColleagueB 
The method of ConcreteColleagueB 

以上就是 中介者模式的全部内容,感谢阅读。

相关推荐
张天龙3 分钟前
【SpringBoot】Java对象级联校验
java·spring boot
醉颜凉15 分钟前
自旋锁(Spinlock):轻量级锁机制
java·面试·线程··自旋锁·非阻塞锁·检查锁状态
深夜无眠T21 分钟前
JVM详解(个人学习笔记)
java·jvm·笔记·学习
lifelalala24 分钟前
IDEA控制台中文乱码
java·ide·intellij-idea
垒咚一夏30 分钟前
SpringMVC02
java·开发语言
麻辣香蝈蝈30 分钟前
Nacos入门:使用IDEA 从零到一 新建项目连接Nacos(简洁入门)
java·数据库·ide·spring cloud·tomcat·gateway·intellij-idea
战神刘玉栋34 分钟前
《学会 SpringMVC 系列 · 返回值处理器》
java·数据库·redis
负载均衡-sch41 分钟前
Java算法-力扣leetcode-383. 赎金信
java·算法·leetcode
like4551 小时前
设计模式-策略模式
java·开发语言·设计模式·策略模式
摆烂牛杂1 小时前
数据结构与算法--栈
java·开发语言·数据结构·c++·算法