Builder Design Pattern in Java

The Builder Design Pattern provides one of the best ways to create an object. So it comes under creational pattern. The builder pattern is an object creation software design pattern. Instead of using numerous constructors in case of abstract factory pattern and factory method pattern, 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.

The definition in original Gang of Four book is given below

Allows for object level access control by acting as a pass through entity or a placeholder object.

Class Diagram

Builder Design Pattern in Java

What problem a Builder design pattern solves

Take an example, a class with a list of constructors where each addition adds a new option parameter:

This is called the Telescoping Constructor Pattern. The problem with this pattern is that once constructors are 4 or 5 parameters long it becomes difficult to remember the required order of the parameters as well as what particular constructor we might want in a given situation.

One alternative we have to the Telescoping Constructor Pattern is the JavaBean Pattern where we call a constructor with the mandatory parameters and then call any optional setters after:

The problem here is that because the object is created over several calls it may be in an inconsistent state partway through its construction. This also requires a lot of extra effort to ensure thread safety.

So the better alternative is to use the Builder Pattern.

The builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters. For example imagine a DOM. We have to create plenty of nodes and attributes to get our final object. A factory is used when the factory can easily create the entire object within one method call.

Advantages

1) more maintainable if number of fields required to create object is more than 4 or 5.
2) less error-prone as user will know what they are passing because of explicit method call.
3) more robust as only fully constructed object will be available to the client.

Disadvantages

verbose and code duplication as Builder pattern needs to copy all fields from original class.

Implementation

To make a Cake we will follow several steps

a. mix ingredients
b. bake
c. frost

In the below example, we will create an abstract class CakeBuilder to define these three steps. Any class which extends CakeBuilder will follow the same steps to make a cake. Then we will use CakeDirector class to force the order of these three steps, i.e., we have to first mix ingredients, then bake and at last we have to frost the made cake. The CakeClient orders the cake of two types, one is Veg Cake and another one is Non-Veg Cake. Even though cakes are of different types but they are made in the same way. The making process allows different representation for the object that is created.

Run the class CakeClient and see the below output in the console

That’s all. Thank you for your reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

2 thoughts on “Builder Design Pattern in Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.