go-pattern-examples/creation/06_builder/builder.go

64 lines
1.1 KiB
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
package builder
2020-04-27 07:29:44 +03:00
//ICar 汽车,我们要造车了
//ICar 车具有以下能力
type ICar interface {
Start()
Stop()
Speed() int
Brand() string
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
//ICarBuilder 用来造车
type ICarBuilder interface {
Wheel(wheel int) ICarBuilder
Engine(engine string) ICarBuilder
Speed(max int) ICarBuilder
Brand(brand string) ICarBuilder
Build() ICar
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
//CarProto 车的原型
type CarProto struct {
Wheel int
Engine string
MaxSpeed int
Brand string
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
//CarStudio 打算通过成立造车实验室进行造车
type CarStudio struct {
prototype CarProto
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// NewCarStudio 造车工作室
func NewCarStudio() ICarBuilder {
return &CarStudio{}
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// Wheel of car
func (c *CarStudio) Wheel(wheel int) ICarBuilder {
c.prototype.Wheel = wheel
return c
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// Engine of car
func (c *CarStudio) Engine(engine string) ICarBuilder {
return c
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// Speed of car
func (c *CarStudio) Speed(max int) ICarBuilder {
return c
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// Brand of car
func (c *CarStudio) Brand(brand string) ICarBuilder {
return c
2020-04-21 17:50:21 +03:00
}
2020-04-27 07:29:44 +03:00
// Build return a car
func (c *CarStudio) Build() ICar {
return c.prototype
2020-04-21 17:50:21 +03:00
}