How to implement Builder Pattern in Node.js

nodejs-builder-pattern-example-feature-image

When we want to create complicated object which has multiple parts, we can use Builder Pattern that separates the building of these parts. This creational process does not care about how these parts are assembled.

In this tutorial, we’re gonna look at 2 ways to implement Builder Pattern in Node.js:

  • using Builder function
  • using Builder class

Node.js Builder Pattern example using Builder function

Our Builder function works as a Builder object that can build a Person object.
A Person object has 4 fields: name, age, location, languages.

PersonBuilder.js

Now look at the code in app.js:

You can see that, instead of using:

We use PersonBuilder object with its methods to set value for fields, then we end with buildInfo() which return a real Person object:

Run with command: node app.js. Here is the result:

Node.js Builder Pattern example using Builder class

In this example, we’re gonna use Builder Pattern to create Customer objects that have fields: name, age, location, languages.

The difference is that we will split Builder function into Builder class (CustomerBuilder) and Product class (Customer).

Create Product class for complex object

Customer represents the complex object:

Customer.js

You can see that we pass a builder object to constructor method and use available values of builder object to set value for each parts. So how do we create parts of Customer object?

We have a builder class:

Create Builder class

CustomerBuilder.js

Now look at these CustomerBuilder methods, they are straightforward, we use them to build the value for name, age, location, languages. The important things we need to notice are:
return this at the end of each method, it guarantees that we always have a Builder object after running the method.
buildInfo() returns the final product: Customer object.

Write Client code for testing

app.js

Run with command: node app.js. Here is the result:

Conclusion

In the tutorial, we’ve known how to implement Nodejs Builder Pattern in 2 ways: using Builder function and using Builder class. Here are some notes:

– We can create complicated objects step by step with encapsulation of construction process that we control abstractly.
– Not comfortable if dealing with objects that can be modified later.
– We duplicate some portion of the code which may have significant impact in some context and turn into an anti-pattern.

By grokonez | April 4, 2019.



Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*