中介者模式(Mediator Pattern)

定义

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

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

属于行为型模式。

在这里插入图片描述

特点:

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

适用场景:

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

标准示例

在这里插入图片描述

  • 抽象中介者(Mediator):定义中介者的接口,用于封装对象之间的交互行为。它持有其他具体同事类的引用。
/**
 * 抽象中介者
 */
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):实现抽象中介者的接口,通过协调各个同事对象来实现协作。包括了具体的调用方向。
public class ConcreteMediator extends Mediator{
    public void transferA() {
        //调用B的方法
        colleagueB.methodB();
    }

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

    public Colleague(Mediator mediator){
        this.mediator = mediator;
    }
}
  • 具体同事类(ConcreteColleague):实现抽象同事类,并持有中介者的引用,通过中介者与其他同事对象进行通信。
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();
    }

}
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();
    }
}

客户端代码:

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();
    }
}

执行结果:

The dependent method of ConcreteColleagueA 
The method of ConcreteColleagueB 

The dependent method of ConcreteColleagueB 
The method of ConcreteColleagueB 

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

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部