-
Notifications
You must be signed in to change notification settings - Fork 246
/
builder.go
51 lines (42 loc) · 1.14 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
// Package builder is an example of the Builder Pattern.
package builder
// Builder provides a builder interface.
type Builder interface {
MakeHeader(str string)
MakeBody(str string)
MakeFooter(str string)
}
// Director implements a manager
type Director struct {
builder Builder
}
// Construct tells the builder what to do and in what order.
func (d *Director) Construct() {
d.builder.MakeHeader("Header")
d.builder.MakeBody("Body")
d.builder.MakeFooter("Footer")
}
// ConcreteBuilder implements Builder interface.
type ConcreteBuilder struct {
product *Product
}
// MakeHeader builds a header of document..
func (b *ConcreteBuilder) MakeHeader(str string) {
b.product.Content += "<header>" + str + "</header>"
}
// MakeBody builds a body of document.
func (b *ConcreteBuilder) MakeBody(str string) {
b.product.Content += "<article>" + str + "</article>"
}
// MakeFooter builds a footer of document.
func (b *ConcreteBuilder) MakeFooter(str string) {
b.product.Content += "<footer>" + str + "</footer>"
}
// Product implementation.
type Product struct {
Content string
}
// Show returns product.
func (p *Product) Show() string {
return p.Content
}