Intent
It builds a complex object using simple objects and using a step-by-step approach. It allows producing different representations, and types of an object using the same construction code.
Problem
If we have a complex object that requires exhausting step-by-step initialization of many fields. this initialization code is often buried inside a constructor with lots of parameters. So we create the elements of a complex aggregate.
Solution
The builder design pattern suggests extracting the object construction code out of its own class and moving it to separate objects called "builders".
To create an object, we must execute a series of steps on a builder object. We don’t have to call all the steps. We only call the steps that are necessary for producing a particular configuration of the object we want to create.
Pros and Cons
Pros
- Encapsulates code for construction and representation.
- Allows us to vary a product's internal representation.
- We can reuse the same construction code when building various representations of products.
Cons
- The complexity of the code increases as the pattern requires creating multiple new classes.
Thank you, and goodbye.