803 B
803 B
nav | group | ||||||||
---|---|---|---|---|---|---|---|---|---|
|
|
建造者模式
// 建造者模式 需要优化
export default class Builder {
constructor() {
this.name = ''
this.author = ''
this.price = 0
this.category = ''
}
withName(name) {
this.name = name
return this
}
withAuthor(author) {
this.author = author
return this
}
withPrice(price) {
this.price = price
return this
}
withCategory(category) {
this.category = category
return this
}
build() {
return {
name: this.name,
author: this.author,
price: this.price,
category: this.category
}
}
}