js-challenges icon indicating copy to clipboard operation
js-challenges copied to clipboard

工厂模式

Open Pcjmy opened this issue 1 year ago • 1 comments

Pcjmy avatar Mar 12 '23 05:03 Pcjmy

// 工厂类
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')


topulikeweb avatar Mar 30 '24 06:03 topulikeweb