-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder.go
58 lines (48 loc) · 1.12 KB
/
builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import "fmt"
type breakfast struct {
coffee bool
eggs bool
bacon bool
extra string
}
type breakfastBuilder struct {
bfast breakfast
}
func NewBreakfastBuilder() *breakfastBuilder {
return &breakfastBuilder{
bfast: breakfast{},
}
}
func (builder *breakfastBuilder) addCoffee() *breakfastBuilder {
fmt.Println("adding coffee to breakfast")
builder.bfast.coffee = true
return builder
}
func (builder *breakfastBuilder) addBacon() *breakfastBuilder {
fmt.Println("adding bacon to breakfast")
builder.bfast.bacon = true
return builder
}
func (builder *breakfastBuilder) addEggs() *breakfastBuilder {
fmt.Println("adding eggs to breakfast")
builder.bfast.eggs = true
return builder
}
func (builder *breakfastBuilder) addExtra(extra string) *breakfastBuilder {
fmt.Println(fmt.Sprintf("adding '%s' as extra to breakfast", extra))
builder.bfast.extra = extra
return builder
}
func (builder *breakfastBuilder) build() breakfast {
fmt.Println("breakfast is ready!")
return builder.bfast
}
func main() {
_ = NewBreakfastBuilder().
addCoffee().
addEggs().
addBacon().
addExtra("bagel").
build()
}