Importance of Builder Design Pattern

Issath Sesni
2 min readMay 24, 2021

--

What is Builder Design Pattern and Why

It is an alternative way to construct a complex object step by step and the final step will return the object. The process of constructing an object should be generic so that it can be used to create a different representations of the same object. This should be used only when you want to build different immutable objects using same object building process.

We use only single line and just pass the parameters that we want. We don’t pass null value, because the builder will handle other parameters. String Builder is a good example for builder pattern.

Note : We don’t use multiple constructor and setter. So that, it’s state can not be changed once it has been built. That ensures guarantee in immutability.

Advantages

  • The parameters to the constructor are reduced and provided in highly readable method calls.
  • It helps to minimize the number of parameters in constructor and thus there is no need to pass in null for optional parameters to the constructor.
  • Object is always instantiated in a complete state
  • Immutable objects can be build without much complex logic in object building process.

Disadvantages

The number of lines of code increase at least to double in builder pattern, but the effort pays off in terms of design flexibility and much more readable code.

Real world example.

Happy Learning.

--

--