-
Notifications
You must be signed in to change notification settings - Fork 79
/
builder.go
55 lines (44 loc) · 1.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
package creational
/*
Example of builder pattern:
builder := NewConcreteBuilder()
director := NewDirector(builder)
director.Construct()
product := builder.GetResult()
*/
// Director is the object which orchestrates the building of a product.
type Director struct {
builder Builder
}
// NewDirector creates a new Director with a specified Builder.
func NewDirector(builder Builder) Director {
return Director{builder}
}
// Construct builds the product from a series of steps.
func (d *Director) Construct() {
d.builder.Build()
}
// Builder is an interface for building.
type Builder interface {
Build()
}
// ConcreteBuilder is a builder for building a Product
type ConcreteBuilder struct {
built bool
}
// NewConcreteBuilder returns a new Builder.
func NewConcreteBuilder() ConcreteBuilder {
return ConcreteBuilder{false}
}
// Build builds the product.
func (b *ConcreteBuilder) Build() {
b.built = true
}
// GetResult returns the Product which has been build during the Build step.
func (b *ConcreteBuilder) GetResult() Product {
return Product{b.built}
}
// Product describes the product to be built.
type Product struct {
Built bool
}