My understanding of Design pattern in node js

Edison Devadoss
3 min readFeb 15, 2020

--

In this article, I will explain the Design pattern in the node .js

google image

What is Design Pattern

In software engineering, a design pattern is generally repeatable to a commonly occurring problem in software design.

Uses of Design pattern

  • Design pattern can speed up the development process by providing tested proven development paradigms.
  • Improve your code better readability.

Type of design patterns in node.js

We can not define these are things are only desing patterns because it evolves depends on projects or needs.

Most of the time we knowingly or unknowingly follow some design patterns. For example, if you using Angular framework the framework itself follows some pattern like create the component structures and router.

Below I list out several design patterns:

  1. Middleware pattern
  2. Module pattern
  3. Control flow pattern
  4. Observers pattern
  5. Decorator pattern
  6. Singleton pattern

Middleware pattern

Middleware function is functions that have access to the request (req) object and response (res) object and the next middleware function in the application’s request-response cycle.

Example:

function(req, res, next){
...
}

Middleware tasks

  • Authenticating/ Authorization
  • Parsing body
  • Validations
  • End request
  • Call the next middleware

These are the tasks middleware can perform.

Module Pattern

Modules break the complex system into reusable parts. That means for our better understanding we maintain our code in different directories.

  • Routes(all the routes come under the Routes directory)
  • Queries(all the complex query come under the Queries directory)
  • Policy(all the roles of user management come under the Policy directory)
  • Controller(all the controllers come under the Controller directory)
  • Module(all the modules come under the Module directory)

Control flow

Using async/await for avoiding to write multiple .then and .catch statement. If you are using multiple callbacks function code is complicated to understand. We can avoid using async/await method.

Example:

//callback function
doc().then((res)=>{
app(res.uri).then((res1)=>{
console.log('res1', res1);
})
});
// async/await
const res = await doc();
const val = await app(res.uri);

Observers

The observer is a behavioural pattern which means that is concerned about communication between the object.

Example:

const fastify = require('fastify);
const app = fastify();
app.listen(3000);

Then listen method is an example of the Observer pattern because it listens to the port. Socket.io is the best example of the Observer.

Decorator

Attach additional responsibility to the object dynamically. Using Decorator we can set up req and res headers also.

Example:

username = user.firstName + user.lastName

From the above example, we combined username is the combination of the firstName and lastName.

Singleton Pattern

Singleton pattern restricts the number of ‘instantiations’ of a class to one.

Using Singleton pattern we create one instance for the object.

Example:

//cirlce.js
var PI = Math.PI;
function circle (radius){
return radius * radius * PI;
}
module.export.circle = circle;
//area.js
const circleArea = require('./circle');
console.log(circleArea.circle(5))//

We can use this function wherever we want using require() method but it creates one object instance.

Thanks for reading. Have a great day!

--

--

Edison Devadoss
Edison Devadoss

Written by Edison Devadoss

Software Engineer / Full Stack Developer / JavaScript / React / React Native / Firebase / Node.js / Book Reader

No responses yet