Builder Design Pattern

Builder is a design pattern that coming under the creational category. It allows developers to build a complex system step by step.

What is Builder Design Pattern 🔨

Definition : — Builder pattern aims to “Separate the construction of a complex object from its representation so that the same construction process can create different representations — geeksforgeeks

In the implementation of the builder pattern in the final step, it returns the object. This pattern uses a different representation of the same object. This was created to overcome the challenge of containing many attributes in the factory and abstract factory. It avoids the telescoping constructor creation. If you want to create a program using this pattern, make sure that you are able to create a common constructor for all the available product types. If not, you cannot proceed with the pattern implementation.

When to use Builder pattern?

Prefer the Builder pattern when we need to create an object which has a lot of attributes in which some of them are optional and some of them are required attributes.

Advantages 👍

Here are some advantages of the builder pattern

  • It provides better control over the construction process
  • It provides a clear separation between the construction and representation of an object
  • It minimizes the number of parameters passed to the constructors (It doesn’t require to pass null as parameter value)
  • It supports changing the internal representation of objects
  • It creates a complete object instead of creating an object from an incomplete state
  • It generates more readable client code

Disadvantages 👎

It inherits some disadvantages too.

  • In order to create an object, you must first create its builder. It could be a problem in some performance-critical situations.
  • It generally creates more codes

Implementation 💻

Use case: — Imagine that in a housing complex there are different type of houses are available. In here you can select a simple house or can add more features like swimming pool, garden, garage, fancy statues. Think you were hired to design a system for this and what is the way that you are going to implement this.

Following implementation explain how this scenario implements using builder design pattern.

  • Create house.js file to manage basic variable information related to the house class.
  • Then you have to create a builder class builder.js to manage the builder design pattern. Here you have to follow these steps
  1. Create a file to manage builder work (builder.js)
  2. Create a constructor with the required parameters
  3. Create functions to manage each parameter variables
  4. Write a return statement for each function
  5. Create a method that will return the instance of the class for which the Builder class is made. build()
  • Finally, create the main application to invoke the builder class.

I think now you are clear about the builder design pattern and how it works. For more details, you can refer to the references included at the end of this article.

Stay Safe & Learn New Things!!! 😃




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
Thilini Weerasinghe

Thilini Weerasinghe

Currently working as an Associate Software Engineer at Virtusa. Has completed degree in B.Sc (Hons) Computing & Information Systems. After all I am a Human...