Skip to content

Commit d99e5bb

Browse files
committed
feat: Builder pattern
1 parent bf4b6bd commit d99e5bb

File tree

8 files changed

+121
-1
lines changed

8 files changed

+121
-1
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
abstract-factory
2+
builder
23
simple-factory
34

45
!abstract-factory/
6+
!builder/
57
!simple-factory/

Makefile

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
abstract-factory:
22
go build -o abstract-factory ./creational/abstract-factory && ./abstract-factory
33

4+
builder:
5+
go build -o builder ./creational/builder && ./builder
6+
47
simple-factory:
58
go build -o simple-factory ./creational/simple-factory && ./simple-factory
69

7-
.PHONY: abstract-factory simple-factory
10+
.PHONY: abstract-factory builder simple-factory

creational/builder/builderer.go

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
type Builderer interface {
4+
setWindowType()
5+
setDoorType()
6+
setFloors()
7+
build() House
8+
}
9+
10+
func getHouseBuilder(builderType string) Builderer {
11+
switch builderType {
12+
case "normal":
13+
return newNormalHouseBuilder()
14+
case "igloo":
15+
return nil
16+
default:
17+
return &Dummy{}
18+
}
19+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
type ConstructionManager struct {
4+
houseBuilder Builderer
5+
}
6+
7+
func newConsturtionManager(b Builderer) *ConstructionManager {
8+
return &ConstructionManager{
9+
houseBuilder: b,
10+
}
11+
}
12+
13+
func (cM ConstructionManager) setHouseBuilder(b Builderer) {
14+
cM.houseBuilder = b
15+
}
16+
17+
func (cM ConstructionManager) buildHouse() House {
18+
cM.houseBuilder.setDoorType()
19+
cM.houseBuilder.setWindowType()
20+
cM.houseBuilder.setFloors()
21+
22+
return cM.houseBuilder.build()
23+
}

creational/builder/dummy.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
type Dummy struct {
4+
house House
5+
}
6+
7+
func (d *Dummy) setWindowType() {
8+
d.house.windowType = ""
9+
}
10+
11+
func (d *Dummy) setDoorType() {
12+
d.house.doorType = ""
13+
}
14+
15+
func (d *Dummy) setFloors() {
16+
d.house.floors = 0
17+
}
18+
19+
func (a *Dummy) build() House {
20+
return House{}
21+
}

creational/builder/house.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
type House struct {
4+
windowType string
5+
doorType string
6+
floors int
7+
}

creational/builder/main.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
normalHouseBuilder := getHouseBuilder("normall")
7+
8+
constructionManager := newConsturtionManager(normalHouseBuilder)
9+
normalHouse := constructionManager.buildHouse()
10+
11+
fmt.Printf("Normal house door type: %s\n", normalHouse.doorType)
12+
fmt.Printf("Normal house window type: %s\n", normalHouse.windowType)
13+
fmt.Printf("Normal house floors: %d\n", normalHouse.floors)
14+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
type NormalHouseBuilder struct {
4+
windowType string
5+
doorType string
6+
floors int
7+
}
8+
9+
func newNormalHouseBuilder() *NormalHouseBuilder {
10+
return &NormalHouseBuilder{}
11+
}
12+
13+
func (b *NormalHouseBuilder) setWindowType() {
14+
b.windowType = "Wooden window"
15+
}
16+
17+
func (b *NormalHouseBuilder) setDoorType() {
18+
b.doorType = "Wooden door"
19+
}
20+
21+
func (b *NormalHouseBuilder) setFloors() {
22+
b.floors = 2
23+
}
24+
25+
func (b *NormalHouseBuilder) build() House {
26+
return House{
27+
doorType: b.doorType,
28+
windowType: b.windowType,
29+
floors: b.floors,
30+
}
31+
}

0 commit comments

Comments
 (0)