Builder Design Pattern

What is Builder Design Pattern

During the object, creation depending on the availability of the variables, and depending on the situation the state of the object has to be changed and the way create is different.

To get the work done developers can set the absent variables into their defaults or null value. or can have multiple constructors. or can use telescopic constructors as below.

//Telescopic Constructors
public PremiumUser(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
public PremiumUser(String id, String firstName, String lastName){
this(firstName, lastName); = id;
public PremiumUser(String id, String firstName, String lastName, boolean isActive){
this(id, firstName, lastName);
this.isActive = isActive;

Though the solutions are working which are not good programming practices.

As a solution for this Builder pattern provides step by step approach to creating objects.

Implementing Builder Design Pattern

The builder class itself is an inner class of the original POJO and it implements like this.

Now using the method chaining approach developers can keep appending the values and create objects.

What happens is actually developers using builder object, but at the moment of the “build()” method call it will return as a new PremiumUser instance.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store