Design patterns are an important part of software development. Design pattern is a proven way of solving a certain problem. In this tutorial, you’ll try to understand factories design pattern in Node.js.

Getting Started

Let’s assume that you are trying to build a web application for employee management. There are different kinds of employees available in the company. For the time being assume three kind of employees working in the company

  • Engineer
  • Manager
  • Director

So, let’s create a separate class to handle each type of employee.

Here is the Engineer class :

/* engineer.js */

class Engineer{
    constructor(fName, lName){
        this.firstName = fName
        this.lastName = lName
    }

    getName(){
        return ' Engineer ' + this.firstName + " " + this.lastName
    }
}
module.exports = Engineer

Here is the Manager class :

/* manager.js */

class Manager{
    constructor(fname, lname){
        this.firstName = fname
        this.lastName = lname
    }

    getName(){
        return 'Manager ' + this.firstName + " " + this.lastName
    }
}
module.exports = Manager

Here is the Director class :

/* director.js */

class Director{
    constructor(fname,lname){
        this.firstName = fname
        this.lastName = lname
    }

    getName(){
        return 'Director ' + this.firstName + " " + this.lastName
    }
}
module.exports = Director

The Problem

You’ll be using the above different classes in the application file and creating objects. Create a file called index.js. This file will be our application file. You’ll create a new instance of each of the employee type and display their name. Here is how index.js file looks:

const Engineer = require('./engineer')
const Manager = require('./manager')
const Director = require('./director')


let engineer = new Engineer('Roy', 'Agasthyan')
let manager = new Manager('Sam', 'Johnson')
let director = new Director('Jack', 'Daniel')


console.log('Engineer is ', engineer.getName())
console.log('Manager is ', manager.getName())
console.log('Director is ', director.getName())

Save the above changes and try running the index.js file. Everything looks good and it should work just fine.

The problem will arise when the application grows and let’s say some more new employee types get added. Our index.js file will need to import all the new employee types and create instances of each.

The Solution : Factories Design Pattern In Node.js

You can use factories design pattern in Node.js to simply your code and reduce messy code. You need to create a factory which will import the different employee classes. Instead of importing all employee types in index.js you can rather import the factory.

Let’s see how you can implement it. Create a file called empFactory.js. Inside empFactory.js import all the required employee classes. Based on the type of employee you can return different employee instances to the application code. Here is how it looks :

/*  empFactory.js  */

const Engineer = require('./engineer')
const Manager = require('./manager')
const Director = require('./director')

const empFactory = function(fname, lname, type){
    if(type === 'engineer'){
        return new Engineer(fname,lname)
    } else if(type === 'manager') {
        return new Manager(fname, lname)
    } else if(type === 'director'){
        return new Director(fname, lname)
    }
}

module.exports = empFactory

Now modify the index.js code to import from empFactory.js and create instance based on employee type. Here is how index.js file looks :

/* index.js */

const empFactory = require('./empFactory')

let engineer = empFactory('Roy','Agasthyan','engineer')
let manager = empFactory('Sam','Johnson','manager')
let director = empFactory('Jack', 'Daniel','director')

console.log(engineer.getName())
console.log(manager.getName())
console.log(director.getName())

Wrapping It Up

In this tutorial, you learnt how to use factories design pattern in Node.js. In the coming days, we’ll try to cover other design patterns in Node.js.

How was you experience learning factories design pattern in Node.js ? Do let us know your thoughts in the comments below.