JavaScript 设计模式 (四) 生成器(建造者)模式 Builder pattern

1,569 阅读2分钟

定义

生成器模式是一种创建型设计模式, 使你能够分步骤创建复杂对象。 该模式允许你使用相同的创建代码生成不同类型和形式的对象。

个人理解

可以看做想要用台式机玩游戏,你可以直接买现成的,也可以买好各种零件自己搭配,也可以买了现成的再改装

流程图

JavaScript 设计模式 (四) 生成器(建造者)模式 Builder pattern 流程图

代码示例

// 组装电脑需要的零件
class Motherboard { // 主板
  chipset: string = 'Intel'
}
class RAM { // 内存
  capacity: string = '16G'
}
class CPU { // 处理器
  mainFrequency: string = '3.00GHz'
}
class GraphicsCard { // 显卡
  videoMemory: string = '16G'
}
class Disk { // 磁盘
  type:string = '固态硬盘'
}
class OpticalDrive { // 光驱
  installationMethod: string = '外置'
}
class Monitor { // 显示器
  inch:number = 36
}
class Mouse { // 鼠标
  wayOfWorking: string = '激光'
}
class Keyboard { // 键盘
  buttonTechnology: string = '红轴键盘'
}
class Headset { // 耳机
  productionDate: string = '2020/01/03'
}
class Sound { // 音响
  numberOfChannels: number = 7.2
}
class Camera { // 摄像头
  pixelCamera: string = '2000W'
}

interface computer {
  motherboard?: Motherboard // 主板
  RAM?: RAM // 内存
  CPU?: CPU // 处理器
  graphicsCard?: GraphicsCard // 显卡
  disk?: Disk // 磁盘
  opticalDrive?: OpticalDrive // 光驱
  monitor?: Monitor // 显示器
  mouse?: Mouse// 鼠标
  keyboard?: Keyboard // 键盘
  headset?: Headset // 耳机
  sound?: Sound // 音响
  camera?: Camera // 摄像头
  [propsName: string]: any
}

class Assembly { // 组装电脑
  private static   computer: computer = {}
  public static addMotherboard() { // 主板
    this.computer.motherboard = new Motherboard()
  }
  public static addRAM() { // 内存
    this.computer.RAM = new RAM()
  }
  public static addCPU() { // 处理器
    this.computer.CPU = new CPU()
  }
  public static addgraphicsCard() { // 显卡
    this.computer.graphicsCard = new GraphicsCard()
  }
  public static adddisk() { // 磁盘
    this.computer.disk = new Disk()
  }
  public static addopticalDrive() { // 光驱
    this.computer.opticalDrive = new OpticalDrive()
  }
  public static addmonitor() { // 显示器
    this.computer.monitor = new Monitor()
  }
  public static addmouse() { // 鼠标
    this.computer.mouse = new Mouse()
  }
  public static addkeyboard() { // 键盘
    this.computer.keyboard = new Keyboard()
  }
  public static addheadset() { // 耳机
    this.computer.headset = new Headset()
  }
  public static addsound() { // 音响
    this.computer.sound = new Sound()
  }
  public static addcamera() { // 摄像头
    this.computer.camera = new Camera()
  }
  public static getComputer(): computer { // 获取
    return this.computer
  }
  public static resetComputer() { // 重置
    this.computer = {}
  }
}

class ComputerShop { // 电脑店
  public static getComputer() {
    Assembly.resetComputer() // 新电脑
    Assembly.addMotherboard() // 加主板
    Assembly.addCPU() // 加处理器
    Assembly.addmonitor() // 加显示器
    Assembly.addmouse() // 鼠标
    Assembly.addkeyboard() // 键盘
    return Assembly.getComputer() // 获取
  }
}

// 自己组装
Assembly.resetComputer()
Assembly.addmouse()
Assembly.addkeyboard()
Assembly.addheadset()
Assembly.addsound()
Assembly.addcamera()
let myComputerShop = Assembly.getComputer()
console.log(myComputerShop)
// 直接买
let playMyComputerShop = ComputerShop.getComputer()
console.log(playMyComputerShop)
let DIYComputerShop = ComputerShop.getComputer()
// 买完改装
Assembly.addkeyboard()
Assembly.addheadset()
Assembly.addsound()
Assembly.addgraphicsCard()
Assembly.adddisk()
Assembly.resetComputer()
console.log(DIYComputerShop)

优缺点

  • 你可以分步创建对象, 暂缓创建步骤或递归运行创建步骤。
  • 生成不同形式的产品时, 你可以复用相同的制造代码。
  • 单一职责原则。 你可以将复杂构造代码从产品的业务逻辑中分离出来。
  • 由于该模式需要新增多个类, 因此代码整体复杂程度会有所增加。