Logo

dev-resources.site

for different kinds of informations.

How to implement simple Factory Pattern in Node.js

Published at
5/3/2021
Categories
node
factory
pattern
Author
loizenai
Categories
3 categories in total
node
open
factory
open
pattern
open
Author
8 person written this
loizenai
open
How to implement simple Factory Pattern in Node.js

https://grokonez.com/design-pattern/implement-simple-factory-method-pattern-node-js-example

How to implement simple Factory Pattern in Node.js

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, grokonez shows you how to do it in NodeJs.

Nodejs simple Factory Pattern example

Overview

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');

Sample Structure

nodejs-simple-factory-pattern-example-sample-structure

Implement simple Factory Pattern in Nodejs

Create default class

This Car class is a parent class for Audi, BMW, Mercedes class that we're gonna create in the next steps.

default_car.js


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 subclasses of default class

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

More at:

https://grokonez.com/design-pattern/implement-simple-factory-method-pattern-node-js-example

How to implement simple Factory Pattern in Node.js

factory Article's
25 articles in total
Favicon
Clojure is Awesome!!!
Favicon
Understanding the Factory and Factory Method Design Patterns
Favicon
Factory Design Pattern
Favicon
Things You Want to Ensure When Factory Resetting Your HP Laptop Without a Password
Favicon
The Factory Pattern in C#: Creating Objects the Smart Way
Favicon
Enhance Your Factory Car Radio's Bass with Affordable Upgrades
Favicon
Page Object Model and Page Factory in Selenium
Favicon
Factory functions with private variables in JavaScript
Favicon
Evoluindo nosso Projeto Rails: Integrando o Padrão Factory para Maior Flexibilidade e Organização
Favicon
Javascript Factory Design Pattern
Favicon
Dart Abstract and Factory Keywords
Favicon
Azure Data Factory Overview For Beginners
Favicon
Improve your factories in Symfony
Favicon
Reduzindo a quantidade de Branchs na criação de Objetos com uma estrutura plugável
Favicon
Java 9 Factory Method for Collections: List, Set, Map
Favicon
How to implement simple Factory Pattern in Node.js
Favicon
Factory Design Pattern (simple example implementation in PHP)
Favicon
Spring's FactoryBean Interface
Favicon
Custom Validators for Angular Reactive Forms
Favicon
Design Patterns: Factory
Favicon
Creating objects dynamically with factory pattern in javascript
Favicon
Is this the real life, is this just fantasy?
Favicon
Design Patterns: Factory Pattern, Part 2
Favicon
Patterns in Kotlin: Abstract Factory
Favicon
SUT Factory

Featured ones: