-
Notifications
You must be signed in to change notification settings - Fork 0
/
builder.go
107 lines (95 loc) · 2.13 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
import "fmt"
/*
Creating complex objects in steps.
This pattern allows you to generate objects of different types and forms using the same creation code.
1. avoid telescopic constructor
2. in steps
3. each step can have several concrete patterns
*/
var (
iceType = "ice"
woodenType = "wood"
)
//Our product
type house struct {
windowType string
doorType string
floor string
}
//Builder builds house
type builder interface {
setWindowType()
setDoorType()
setFloor()
getHouse() house
}
type iglooBuilder struct {
house
}
func (i *iglooBuilder) setWindowType() {
i.windowType = iceType
}
func (i *iglooBuilder) setDoorType() {
i.doorType = iceType
}
func (i *iglooBuilder) setFloor() {
i.floor = iceType
}
func (i *iglooBuilder) getHouse() house {
return i.house
}
type woodBuilder struct {
house
}
func (w *woodBuilder) setWindowType() {
w.windowType = woodenType
}
func (w *woodBuilder) setDoorType() {
w.doorType = woodenType
}
func (w *woodBuilder) setFloor() {
w.floor = woodenType
}
func (w *woodBuilder) getHouse() house {
return w.house
}
//Director uses builder build house and controls steps
type director struct {
builder builder
}
func newDirector(material string) *director {
switch material {
case iceType:
return &director{
builder: &iglooBuilder{},
}
case woodenType:
return &director{
builder: &woodBuilder{},
}
default:
return nil
}
}
func (d *director) setBuilder(b builder) *director {
return &director{builder: b}
}
func (d *director) buildHouse() house {
d.builder.setFloor()
d.builder.setDoorType()
d.builder.setWindowType()
return d.builder.getHouse()
}
func RunBuilder() {
d := newDirector(iceType)
iceHouse := d.buildHouse()
fmt.Printf("Igloo House Door Type: %s\n", iceHouse.doorType)
fmt.Printf("Igloo House Window Type: %s\n", iceHouse.windowType)
fmt.Printf("Igloo House Floor Type: %s\n", iceHouse.floor)
d.setBuilder(&woodBuilder{})
woodHouse := d.buildHouse()
fmt.Printf("Wooden House Door Type: %s\n", woodHouse.doorType)
fmt.Printf("Wooden House Window Type: %s\n", woodHouse.windowType)
fmt.Printf("Wooden House Floor Type: %s\n", woodHouse.floor)
}