Builder
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.
Here's an example of builder that should satisfy the Builder interface. Let's say that Porsche can only build Boxster at the moment.
The Boxster struct should look like this.
Ideally we can have many different types of builder, like BMWBuilder, AudiBuilder and etc...
Finally we can use what we have developed to build some Boxsters.
Last updated