mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-26 21:56:02 +03:00
67 lines
850 B
Go
67 lines
850 B
Go
|
package builder
|
||
|
|
||
|
//Builder 是生成器接口
|
||
|
type Builder interface {
|
||
|
Part1()
|
||
|
Part2()
|
||
|
Part3()
|
||
|
}
|
||
|
|
||
|
type Director struct {
|
||
|
builder Builder
|
||
|
}
|
||
|
|
||
|
// NewDirector ...
|
||
|
func NewDirector(builder Builder) *Director {
|
||
|
return &Director{
|
||
|
builder: builder,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//Construct Product
|
||
|
func (d *Director) Construct() {
|
||
|
d.builder.Part1()
|
||
|
d.builder.Part2()
|
||
|
d.builder.Part3()
|
||
|
}
|
||
|
|
||
|
type Builder1 struct {
|
||
|
result string
|
||
|
}
|
||
|
|
||
|
func (b *Builder1) Part1() {
|
||
|
b.result += "1"
|
||
|
}
|
||
|
|
||
|
func (b *Builder1) Part2() {
|
||
|
b.result += "2"
|
||
|
}
|
||
|
|
||
|
func (b *Builder1) Part3() {
|
||
|
b.result += "3"
|
||
|
}
|
||
|
|
||
|
func (b *Builder1) GetResult() string {
|
||
|
return b.result
|
||
|
}
|
||
|
|
||
|
type Builder2 struct {
|
||
|
result int
|
||
|
}
|
||
|
|
||
|
func (b *Builder2) Part1() {
|
||
|
b.result += 1
|
||
|
}
|
||
|
|
||
|
func (b *Builder2) Part2() {
|
||
|
b.result += 2
|
||
|
}
|
||
|
|
||
|
func (b *Builder2) Part3() {
|
||
|
b.result += 3
|
||
|
}
|
||
|
|
||
|
func (b *Builder2) GetResult() int {
|
||
|
return b.result
|
||
|
}
|