JavaScript设计模式「基于ES2024」:结构型模式-外观模式

54 阅读2分钟

外观模式为复杂的子系统提供了一个简化的接口,通过创建一个高层接口来隐藏系统的复杂性,使得客户端代码更容易使用和理解。

// 子系统类:灯光控制
class LightingSystem {
    #lights = new Map();

    addLight(room, state = false) {
        this.#lights.set(room, state);
    }

    turnOn(room) {
        if (this.#lights.has(room)) {
            this.#lights.set(room, true);
            console.log(`Turned on lights in ${room}`);
        }
    }

    turnOff(room) {
        if (this.#lights.has(room)) {
            this.#lights.set(room, false);
            console.log(`Turned off lights in ${room}`);
        }
    }
}

// 子系统类:温度控制
class TemperatureSystem {
    #temperature = 22;  // 默认温度

    setTemperature(temp) {
        this.#temperature = temp;
        console.log(`Set temperature to ${temp}°C`);
    }

    getTemperature() {
        return this.#temperature;
    }
}

// 子系统类:安全系统
class SecuritySystem {
    #isArmed = false;

    arm() {
        this.#isArmed = true;
        console.log("Security system armed");
    }

    disarm() {
        this.#isArmed = false;
        console.log("Security system disarmed");
    }

    isArmed() {
        return this.#isArmed;
    }
}

// 外观类
class SmartHomeFacade {
    #lightingSystem;
    #temperatureSystem;
    #securitySystem;

    constructor() {
        this.#lightingSystem = new LightingSystem();
        this.#temperatureSystem = new TemperatureSystem();
        this.#securitySystem = new SecuritySystem();

        // 初始化一些房间的灯光
        this.#lightingSystem.addLight("Living Room");
        this.#lightingSystem.addLight("Bedroom");
        this.#lightingSystem.addLight("Kitchen");
    }

    leaveHome() {
        this.#lightingSystem.turnOff("Living Room");
        this.#lightingSystem.turnOff("Bedroom");
        this.#lightingSystem.turnOff("Kitchen");
        this.#temperatureSystem.setTemperature(18);  // 设置到节能模式
        this.#securitySystem.arm();
        console.log("Home is set to 'Away' mode");
    }

    arriveHome() {
        this.#securitySystem.disarm();
        this.#lightingSystem.turnOn("Living Room");
        this.#temperatureSystem.setTemperature(22);  // 设置到舒适温度
        console.log("Home is set to 'Welcome' mode");
    }

    nightMode() {
        this.#lightingSystem.turnOff("Living Room");
        this.#lightingSystem.turnOff("Kitchen");
        this.#lightingSystem.turnOn("Bedroom");
        this.#temperatureSystem.setTemperature(20);  // 设置到睡眠温度
        this.#securitySystem.arm();
        console.log("Home is set to 'Night' mode");
    }
}

// 使用示例
function controlSmartHome() {
    const smartHome = new SmartHomeFacade();

    console.log("Leaving home:");
    smartHome.leaveHome();

    console.log("\nArriving home:");
    smartHome.arriveHome();

    console.log("\nSetting to night mode:");
    smartHome.nightMode();
}

controlSmartHome();

实现思路

  1. 子系统类

    • LightingSystem:管理家中不同房间的灯光。
    • TemperatureSystem:控制家中的温度。
    • SecuritySystem:管理家庭安全系统的状态。
  2. 外观类 SmartHomeFacade

    • 包含所有子系统的实例作为私有字段。
    • 提供高层次的方法(如 leaveHome, arriveHome, nightMode),这些方法内部协调多个子系统的操作。
    • 对客户端隐藏子系统的复杂性,提供一个简单的接口。
  3. 使用私有字段(如 #lightingSystem, #temperatureSystem 等)来增强封装性。

  4. 外观类的方法实现了常见的家庭场景,如离家、回家和夜间模式,简化了客户端的操作。

优点

  • 简化接口:客户端只需要与外观类交互,而不需要了解复杂的子系统。
  • 解耦:外观将客户端代码与子系统的组件解耦。
  • 层次化:可以引入多个外观,为系统提供不同层次的抽象。
  • 灵活性:可以轻松地修改或扩展子系统,而不影响客户端代码。