We want to build some cars here. Let's see we can use builder pattern build some cars.
Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations.
Define what is a car? It can drive, brake and steer.
package car
type Car interface {
Drive() error
Brake() error
Steer() error
}
What are the parts that make up a car?
type Engine struct {
PeakTorqueRPM uint
MaxTorque uint
}
type Wheels struct {
Width float64
TireMake string
}
Now how should a builder behave? It should install parts.
type Builder interface {
Init(model string) Builder
InstallEngine(*Engine) Builder
InstallWheels(*Wheels) Builder
Build() Car
}
Here's an example of builder that should satisfy the Builder interface. Let's say that Porsche can only build Boxster at the moment.