mirror of
https://github.com/crazybber/go-pattern-examples.git
synced 2024-11-22 11:56:03 +03:00
add content for builders
This commit is contained in:
parent
229509e234
commit
0453d8f5aa
@ -1,10 +1,13 @@
|
|||||||
package builder
|
package builder
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
//ICar 汽车,我们要造车了
|
//ICar 汽车,我们要造车了
|
||||||
//ICar 车具有以下能力
|
//ICar 车具有以下能力
|
||||||
type ICar interface {
|
type ICar interface {
|
||||||
Speed() int
|
Speed() int
|
||||||
Brand() string
|
Brand() string
|
||||||
|
Brief()
|
||||||
}
|
}
|
||||||
|
|
||||||
//ICarBuilder 造一辆车需要具有的部件
|
//ICarBuilder 造一辆车需要具有的部件
|
||||||
@ -34,6 +37,14 @@ func (c *CarProto) Brand() string {
|
|||||||
return c.BrandName
|
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 打算通过成立造车实验室进行造车
|
//CarStudio 打算通过成立造车实验室进行造车
|
||||||
type CarStudio struct {
|
type CarStudio struct {
|
||||||
prototype CarProto
|
prototype CarProto
|
||||||
|
@ -19,3 +19,14 @@ func TestBuilderCar(t *testing.T) {
|
|||||||
fmt.Println(car.Speed())
|
fmt.Println(car.Speed())
|
||||||
fmt.Println(car.Brand())
|
fmt.Println(car.Brand())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestBuilderCarMore(t *testing.T) {
|
||||||
|
builder := NewCarStudio()
|
||||||
|
builder.Brand("land").Speed(110).Engine("bmw")
|
||||||
|
builder.Engine("man made").Brand("panda").Wheel(15)
|
||||||
|
car := builder.Build()
|
||||||
|
|
||||||
|
fmt.Println(car.Speed())
|
||||||
|
fmt.Println(car.Brand())
|
||||||
|
car.Brief()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user