Create complex object at run-time with Builder pattern

Ankit Sinhal
AndroidPub
Published in
3 min readMay 25, 2017

--

A design pattern is a well described solution to a common software problem. It also helps to prevent subtle issues that can cause major problems and improves code readability with the patterns.

The Builder is a creational pattern. With Builder pattern we can separate the construction of a complex object from its representation and build different objects using same construction process. Builder pattern is more likely implemented using method cascading.

As mentioned in the Gang of four -

“Separate the construction of a complex object from its representation so that the same construction process can create different representations.”

Where to use?

Following are the conditions which we can easily handle using builder design pattern-

1. When multiple representation of objects are required.

2. Too many argument to pass from client.

3. Object creation contains optional parameter.

What problem it solve?

We all know each class has a constructor in Java and if we don’t explicitly declare a constructor of a class then compiler builds a default constructor for that class. We can also create parameterized constructor which can take different parameters required to create an object.

Problem starts when an object can be created with lot of parameters and some of them may be mandatory and others may be optional.

In general we pass the NULL values for the optional parameters or overload more constructors to create a desired object of a class. Situation become more error prone when many fields has same type and we confused with their order while passing the values to the constructor.

To create an object of Employee class, we must pass all the values of parameters to its constructor. It will look like this:

Now what if only id, firstName and lastName are mandatory and rest fields are optional then object generation looks like-

Any like many more constructor…

The problem with above approach is that the object state will be inconsistent during building. Here builder design pattern will help you to achieve the goal.

How builder works

Below are the steps required to create a builder pattern:

  • Create a builder class with all the required fields.
  • Builder class should have a public constructor with all the required parameters.
  • Create methods to get the values of optional parameters. These methods should return the same builder object after setting the optional attribute.
  • Finally provide a build() method in the builder class which will return the desired object.

Here is the sample builder pattern example code where we have Employee class and EmployeeBuilder class to build it.

You can get the complete example code from GitHub.

And below is the way, we will use the EmployeeBuilder in our code:

Notice that Employee class has only no public constructor and buildEmployee() method in the EmployeeBuilder class is the only way to create Employee object.

Examples in JDK

Builder pattern is also used in Java classes like -

java.lang.StringBuilder#append() (unsynchronized)

java.lang.StringBuffer#append() (synchronized)

Conclusion

Using builder design patter we can have more maintainable code if number of fields required to create object is increased. It also reduce the chance to pass invalid values because of explicit optional method calls.

Thanks for reading. To help others please click ❤ to recommend this article if you found it helpful.

Stop of being selfish, spread the knowledge

Stay tuned for upcoming articles. For any quires or suggestions, feel free to hit me on Twitter Google+

Check out my blogger page for more interesting topics on Software development.

--

--

Ankit Sinhal
AndroidPub

Senior Software Engineer at Walmart. Dreamer and Achiever..