Java设计模式——行为模式:中介者模式(调停者模式)

模式动机

在面向对象的软件设计与开发中,根据“单一职责原则”,我们应尽量将对象细化,使其值负责或呈现单一职责。对于一个模块,可能由很多对象构成,而且这些对象之间可能存在相互引用,为了减少对象两两之间的复杂引用关系,使之成为一个松耦合系统,这就是中介者模式的模式动机。

模式定义

用一个中介对象来封装一系列的对象交互中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互。中介者模式又称为调停者模式,它是一种对象行为型模式。

模式结构

中介者模式包含如下角色:

  • Mediator: 抽象中介者
  • ConcreteMediator: 具体中介者
  • Colleague: 抽象同事类
  • ConcreteColleague: 具体同事类

在这里插入图片描述

时序图

在这里插入图片描述

代码实现

//抽象中介类
public abstract class Mediator {
    
    
    public abstract void concat(String content, Colleague colleague);
}

//抽象同事类
public abstract class Colleague {
    
    

    protected String name;
    protected Mediator mediator;

    public Colleague(String name,Mediator mediator){
    
    
        this.mediator=mediator;
        this.name=name;
    }
}

//具体同事类
public class ColleagueA extends Colleague{
    
    
    public ColleagueA(String name,Mediator mediator){
    
    
        super(name,mediator);
    }

    public void getMessage(String message){
    
    
        System.out.println("同事A"+name+"获得信息"+message);
    }

    public void contact(String message){
    
    
        mediator.concat(message, this);
    }
}

public class ColleagueB extends Colleague{
    
    
    public ColleagueB(String name,Mediator mediator){
    
    
        super(name,mediator);
    }

    public void getMessage(String message){
    
    
        System.out.println("同事B"+name+"获得信息"+message);
    }

    public void contact(String message){
    
    
        mediator.concat(message, this);
    }
}

//具体中介类
public class ConcreteMediator extends Mediator{
    
    

    ColleagueA collA;
    ColleagueB collB;

    public ColleagueA getCollA() {
    
    
        return collA;
    }

    public void setCollA(ColleagueA collA) {
    
    
        this.collA = collA;
    }

    public ColleagueB getCollB() {
    
    
        return collB;
    }

    public void setCollB(ColleagueB collB) {
    
    
        this.collB = collB;
    }

    @Override
    public void concat(String content, Colleague colleague) {
    
    
        if(colleague==collA){
    
    
            collB.getMessage(content);
        }else{
    
    
            collA.getMessage(content);
        }
    }
}

//客户类
public class Client {
    
    
    public static void main(String[] args) {
    
    
        // 定义中介者
        ConcreteMediator mediator = new ConcreteMediator();
        // 定义具体同事类
        ColleagueA colleagueA = new ColleagueA("张三", mediator);
        ColleagueB colleagueB = new ColleagueB("李四", mediator);
        // 中介者知晓每一个具体的Colleague类
        mediator.setCollA(colleagueA);
        mediator.setCollB(colleagueB);
        colleagueA.contact("我是A,我要和同事B说说工作的事情");
        colleagueB.contact("我是B,我下午有时间,下午商量吧");
    }
}

模式分析

中介者模式可以使对象之间的关系数量急剧减少。
中介者承担两方面的职责:

  • 中转作用(结构性):过中介者提供的中转作用,各个同事对象就不再需要显式引用其他同事,当需要和其他同事进行通信时,通过中介者即可。该中转作用属于中介者在结构上的支持
  • 协调作用(行为性):中介者可以更进一步的对同事之间的关系进行封装,同事可以一致地和中介者进行交互,而不需要指明中介者需要具体怎么做,中介者根据封装在自身内部的协调逻辑,对同事的请求进行进一步处理,将同事成员之间的关系行为进行分离和封装。该协调作用属于中介者在行为上的支持。

模式优缺点

优点

  • 简化了对象之间的交互。
  • 将各同事解耦。
  • 减少子类生成。
  • 可以简化各同事类的设计和实现。

缺点

  • 在具体中介者类中包含了同事之间的交互细节,可能会导致具体中介者类非常复杂,使得系统难以维护。

猜你喜欢

转载自blog.csdn.net/qq_35531985/article/details/109062786