Sunny-117 / js-challenges

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

工厂模式 #487

Open Pcjmy opened 1 year ago

topulikeweb commented 6 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')