JavaScript设计模式「基于ES2024」:创建型模式-抽象工厂模式

60 阅读2分钟

抽象工厂模式是一种更高级的创建型设计模式,它提供了一个接口来创建一系列相关或相互依赖的对象,而无需指定它们的具体类。

// 抽象产品类
class Chair {
    constructor(style) {
        this.style = style;
    }

    sit() {
        console.log(`Sitting on a ${this.style} chair.`);
    }
}

class Table {
    constructor(style) {
        this.style = style;
    }

    use() {
        console.log(`Using a ${this.style} table.`);
    }
}

class Sofa {
    constructor(style) {
        this.style = style;
    }

    relax() {
        console.log(`Relaxing on a ${this.style} sofa.`);
    }
}

// 具体产品类
class ModernChair extends Chair {
    constructor() {
        super('Modern');
    }
}

class ModernTable extends Table {
    constructor() {
        super('Modern');
    }
}

class ModernSofa extends Sofa {
    constructor() {
        super('Modern');
    }
}

class VictorianChair extends Chair {
    constructor() {
        super('Victorian');
    }
}

class VictorianTable extends Table {
    constructor() {
        super('Victorian');
    }
}

class VictorianSofa extends Sofa {
    constructor() {
        super('Victorian');
    }
}

// 抽象工厂类
class FurnitureFactory {
    createChair() {
        throw new Error('Method not implemented.');
    }

    createTable() {
        throw new Error('Method not implemented.');
    }

    createSofa() {
        throw new Error('Method not implemented.');
    }
}

// 具体工厂类
class ModernFurnitureFactory extends FurnitureFactory {
    createChair() {
        return new ModernChair();
    }

    createTable() {
        return new ModernTable();
    }

    createSofa() {
        return new ModernSofa();
    }
}

class VictorianFurnitureFactory extends FurnitureFactory {
    createChair() {
        return new VictorianChair();
    }

    createTable() {
        return new VictorianTable();
    }

    createSofa() {
        return new VictorianSofa();
    }
}

// 家具商店类
class FurnitureStore {
    #factory;

    constructor(factory) {
        this.#factory = factory;
    }

    orderFurnitureSet() {
        const chair = this.#factory.createChair();
        const table = this.#factory.createTable();
        const sofa = this.#factory.createSofa();

        chair.sit();
        table.use();
        sofa.relax();
    }
}

// 使用示例
const modernStore = new FurnitureStore(new ModernFurnitureFactory());
console.log("Ordering a modern furniture set:");
modernStore.orderFurnitureSet();

console.log("\nOrdering a Victorian furniture set:");
const victorianStore = new FurnitureStore(new VictorianFurnitureFactory());
victorianStore.orderFurnitureSet();

实现思路

  1. 抽象产品类 (Chair, Table, Sofa): 定义了每种家具的基本结构和方法。
  2. 具体产品类: ModernChair, ModernTable, ModernSofa, VictorianChair, VictorianTable, VictorianSofa 实现了特定风格的家具。
  3. 抽象工厂类 (FurnitureFactory): 定义了创建各种家具的接口方法。
  4. 具体工厂类: ModernFurnitureFactory, VictorianFurnitureFactory 实现了创建特定风格家具的方法。
  5. 家具商店类 (FurnitureStore): 使用工厂来创建和使用家具。通过构造函数注入方式接收一个具体的工厂,并通过私有字段 (#factory) 存储工厂实例,增强了封装性。

优点

  • 一致性: 确保创建的产品系列相互匹配(例如,所有现代风格或所有维多利亚风格)。
  • 解耦: 客户端代码与具体产品类解耦,只需与抽象接口交互。
  • 可扩展性: 可以轻松添加新的产品系列(如新的家具风格),而不需要修改现有代码。
  • 封装性: 产品的创建逻辑被封装在工厂类中。