设计模式从入门到放弃(十八)中介者模式

基本概念

中介者模式 Mediator Pattern 属于行为型设计模式使用一个中介对象来封装一系列的对象交互,使得原有对象之间耦合降低,可以独自修改他们他们交互。

UML

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uGOiTmKD-1589428821602)(C:\Users\denglw\AppData\Roaming\Typora\typora-user-images\image-20200513175946963.png)]

角色分析

Mediator 抽象中介者类 核心registry 方法将Colleague 注册到具体的实现类中进行管理,可以添加到集合或者Map中进行管理 getMessage方法接受到不同colleague传入的不同的消息,根据不同的Colleague对象和不同的消息进行具体复杂的业务处理

ConcreteMediator 具体的中介者实现

Colleague 同事对象 用于被中介者管理的对象 核心方法有setMediator 设置中介者该方法中使用Mediator的registry 方法将自己注册到中介者中 sendMessage方法本质调用Mediator的getMessage方法传入this和具体msg 相当于将消息发给了中介者,并告诉中介者是我发出来的。具体的之后的逻辑由中介者的getMessage方法进行处理

ColleagueA ColleageB 具体的Colleague实现类

代码实现

// 中介者接口
public interface Mediator {

    void registry(Colleague colleague);

    void getMessage(Colleague colleague, String msg);

}

// 中介者具体实现
public class ConcreteMediator implements Mediator {
    List<Colleague> colleagueList;

    public ConcreteMediator() {
        this.colleagueList = new ArrayList<>();
    }

    @Override
    public void registry(Colleague colleague) {
        if (!colleagueList.contains(colleague))
            colleagueList.add(colleague);
    }

    @Override
    public void getMessage(Colleague colleague, String msg) {
        System.out.println("接受到" + colleague.getName() + "的信息" + msg);
        System.out.println(" 下面执行处理具体逻辑 xxx");
    }
}

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

    private final String name;

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

    abstract void setMediator(Mediator mediator);

    abstract void sendMsg(String msg);

    public String getName() {
        return name;
    }
}

// 具体Colleague实现
public class ColleagueA extends Colleague {

    private Mediator mediator;

    public ColleagueA(String name) {
        super(name);
    }

    @Override
    void setMediator(Mediator mediator) {
        this.mediator = mediator;
        mediator.registry(this);
    }

    @Override
    void sendMsg(String msg) {
        this.mediator.getMessage(this, msg);
    }
}

public class ColleagueB extends Colleague {

    private Mediator mediator;

    public ColleagueB(String name) {
        super(name);
    }

    @Override
    void setMediator(Mediator mediator) {
        this.mediator = mediator;
        mediator.registry(this);
    }

    @Override
    void sendMsg(String msg) {
        this.mediator.getMessage(this, msg);
    }
}


// 客户端调用
public class MediatorTest {

    public static void main(String[] args) {
        ConcreteMediator concreteMediator = new ConcreteMediator();

        ColleagueA colleagueA = new ColleagueA("ColleagueA");
        colleagueA.setMediator(concreteMediator);
        ColleagueB colleagueB = new ColleagueB("ColleagueB");
        colleagueB.setMediator(concreteMediator);

        colleagueA.sendMsg(" i am A send a msg");
        colleagueB.sendMsg(" i am B send a msg");

    }
}

使用细节

  • 中介者模式将对象构建成星型结构,中介者位于中心,减少类之间的依赖,使得多个类之间的耦合解耦
  • 缺点在于中介者承担了主要的业务逻辑,一旦中介者出现问题,影响整个系统
  • 中介者很容易变得十分复杂难以维护

猜你喜欢

转载自blog.csdn.net/woshiwjma956/article/details/106117131
今日推荐