vimwiki/tech/builder.wiki

26 lines
988 B
Plaintext

= Builder =
This is a design pattern where you create a complex object step by step. The
pattern allows you to produce different types and representations of an object
using the same construction code.
This is often used when you have an object with a large number of constructor
paramaters due to the complexity of said object. Or, when you have a large
number of subclasses for specific variations on the object.
What we can do therefore is create a *builder* class who has several functions
that can be called to modify some object to be created, then return the object
once it is in a state to be returned.
== Implementation ==
For different features of the object, have a function to add that feature or
flag to the object, such as `addGPS`, `addSeats`, and `addWindows` for a car.
Have each return `this` so that they may be chained together.
== Use case ==
* when code needs to create different representations of the same product, such
as a sports car, truck, and sedan