Design Pattern (Part 04) — Builder Design Pattern

Creating a subclass for every possible configuration of an object

When to use a Builder Pattern

Advantage of Builder Design Pattern

  1. Given better control over the construction process.
  2. Given a clear separation between the construction and representation of an object.
  1. First, 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 and if the class name is Computer then the builder class should be named as ComputerBuilder.
  2. Java Builder class should have a public constructor with all the required attributes as parameters.
  3. Java Builder class should have methods to set the optional parameters and it should return the same Builder object after setting the optional attribute.
  4. The final step is to provide a build() method in the builder class that will return the Object needed by the client program. For this, we need to have a private constructor in the Class with Builder class as an argument.

--

--

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
Arun prashanth

Arun prashanth

Software Engineer at Virtusa (Pvt) Ltd.