You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
|
|
|
```go
|
|
|
|
package builder
|
|
|
|
|
|
|
|
type Builder interface {
|
|
|
|
Build()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Director struct {
|
|
|
|
build Builder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDirector(b Builder) Director {
|
|
|
|
return Director{build: b}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Director) Construct() {
|
|
|
|
d.build.Build()
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConcreateBuilder struct {
|
|
|
|
Built bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConcreateBuilder() ConcreateBuilder {
|
|
|
|
return ConcreateBuilder{Built: false}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ConcreateBuilder) Build() {
|
|
|
|
b.Built = true
|
|
|
|
}
|
|
|
|
|
|
|
|
type Product struct {
|
|
|
|
Built bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *ConcreateBuilder) GetResult() Product {
|
|
|
|
return Product{b.Built}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
* 建造者模式
|
|
|
|
|
|
|
|
* 封装接口并写实现方法,这是定义的函数,当参数为接口时,入参可以是任何实现接口的实现,并且调用时和作为参数传入时,均会对数据校验。
|
|
|
|
|
|
|
|
* 在具体接口实现中完成自己想要完成的代码实现,即实现分离。
|