Builder Pattern: Design Patterns

Builder Pattern: Design Patterns

Builder pattern
Builder pattern

 

I have always been in love with the Creational design pattern: The Builder pattern. There are various instances in software development where you encounter complex object generation that has certain mandatory values and some optional values. The builder pattern comes in handy and is the best fit for these circumstances.

Why did we need the creation of this new design pattern?

Builder pattern was introduced to solve some of the problems with Factory and Abstract Factory design patterns when the Object contains a lot of attributes that may be mandatory or optional to create that object.

We have beautiful creational design patterns like Factory and the Abstract Factory pattern. It solves many design issues but also comes with some downsides.

There are majorly four flip-sides of the Factory pattern:

  1. Too Many arguments to be passed from client program to the Factory class cabe error prone. This is because most of the time the type of arguments are same.
  2. It is hard to maintain the order of arguments passed to the Factory class.
  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 be sent as NULL.
  4. When the object is heavy and its creation is complex, then all that complexity will be part of Factory classes. This might lead to complex factory design and may be confusing. The code might not be maintainable after some time.

Coming to the rescue, the builder pattern can solve the above issues by creating a step by step construction of any object with both mandatory and optional attributes. This helps to avoid inconsistency by getting the mandatory attributes via  a constructor and the optional attributes by some simple setter methods.

Let’s see how we can implement this in Java:

We can create a Builder design pattern using these simple steps.

  1. We need to create a static nested class and then copy all the arguments from the outer class to the Builder class. We should follow the naming convention. For example: if the class name is Subway then builder class should be named as SubwayBuilder.
  2. The Java Builder class should have a public constructor with all the required attributes as parameters.
  3. The Java Builder class should have methods to set the optional parameters and it should return the same Builder object instance(this) after setting the optional attribute.
  4. Provide a build() method in the builder class that will return the Object needed by client program. Keep a private constructor in the Class with Builder class as argument.

 

Let us now take a dive into the program in Java that explains the creation this pattern.

Let us take an example of making a sub in our favourite food chain Subway.

They need to know the bread type and if we want to have it toasted before adding any more items into the sub. These two things become mandatory for all the orders in the restaurant. Next we have optional items like onion, tomato, cheese etc that can be added according to our wish. These items are our optional attributes in the code.

Let us now see how would the code look like as explained above.

The Actual class and our Builder class would look like this:

Our client code, in this case the Main would create the instance in the following manner:

 

This simple design pattern can help to cleanly define our purpose and make an easy code that is both maintainable and easy to work with on the client side. Any new attribute addition can be added by simply adding new methods and the code remains intact. We do not have to refactor any code in multiple places where the Builder class is being called. If we need to use the new attribute, simply update the changes in only the required places you need to.

 

With these flexibility and beauty, the builder pattern is suggested and used by many programmers in various domains. This makes it one of the most used and extremely powerful design patters to be used in the industry.

For reference, please visit my Github Repository for this project.

Please follow and like: