Constructor is also one of the way to create an object and it does have some cons to which Builder pattern covers up very well to create an object piece-by-piece and provides us with more flexibility creating an objects.
Generic approach is implemented in the process of constructing an object so that it can be used to create different representation of same object.
The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming.
When to use?
This pattern is to be implemented when:
- There is a need to use constructor with long parameter list or multiple constructor with different parameters.
- There is a need to build multiple representation of same object, i.e. objects of same class with different characteristics.
Scenario
Let’s understand the pattern diving into a scenario where you went to create a profile on an E-commerce website.
Creating you profile for the first time will make you enter some of your details: