First of all, let’s start with an explanation of what design patterns are. In the simplest terms, they allow us to re-use code for re-occurring problems. Instead of solving the same problems again and again, we can re-use efficient code that already works. Why would you use design patterns? The reasons are that they are:

  • Well documented and tested
  • Reusable in many different situations
  • Efficient
  • Saving you time

There are three types of design patterns:

  1. Creational - the creation of the object instances
  2. Structural - the way the objects are designed
  3. Behavioural - how objects interact with each other

The Creational Design Patterns covered in the article are:

  1. Singleton
  2. Factory
  3. Builder
  4. Prototype

These are the two ways of implementing the Singleton pattern in NodeJS. To recap:

  • Singleton is useful when you want only one instance of a class.
  • In NodeJS, we can make use of the module caching system to export an instance directly.

#general programming #javascript #node.js #node

The 4 Creational Design Patterns In Node.js You Should Know
1.35 GEEK