1
0
Fork 0
mirror of https://github.com/tmrts/go-patterns.git synced 2025-04-06 06:33:31 +03:00

updated builder pattern example for code completeness.

This commit is contained in:
Ryan Hartje 2018-10-06 12:41:43 -04:00
parent f978e42036
commit f600fa3429
No known key found for this signature in database
GPG key ID: D46C0B25CBEB3B1B

View file

@ -13,38 +13,80 @@ however passing a struct to the builder method fills the code with boilerplate
```go
package car
import (
"fmt"
)
type Speed float64
const (
MPH Speed = 1
KPH = 1.60934
)
type Color string
const (
BlueColor Color = "blue"
GreenColor = "green"
RedColor = "red"
)
type Wheels string
const (
SportsWheels Wheels = "sports"
SteelWheels = "steel"
MPH Speed = 1
KPH Speed = 1.60934
SportsWheels Wheels = "sports"
SteelWheels Wheels = "steel"
BlueColor Color = "blue"
GreenColor Color = "green"
RedColor Color = "red"
)
type Builder interface {
Color(Color) Builder
Wheels(Wheels) Builder
TopSpeed(Speed) Builder
Build() Interface
Color(Color) Builder
Wheels(Wheels) Builder
TopSpeed(Speed) Builder
Build() Interface
}
type Interface interface {
Drive() error
Stop() error
Drive() error
Stop() error
}
type carBuilder struct {
speedOption Speed
color Color
wheels Wheels
}
func (c *car) Drive() error {
fmt.Println("VROOM")
return nil
}
func (c *car) Stop() error {
return nil
}
func (cb *carBuilder) Color(color Color) Builder {
cb.color = color
return cb
}
func (cb *carBuilder) Wheels(wheels Wheels) Builder {
cb.wheels = wheels
return cb
}
func (cb *carBuilder) TopSpeed(speed Speed) Builder {
cb.speedOption = speed
return cb
}
func (cb *carBuilder) Build() Interface {
return &car{
topSpeed: cb.speedOption,
color: cb.color,
}
}
func NewBuilder() Builder {
return &carBuilder{}
}
type car struct {
topSpeed Speed
color Color
}
```