Builder Design Pattern

Builder design pattern also helps to create objects when we have different constructors with various parameters. The creation of separate constructors for different cases may consume additional memory and time for execution.

Suppose that we have a class called Computer with different properties and some of them are mandatory while others are optional. Today, we are implementing the code with the use of node js.

Here you can see that we have to pass values for each parameter. If it is mandatory, that will be ok.

But what if you need to get the values of only 3 parameters like brand, price and operating system details you have to manually set other vales as undefined as below.

The output:

But to be honest, that does not seems like good.. right.. 😞Because, you will have to remember the order of variables mentioned in the constructor. Therefore, this method is kind of complex and time consuming.

To avoid this problem we can use 2 different ways. The first way is create different builder class to create required parameters for computer. In our case, “brand” is the only parameter that we need to use for creating the object from computer (That means all the computer details section should display brand while other parameters are optional). Therefore, we can modify our program to become more flexible.

  • Use of Builder design pattern: The first way

Now we are better than previous since we have more flexible behavior in instantiation process.

JavaScript has also another way to use this builder pattern which is more convenient, less complex and even more readable than our second method.

In this method, we can pass the values for our constructor which are optional. Furthermore, if we assigned value it displays. If we do not assign values, the values are assigned as undefined.

  • Use of Builder design pattern: The second way

You can see that in line no: 3, we created mandatory field as normal way while optional properties inside the {}. So, I have mentioned there when the optional property does not consume any value, assign undefined value.

The output:

Pros and Cons of Builder Design Pattern


  • Provide readable method calls.
  • Object can be created in completed manner.
  • Reduce the coding stuff.
  • Immutable objects can be built easily.


  • Need separated builder classes for different products.
  • Lot of coding will involve is you go with the first way.





Associate Software Engineer at Virtusa.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Uses Of Tuple Functions In Python

Add FaceID & TouchID to your iOS App (Swift)

A different drumbeat

How I code Flutter on Sublime Text.

Future Proofing Mobile Applications

How to Download an Excel File with Multiple Sheets from Azure Blob Storage Using Python

Single degree of freedom SDOF.

Create an Angry Birds style game in 5 minutes with Unity!

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
Sanduni Jayaweera

Sanduni Jayaweera

Associate Software Engineer at Virtusa.

More from Medium

CS373 Spring 2022: Avi Ghayalod

CS373 Spring 2022: Dinesh Krishnan Balakrishnan

Formal Systems, Self Reference, and Epimenides

Configure OpenLdap in NOI to support any corporate LDAP structure (Watson AIOps v3.1.1 and v3.2)