Factory Pattern Node.js Coding Example

Nodejs Simple Factory Pattern Example

– Tutorial: “Factory Pattern Node.js Example”

Instead 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 from the code that does not vary.

In this tutorial, I shows you how to do it in NodeJs.

Factory Pattern Node.js Example

In this example, we have 3 types of cars: Audi, BMW, Mercedes. The car generating process depends on the input string "Audi" or "BMW" or "Mercedes".

CarFactory is the Factory class. In the client, we won’t use new keyword but create() method to create a new car:

const BMW = CarFactory.create('BMW');
Nodejs Simple Factory Pattern Example Sample Structure
Nodejs Simple Factory Pattern Example Sample Structure

Let’s implement Factory Pattern in Node.js !

Create default Node.js class

In the tutorial “Factory Pattern Node.js Example”, we create a Car class which is a parent class for Audi, BMW, Mercedes class that we’re gonna create in the next steps.


class Car {
    constructor(name) {
        this.name = name + '-' + Math.random().toString(36).substring(2, 15);

    showInfo() {
        console.log(`I'm ${this.name}`)

module.exports = Car;

This class has constructor method that assign a generated id to the car’s name and a showInfo() method to show the name field.

Create Node.js Sub-Classes of Default Class

Now we create 3 classes: Audi, BMW, Mercedes that extends default Car class above.


const Car = require('./default_car');

class Audi extends Car{
    constructor () {

module.exports = Audi;


const Car = require('./default_car');

class BMW extends Car {
    constructor() {

module.exports = BMW;


const Car = require('./default_car');

class Mercedes extends Car {
    constructor() {

module.exports = Mercedes;

Create Node.js Factory class

In the tutorial “Factory Pattern Node.js Example”, we create a CarFactory class that has create() method that genetes new car based on the input string type.


const Audi = require('./Audi');
const BMW = require('./BMW');
const Mercedes = require('./Mercedes');

class CarFactory {
    create(type) {
        switch (type) {
            case 'Audi':
                return new Audi();

            case 'BMW':
                return new BMW();

            case 'Mercedes':
                return new Mercedes();

                    console.log('Unknown Car type...');

module.exports = new CarFactory();

Use Nodejs Factory in Client – Factory Pattern Node.js Example


const CarFactory = require('./car_factory');

const Audi = CarFactory.create('Audi');
const BMW = CarFactory.create('BMW');
const Mercedes = CarFactory.create('Mercedes');
const Audi2 = CarFactory.create('Audi');


Check the result – Factory design pattern in Node.js

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

I'm Audi-7nuweb00pkj
I'm Audi-fick16rbvrd
I'm BMW-ygw7tqsdbxr
I'm Mercedes-1vfgp9wnhvk

Some notes

In the tutorial “Factory Pattern Node.js Example”, we have some notes as below:
– Why don’t we create the objects using new keyword?
Because we want to separate the part of code that is most likely to change from the others. In the future, if we need to modify the process of creating objects, we only need to change code inside the create() method of SimpleFactory class. So this client is not affected by these modifications.

– Cons of using simple factory method:
In case we want to add a new Car subclass, or delete an existing one, we have to change code in the factory’s create() method.

Read More

Here are a list of related articles with the tutorial “Factory Pattern Node.js Example”:

Related posts:

– References: Node.js Class.


We’ve learned how to use simple factory method to create objects in a cleaner and resilient way that gives us an easy-to-understand interface of our package’s function. This pattern is extremely useful when writing NPM package for public use.

Leave a Reply

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