File tree 8 files changed +121
-1
lines changed
8 files changed +121
-1
lines changed Original file line number Diff line number Diff line change 1
1
abstract-factory
2
+ builder
2
3
simple-factory
3
4
4
5
! abstract-factory /
6
+ ! builder /
5
7
! simple-factory /
Original file line number Diff line number Diff line change 1
1
abstract-factory :
2
2
go build -o abstract-factory ./creational/abstract-factory && ./abstract-factory
3
3
4
+ builder :
5
+ go build -o builder ./creational/builder && ./builder
6
+
4
7
simple-factory :
5
8
go build -o simple-factory ./creational/simple-factory && ./simple-factory
6
9
7
- .PHONY : abstract-factory simple-factory
10
+ .PHONY : abstract-factory builder simple-factory
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ type House struct {
4
+ windowType string
5
+ doorType string
6
+ floors int
7
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments