Mediator模式(中介者模式)

2020-12-17  本文已影响0人  涅槃快乐是金

中介者模式(Mediator Pattern)提供了一个中介类,该类通常处理不同类之间的通信,降低多个对象和类之间的通信复杂性,并支持松耦合,使代码易于维护。中介者模式属于行为型模式。

介绍

示例

SmartDevice.ts 智能设备类(抽象同事角色)
import SmartMediator from "./SmartMediator";

export default abstract class SmartDevice {
    //相关设备打开之后 使其进入准备状态
    public abstract readyState(instruction: string): void;
    //操作该设备
    public abstract operateDevice(instruction: string, mediator: SmartMediator): void;
}
CurtainDevice.ts 窗帘设备(具体同事角色)
import SmartDevice from "./SmartDevice";
import SmartMediator from "./SmartMediator";

export default class CurtainDevice extends SmartDevice {

    public operateDevice(instruction: string, mediator: SmartMediator): void {
        console.log(`窗帘已${instruction}`);//通过传入指令,打开或关闭窗帘
        mediator.curtain(instruction);//窗帘通过中介者唤醒音乐设备和洗浴设备
    }

    public readyState(instruction: string): void {
        //如果其他设备开启则调用此方法,唤醒窗帘
        console.log(`窗帘设备准备${instruction}`);
    }

}
MusicDevice.ts 音响设备(具体同事角色)
import SmartDevice from "./SmartDevice";
import SmartMediator from "./SmartMediator";

export default class MusicDevice extends SmartDevice {

    public operateDevice(instruction: string, mediator: SmartMediator): void {
        console.log(`音乐设备已${instruction}`);//通过传入指令,打开或关闭窗帘
        mediator.curtain(instruction);//窗帘通过中介者唤醒音乐设备和洗浴设备
    }

    public readyState(instruction: string): void {
        //如果其他设备开启则调用此方法,唤醒窗帘
        console.log(`音乐设备准备${instruction}`);
    }

}
BathDevice.ts 洗浴设备(具体同事角色)
import SmartDevice from "./SmartDevice";
import SmartMediator from "./SmartMediator";

export default class BathDevice extends SmartDevice {

    public operateDevice(instruction: string, mediator: SmartMediator): void {
        console.log(`洗浴设备${instruction}`);//通过传入指令,打开或关闭窗帘
        mediator.curtain(instruction);//窗帘通过中介者唤醒音乐设备和洗浴设备
    }

    public readyState(instruction: string): void {
        //如果其他设备开启则调用此方法,唤醒窗帘
        console.log(`洗浴设备正在准备${instruction}`);
    }

}
SmartMediator.ts 抽象中介者
import SmartDevice from "./SmartDevice";

export default abstract class SmartMediator {
    //保留所有设备的引用是为了当接收指令时可以唤醒其他设备的操作
    public bd: SmartDevice;
    public md: SmartDevice;
    public cd: SmartDevice;
    constructor(bd: SmartDevice, md: SmartDevice, cd: SmartDevice) {
        this.bd = bd;
        this.md = md;
        this.cd = cd;
    }
    public abstract music(instruction: string): void;
    public abstract curtain(instruction: string): void;
    public abstract bath(instruction: string): void;
}

Shape.ts 具体中介者
import SmartDevice from "./SmartDevice";
import SmartMediator from "./SmartMediator";

export default class ConcreteMediator extends SmartMediator {

    constructor(bd: SmartDevice, cd: SmartDevice, md: SmartDevice) {
        super(bd, cd, md);
    }

    public music(instruction: string): void {//音乐被唤醒后,使其他设备进入准备状态
        this.cd.readyState(instruction);//调用窗帘的准备方法
        this.bd.readyState(instruction);//调用洗浴设备的准备方法
    }

    public curtain(instruction: string): void {
        this.md.readyState(instruction);
        this.bd.readyState(instruction);
    }

    public bath(instruction: string): void {
        this.cd.readyState(instruction);
        this.md.readyState(instruction);
    }

}

Shape.ts 模型接口
export interface Shape {
    draw(): void;
}
index.ts
import SmartDevice from "./SmartDevice";
import BathDevice from "./BathDevice";
import CurtainDevice from "./CurtainDevice";
import MusicDevice from "./MusicDevice";
import ConcreteMediator from "./ConcreteMediator";
import SmartMediator from "./SmartMediator";

const bd: SmartDevice = new BathDevice();
const cd: SmartDevice = new CurtainDevice();
const md: SmartDevice = new MusicDevice();
const sm: SmartMediator = new ConcreteMediator(bd, cd, md);//把设备引用都保存在调停者中
cd.operateDevice("open", sm); //开启窗帘
md.operateDevice("close", sm);//关闭音乐

result
窗帘已open
窗帘设备准备open
洗浴设备正在准备open
音乐设备已close
窗帘设备准备close
洗浴设备正在准备close

类图

类图

角色

上一篇下一篇

猜你喜欢

热点阅读