In this tutorial, I will so you 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.
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
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.
Audi.js
const Car = require('./default_car');
class Audi extends Car{
constructor () {
super('Audi')
}
}
module.exports = Audi;
BMW.js
const Car = require('./default_car');
class BMW extends Car {
constructor() {
super('BMW')
}
}
module.exports = BMW;
Mercedes.js
const Car = require('./default_car');
class Mercedes extends Car {
constructor() {
super('Mercedes')
}
}
module.exports = Mercedes;
Create Factory class
The CarFactory
class has create()
method that genetes new car based on the input string type
.
car_factory.js
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();
default:
{
console.log('Unknown Car type...');
}
}
}
}
module.exports = new CarFactory();
Use simple Factory in Client
app.js
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');
Audi.showInfo();
Audi2.showInfo();
BMW.showInfo();
Mercedes.showInfo();
Check the result
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
– 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.
Conclusion
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.