Monday, September 12, 2016

Builder Design Pattern

Builder Pattern says that "construct a complex object from simple objects using step-by-step approach"

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.
There are three major issues with Factory and Abstract Factory design patterns when the Object contains a lot of attributes.
  1. Too Many arguments to pass from client program to the Factory class that can be error prone because most of the time, the type of arguments are same and from client side it’s hard to maintain the order of the argument.
  2. Some of the parameters might be optional but in Factory pattern, we are forced to send all the parameters and optional parameters need to send as NULL.
  3. If the object is heavy and its creation is complex, then all that complexity will be part of Factory classes that is confusing.
We can solve the issues with large number of parameters by providing a constructor with required parameters and then different setter methods to set the optional parameters. The problem with this approach is that the Object state will be inconsistent until unless all the attributes are set explicitly.
Builder pattern solves the issue with large number of optional parameters and inconsistent state by providing a way to build the object step-by-step and provide a method that will actually return the final Object.

For example, you can consider construction of a home. Home is the final end product (object) that is to be returned as the output of the construction process. It will have many steps, like basement construction, wall construction and so on roof construction. Finally, the whole home object is returned. Here using the same process, you can build houses with different properties.
GOF says,

Advantage of Builder Design Pattern
The main advantages of Builder Pattern are as follows:
  • It provides clear separation between the construction and representation of an object.
  • It provides better control over construction process.
  • It supports to change the internal representation of objects.
Builder Design Pattern Example in JDK
DocumentBuilderFactory, StringBuffer, StringBuilder are some examples of builder pattern usage in java API.
Some of the builder pattern example in Java classes are;
  • java.lang.StringBuilder#append() (unsynchronized)
  • java.lang.StringBuffer#append() (synchronized)
Usage examples:

  1. Builder pattern may be very useful while writing Unit Tests. When in order to construct the object under the test, need to pass a lot of parameters to the constructor and some of these parameters are completely irrelevant for the specific test. Builder class creation with separate methods for each parameter that should be tested, which returns by the end complete object under the test will help to write many UTs effectively, without duplicating the code.
  2. Building an XML document with HTML elements (<html>,<h1>,<h2>, <body>,<p> and etc)
  3. Building a smartphone object with attributes like RAM, size, resolution, OS, waterproof and so on.



No comments:

Post a Comment