Builder design pattern in Node JS

Recently, I have learned about the builder design pattern and I feel that this pattern will solve the code readability problem in Node JS. In this blog, I have mentioned a problem and its solution.

Did you ever feel awkward when you create an object by passing so many arguments in the constructor? The codes for the object creations will look ugly and unreadable if you pass all arguments in the constructor.

For example, let's say you have a class Person and this class has a constructor with 7 arguments (say, the arguments are, UID, name, age, address, DOB, education, phone_number).

Fig a. class name: Person

If you want to create a person object from the class Person, the code will look like,

var person = new Person(“UID_123”, “Saravanan AR”, “25”, “India”, “1994”, “B.Tech”, “00000”);

Does the code look good and maintainable for the longer term?

We can make these codes more readable and more maintainable by using the Builder design pattern.

To do this, create a separate class method for each argument in the constructor and all the methods should return “this”.

For example, in the below code snippet, we define a method setName() which has an argument “name”. This method’s task is to assign the argument “name” to the object’s name property. ( = name)

Fig b

Likewise, we have to create a separate method for all the remaining constructor arguments.

The final code for the Person class will look like,

Fig c. Person class definition using Builder design pattern

Now, we have changed our Person class definition. Next, we are going to create an object for this new class definition and we will compare both to see its benefit.

The code for creating an object for the new Person class definition (Fig c) is given in the below code snippet(Fig d). This looks far better than passing all arguments in the constructor itself. Any new developers can easily maintain the code than the previous one.

Fig d. Object creation for the class Person


It’s the time to compare both designs. By the end, you will know how much the builder design pattern solves our problem.

a) Full object creation:
The code for creating an object with all the arguments.

Using the normal way (Fig e), in this way, the code is unreadable and hard to maintain.

Fig e. Creating an object with all the arguments in the Normal way

Using the builder pattern (Fig f), here, we have more control over the arguments and it is highly readable and easy to maintain.

Fig f. Creating an object with all the arguments using the builder pattern

b) Partial object creation (Object creation with some arguments):

The code for creating an object with some arguments.

Using the normal way (Fig g), here, we created an object without age, address and DOB property. This way, the code looks ugly because we have to pass either undefined or null as the value for these params(age, address, and DOB).

Fig g. Creating an object with some arguments in the normal way

Using the builder design pattern (Fig h), here, we don’t have to care about the properties which we don’t want. We haven’t passed either undefined or null for omitting a certain property like we did in the normal way of creation.

In the fig h, we have created an object without age, address and DOB properties.

Fig h. Creating an object with some arguments using the builder pattern

Please ping me, if you find any good method than the above.