Separate the construction of a complex object from its representation so that the same construction process can create different representations.
Instead of using numerous constructors, the builder pattern uses another object, a builder that receives each initialization parameter step by step and then returns the resulting constructed object at once.
One solution for multiple problems:
1. The intention of the builder pattern is to find a solution to the telescoping constructor anti-pattern.
The telescoping constructor anti-pattern occurs when the increase of object constructor parameter combination leads to an exponential list of constructors.
2. Too Many arguments to pass from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side it’s hard to maintain the order of the argument.
3. Some of the parameters might be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to send as NULL or we need to make another constructor.
4. If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that is confusing.
How to Implement Builder Pattern?
1. We need to create a static nested class and then copy all the arguments from the outer class to the Builder class. It will be better to follow naming convention as StringBuilder class.
2. The Builder class should have a public constructor with all the mandatory attributes as parameters.
3. Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute.
4. Finally, there should be a method in the builder class that will return the Object needed by client program. For this we need to have a private constructor in the Class with Builder class as argument.
|Builder Design Pattern|
Example of Builder Pattern:
Builder Design Pattern Example in JDK