- What to look for:
- Heavy-parametrizable constructor (many optional parameters)
- Usually when u have more than 3-5 params for the object
- Creating the same object with different variations (e.g., luxury vs normal)
- Also useful for: sets of highly customizable objects (different versions) with the same methods (i.e., higher and lower levels of variations (e.g., two types of computers with different specs for all concrete methods (builders) (lower, specific to each), and different number of components in the computer (higher, shared)). On one side you customize the concrete builder (creating different versions for the same object), on the other you customize the level of construction (in the director)
- Use cases:
- ML pipeline
- HTTP request
- Database query
- Outline:
- Problem is that if you want to create an object that has too many params (very detailed specs, lots of possible combinations) you may fall under the temptation to create too many subclasses or create a very large constructor with most params being usually set to 0
- Breaks down steps to build objects into its own houses. Construction steps may be differ
- Builder class provides the given methods to build and object whereas the Director provides the order of execution. Director is not strictly necessary. However, it does encapsulate some recycable logic that could prevent redundant calls from the client.
- Components
- Product ⇒ final result
- Abstract builder (interface) ⇒ provides all the methods to include in the building process for a given product
- Note: you can have a matrix of object to variations where the variations are defined per the directors methods and the objects by the passed builders (assuming Director receives Builders with same methods
- Note 2: retuning self at each stage of the build could be a great idea for
- Flow
- Client instantiates a
Director
and a ConcreteBuilder
- Code sample