Sunny-117/js-challenges

工厂模式

Pcjmy opened this issue · 1 comments

Pcjmy commented
工厂模式
// 工厂类
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')