js-challenges
js-challenges copied to clipboard
工厂模式
// 工厂类
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')