-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuilder.go
92 lines (77 loc) · 1.69 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
package builder
import "fmt"
// ICar 汽车,我们要造车了
// ICar 车具有以下能力
type ICar interface {
Speed() int
Brand() string
Brief()
}
// ICarBuilder 造一辆车需要具有的部件
type ICarBuilder interface {
Wheel(wheel int) ICarBuilder
Engine(engine string) ICarBuilder
Speed(max int) ICarBuilder
Brand(brand string) ICarBuilder
Build() ICar
}
// CarProto 车的原型
type CarProto struct {
Wheel int
Engine string
MaxSpeed int
BrandName string
}
// Speed 最大车速
func (c *CarProto) Speed() int {
return c.MaxSpeed
}
// Brand 车品牌
func (c *CarProto) Brand() string {
return c.BrandName
}
// Brief 简介
func (c *CarProto) Brief() {
fmt.Println("this is a cool car")
fmt.Println("car wheel size: ", c.Wheel)
fmt.Println("car MaxSpeed: ", c.MaxSpeed)
fmt.Println("car Engine: ", c.Engine)
}
// CarStudio 打算通过成立造车实验室进行造车
type CarStudio struct {
prototype CarProto
}
// NewCarStudio 造车工作室
func NewCarStudio() ICarBuilder {
return &CarStudio{}
}
// Wheel of car
func (c *CarStudio) Wheel(wheel int) ICarBuilder {
c.prototype.Wheel = wheel
return c
}
// Engine of car
func (c *CarStudio) Engine(engine string) ICarBuilder {
c.prototype.Engine = engine
return c
}
// Speed of car
func (c *CarStudio) Speed(max int) ICarBuilder {
c.prototype.MaxSpeed = max
return c
}
// Brand of car
func (c *CarStudio) Brand(brand string) ICarBuilder {
c.prototype.BrandName = brand
return c
}
// Build return a car
func (c *CarStudio) Build() ICar {
car := &CarProto{
Wheel: c.prototype.Wheel,
Engine: c.prototype.Engine,
MaxSpeed: c.prototype.MaxSpeed,
BrandName: c.prototype.BrandName,
}
return car
}