设计模式简述(八)中介者模式

中介者模式

描述

为了简化多个类间复杂的耦合关系,单独定义一个中介者

将边界交互的部分交给中介者,从而简化各个类内部逻辑
个人建议在3个及以上的类间存在复杂交互关系时再考虑中介者,否则可能反而增加系统复杂度

基本使用

  • 定义抽象业务对象(引用中介者)
java 复制代码
public abstract class AbstractColleague {
    protected AbstractMediator mediator;

    public AbstractColleague(AbstractMediator _mediator) {
        this.mediator = _mediator;
    }
}
  • 定义三个具体业务对象
java 复制代码
public class ColleagueA extends AbstractColleague {
    public ColleagueA(AbstractMediator _mediator) {
        super(_mediator);
    }

    /**
     * 外部调a
     * @param a
     */
    public void action(int a) {
        System.out.println("a..." + a);
    }
    
    public void action2() {
        System.out.println("a 内部业务");
        this.invokeMediator(2);
    }

    /**
     * a 调外部
     * @param a
     */
    private void invokeMediator(int a) {
        System.out.println("a外部交互..." + a);
        mediator.doAction("a", "a的业务参数");
    }
}

public class ColleagueB extends AbstractColleague {
    public ColleagueB(AbstractMediator _mediator) {
        super(_mediator);
    }

    /**
     * 外部调b
     * @param b
     */
    public void action(int b) {
        System.out.println("b..." + b);
    }
    public void action2() {
        System.out.println("b 内部业务");
        this.invokeMediator(3);
    }

    /**
     * b 调外部
     * @param b
     */
    private void invokeMediator(int b) {
        System.out.println("invokeMediator..." + b);
        mediator.doAction("b", "b的业务参数");
    }
}


public class ColleagueC extends AbstractColleague {
    public ColleagueC(AbstractMediator _mediator) {
        super(_mediator);
    }

    /**
     * 外部调c
     * @param c
     */
    public void action(int c) {
        System.out.println("c..." + c);
    }

    public void action2() {
        System.out.println("c 内部业务");
        this.invokeMediator(3);
    }
    /**
     * c 调外部
     * @param c
     */
    private void invokeMediator(int c) {
        System.out.println("invokeMediator..." + c);
        mediator.doAction("c", "c的业务参数");
    }
}
  • 定义提抽象中介者
java 复制代码
public abstract class AbstractMediator {
    public abstract void doAction(String command, Object... param);
}
  • 定义具体中介者(中介者要关联所有相关方,代替各方直接调用其他业务方)
java 复制代码
public class MediatorAbc extends AbstractMediator {
    private ColleagueA colleagueA;
    private ColleagueB colleagueB;
    private ColleagueC colleagueC;

    public MediatorAbc() {
        this.colleagueA = new ColleagueA(this);
        this.colleagueB = new ColleagueB(this);
        this.colleagueC = new ColleagueC(this);
    }

    @Override
    public void doAction(String command, Object... param) {
        switch (command) {
            case "a":
                // a -> 调用 b c
                this.colleagueB.action(param.length);
                this.colleagueC.action(param.length);
                break;
            case "b":
                // b -> 调用 a c
                this.colleagueA.action(param.length);
                this.colleagueC.action(param.length);
                break;
            case "c":
                // c -> 调用 a b
                this.colleagueA.action(param.length);
                this.colleagueB.action(param.length);
                break;
            default:
                throw new RuntimeException();
        }
    }
}

使用

其实就是字面意思,将和其他模块交互的部分交给中介完成(由中介去沟通各方 这和现实中的中介如出一辙)

java 复制代码
public class Sample {
    public static void main(String[] args) {
        ColleagueA colleagueA  = new ColleagueA(new MediatorAbc());
        colleagueA.action2();
    }
}
相关推荐
Zane19943 天前
一个 new 就能创建对象,为什么还要拆出单例、工厂、建造者、原型四种模式?
设计模式
怕浪猫3 天前
一行命令复刻爆款视频,我把 Hypit 从安装跑到了出片
人工智能·设计模式·程序员
Zane19944 天前
函数式编程里的函数,其实不是你天天写的那个函数——三大编程范式的边界在哪
设计模式
Zane19945 天前
策略模式现在该不该上?一次讲清楚过度设计和设计不足怎么找平衡
设计模式
她说..5 天前
常见设计模式-模板方法模式
java·spring·设计模式·springboot
xiaofeiyang1505 天前
第六章 · 桥接 — 三支毛笔,画出九种颜色
设计模式
Shadow(⊙o⊙)6 天前
OTOL设计模式 One Thread One Loop
服务器·网络·设计模式
sarasuki6 天前
如何让LLM 能在半夜偷偷打开网易云呢?
人工智能·设计模式·agent
小王师傅666 天前
【设计模式】装饰模式(四):框架源码实战——从 Java I/O 到 Spring 到 MyBatis
java·设计模式
执明wa7 天前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio