Builder Design Pattern

Sanduni Jayaweera
3 min readMay 25, 2021


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.