Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

精读《设计模式 - Builder 生成器》 #273

Closed
ascoders opened this issue Oct 7, 2020 · 3 comments
Closed

精读《设计模式 - Builder 生成器》 #273

ascoders opened this issue Oct 7, 2020 · 3 comments

Comments

@ascoders
Copy link
Owner

ascoders commented Oct 7, 2020

设计模式属于能看懂,又很容易忘记的知识,为了方便设计模式应用于日常项目中,精读会增加一些配图解释,希望在读完设计模式后,以后再次翻阅文章时,通过图就可以快速回忆起内容。

上次的抽象工厂我们举了三个例子:汽车工厂、迷宫游戏、事件联动,我觉得这种例子也很利于理解枯燥的设计模式,后面的文章都会用类似的手法书写。


精读《设计模式 - Builder 生成器》

@ositowang
Copy link

挖个坟,最近才又有时间看了,按照示例里的代码的话,那么如果我要再增加一个concreatebuilder, 我们就得同时修改director 和 添加一个新的concreatebuilder嘛,不知道是否我理解有误。
`
class Director {
create(concreteBuilder: ConcreteBuilder) {
// 创建了一些零件
concreteBuilder.buildA();
concreteBuilder.buildB();

// 校验参数已经生成实例
return concreteBuilder.build();

},
createCabin(builder:concreateBuilder){
buildA,
buildB,
buildC,
return builder.build()
}
}

class CabinBuilder {
public buildA() {
// 创建房屋
// this.xxx = xxx
}

public buildB() {
// 刷油漆
}
public buildC(){
// add roof
}

public build() {
// 最终创建实例
return new House(/* ..一堆参数 this.xxx.. */);
}
}
`

@ascoders
Copy link
Owner Author

ascoders commented Mar 9, 2021

新增一个 Builder 一般不用新增 Director:

const director = new Director();
const houseBuilder = new HouseBuilder();
const house = director.create(houseBuilder);
const carBuilder = new CarBuilder();
const car = director.create(carBuilder);

@levidcd
Copy link

levidcd commented Sep 4, 2023

const builder = HouseBuilder() 是不是少了new

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants