mirror of
https://github.com/rjNemo/design-patterns
synced 2026-06-06 02:26:40 +00:00
32 lines
2.6 KiB
Markdown
32 lines
2.6 KiB
Markdown
# Builder
|
||
|
||
Builder is a creational design pattern, which allows constructing complex objects step by step.
|
||
|
||
## Summary
|
||
|
||
Builder is a creational design pattern that lets you construct complex objects step by step. The pattern allows you to produce different types and representations of an object using the same construction code.
|
||
|
||
## Problem
|
||
|
||
Imagine a complex object that requires laborious, step-by-step initialization of many fields and nested objects. Such initialization code is usually buried inside a monstrous constructor with lots of parameters. Or even worse: scattered all over the client code.
|
||
|
||
For example, let’s think about how to create a `House` object. To build a simple house, you need to construct four walls and a floor, install a door, fit a pair of windows, and build a roof. But what if you want a bigger, brighter house, with a backyard and other goodies (like a heating system, plumbing, and electrical wiring)?
|
||
|
||
## Solution
|
||
|
||
The Builder pattern suggests that you extract the object construction code out of its own class and move it to separate objects called builders.
|
||
|
||
## How to Implement
|
||
|
||
1. Make sure that you can clearly define the common construction steps for building all available product representations. Otherwise, you won’t be able to proceed with implementing the pattern.
|
||
|
||
1. Declare these steps in the base builder interface.
|
||
|
||
1. Create a concrete builder class for each of the product representations and implement their construction steps.
|
||
Don’t forget about implementing a method for fetching the result of the construction. The reason why this method can’t be declared inside the builder interface is that various builders may construct products that don’t have a common interface. Therefore, you don’t know what would be the return type for such a method. However, if you’re dealing with products from a single hierarchy, the fetching method can be safely added to the base interface.
|
||
|
||
1. Think about creating a director class. It may encapsulate various ways to construct a product using the same builder object.
|
||
|
||
1. The client code creates both the builder and the director objects. Before construction starts, the client must pass a builder object to the director. Usually, the client does this only once, via parameters of the director’s constructor. The director uses the builder object in all further construction. There’s an alternative approach, where the builder is passed directly to the construction method of the director.
|
||
|
||
1. The construction result can be obtained directly from the director only if all products follow the same interface. Otherwise, the client should fetch the result from the builder.
|