Sunny-117 / js-challenges

✨✨✨ Challenge your JavaScript programming limits step by step
https://juejin.cn/column/7244788137410560055
2.03k stars 235 forks source link

工厂模式 #487

Open Pcjmy opened 1 year ago

topulikeweb commented 8 months ago
// 工厂类
class shapeFactory {
  chooseShape (type) {
    if (type === 'circle') {
      return new Circle()
    }
    if (type === 'rectangle'){
      // ...
    }
  }
}

// 圆形类
class Circle {
  draw () {
    console.log('the shape is circle')
  }
}

const factory = new shapeFactory()
factory.chooseShape('circle')