Node

Singleton Pattern in Node.js

Singleton is object that can have only a single, unique instance, with a single point of access. Node.js module system provides simple way to implement Singleton using module.exports. Module will be cached when it is accessed using require() statement. So our module is merely a cached instance although it behaves like a Singleton. In this tutorial, we’re gonna look at ways to implement this kind of Singleton in Node.js: Singleton ObjectSingleton Class Node.js Singleton Object example We will create a Bank object, it has 3 methods:– deposit() increases cash.– withdraw() decreases cash.– total() returns cash. Bank.js let cash =...

Read more...

Factory Pattern in Node.js

nstead of using class constructors or new keyword to create an object of a class, we can abstract this process. So, we can determine the type of object at run-time, by the time of generating that class object. The implementation seems like Factory Method, but simpler than Factory Method. This simple Factory is not treated as a standard GoF design pattern, but the approach is common to any place where we want to separate the code that varies a lot...

Read more...

Builder Pattern in Node.js

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 functionusing 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 function PersonBuilder() {    this.person...

Read more...

Dependency Injection in Node.js with Example

Dependency Injection is one form of Inversion of Control technique that supports the Dependency Inversion principle (the last one of SOLID Principles – decoupling software modules). In this tutorial, we’re gonna look at way to implement Dependency Injection pattern in Node.js. Dependency Injection Overview What is the problem with tightly coupled modules?We may end up hardwiring multiple modules. It’s difficult to change a module later, so we may need to refactor a lot of code.=> How we can avoid writing...

Read more...

Proxy Pattern in Node.js

Sometimes we may want to access indirectly to an object, via a substitute. That substitute is called Proxy. The Proxy Pattern helps us deal with a proxy object and talk to the actual object. In this tutorial, we’re gonna look at 2 ways to implement Proxy Pattern in Node.js: Using custom Proxy ClassUsing Proxy API Proxy Pattern Overview The image above shows that Proxy and Subject have the same methods. Proxy forwards each operation to Subject, we can improve Subject‘s methods with additional pre-processing or post-processing. Proxy Pattern in Node.js using...

Read more...