mirror of
https://github.com/tmrts/go-patterns.git
synced 2024-11-22 13:06:09 +03:00
creational/builder: Added a complete example of the builder pattern
This commit is contained in:
parent
f978e42036
commit
d86739c965
@ -13,7 +13,9 @@ however passing a struct to the builder method fills the code with boilerplate
|
|||||||
```go
|
```go
|
||||||
package car
|
package car
|
||||||
|
|
||||||
type Speed float64
|
import "strconv"
|
||||||
|
|
||||||
|
type Speed float32
|
||||||
|
|
||||||
const (
|
const (
|
||||||
MPH Speed = 1
|
MPH Speed = 1
|
||||||
@ -43,8 +45,55 @@ type Builder interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Interface interface {
|
type Interface interface {
|
||||||
Drive() error
|
Drive() string
|
||||||
Stop() error
|
Stop() string
|
||||||
|
}
|
||||||
|
|
||||||
|
type carBuilder struct {
|
||||||
|
speed Speed
|
||||||
|
color Color
|
||||||
|
wheels Wheels
|
||||||
|
}
|
||||||
|
|
||||||
|
type carObject struct {
|
||||||
|
topSpeed Speed
|
||||||
|
color Color
|
||||||
|
wheels Wheels
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBulder() Builder {
|
||||||
|
return &carBuilder{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *carBuilder) TopSpeed(speed Speed) Builder {
|
||||||
|
cb.speed = speed
|
||||||
|
return cb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *carBuilder) Paint(color Color) Builder {
|
||||||
|
cb.color = color
|
||||||
|
return cb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *carBuilder) Wheels(wheels Wheels) Builder {
|
||||||
|
cb.wheels = wheels
|
||||||
|
return cb
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cb *carBuilder) Build() Interface {
|
||||||
|
return &carObject{
|
||||||
|
topSpeed: cb.speed,
|
||||||
|
color: cb.color,
|
||||||
|
wheels: cb.wheels,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *carObject) Drive() string {
|
||||||
|
return "Driving at speed: " + strconv.FormatFloat(float64(c.topSpeed), 'f', 2, 32)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *carObject) Stop() string {
|
||||||
|
return "Stopping a " + string(c.color) + " car"
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -53,9 +102,12 @@ type Interface interface {
|
|||||||
```go
|
```go
|
||||||
assembly := car.NewBuilder().Paint(car.RedColor)
|
assembly := car.NewBuilder().Paint(car.RedColor)
|
||||||
|
|
||||||
familyCar := assembly.Wheels(car.SportsWheels).TopSpeed(50 * car.MPH).Build()
|
familyCar := assembly.Wheels(car.SteelWheels).TopSpeed(50 * car.MPH).Build()
|
||||||
familyCar.Drive()
|
fmt.Println(familyCar.Drive())
|
||||||
|
|
||||||
sportsCar := assembly.Wheels(car.SteelWheels).TopSpeed(150 * car.MPH).Build()
|
sportsCar := assembly.Paint(car.BlueColor).Wheels(car.SportsWheels).TopSpeed(150 * car.MPH).Build()
|
||||||
sportsCar.Drive()
|
fmt.Println(sportsCar.Drive())
|
||||||
|
|
||||||
|
fmt.Println(familyCar.Stop())
|
||||||
|
fmt.Println(sportsCar.Stop()
|
||||||
```
|
```
|
||||||
|
Loading…
Reference in New Issue
Block a user