Factory Pattern, Singleton Pattern, Module Pattern
Design Patterns in JavaScript Programming A basic reuse single module with multiple uses choice is one of the best readymade fast software design solutions for common problems in software design development. Reuse module concept in programming is widely used in object-oriented programming concept, but many of those concepts can be applied in JavaScript programming as well. Here in JavaScript Programming Responses, we will mainly focus on three design patterns namely, Factory Pattern, Singleton Pattern, and Module Pattern.

Factory Pattern in JavaScript.
Factory pattern in JavaScript software development is a design pattern method or process used to create a class object in JavaScript program without indicating the exact class of the class object being created. Here the factory pattern is used in JavaScript when the process of object creation in JavaScript is very complex, or it needs to be separated before use in the program. So the factory pattern in JavaScript program provides an interface to create an object in the superclass. But the subclass required in the program provides permission to modify the data type of the object being created.
Factory pattern use case in JavaScript.
When you need to create multiple class objects of the same type in a JavaScript program. But all these class objects can be created with different configurations.
Example of Factory pattern in JavaScript.
// let create a Factory Function
function Course(type, name) {
if (type === ‘javascript’) {
return new Javascript(name);
} else if (type === ‘java’) {
return new Java(name);
}
throw new Error(‘it is Invalid course type’); // it throw and error
}
// here Base class created
function Javascript(name) {
this.type = ‘Javascript’;
this.name = name;
this.medium = ‘English’;
}
Javascript.prototype.sayMedium = function() {
console.log(this.medium);
};
// Another class created
function Java(name) {
this.type = ‘java’;
this.name = name;
this.medium = ‘Hindi’;
}
Java.prototype.sayMedium = function() {
console.log(this.medium);
};
// above class Usage
const javascript = Course(‘Javascript’, ‘Python’);
const java = Course(‘Java’, ‘Html’);
javascript.sayMedium(); // result is – english
java.sayMedium(); // result is – hindi
Explanation of Factory pattern in JavaScript.
Here course a factory function is created, which declares the class data type of the course, and creates JavaScript and Java objects based on the given class data type.
Where JavaScript and Java indicate the special Behavior of the class.
Singleton pattern in JavaScript.
Singleton pattern in JavaScript software development process is a software design pattern that limits the instantiation of developed classes in JavaScript program to a single class object. It ensures that there is only one class instance in the current class program and a global access point is provided for it.
Use case of Singleton pattern.
When you want to ensure in a JavaScript program that only one instance of a class is created in the current program, such as to manage a database connection or to manage application-wide configuration.
Example of Singleton pattern.
// Singleton Class created
class Database {
constructor() {
if (Database.instance) {
return Database.instance; // it use to Return existing instance
}
this.connection = ‘Here Database connection is created’;
Database.instance = this; // it use to Store the instance
}
testConnection() {
return this.connection;
}
}
// let create database Usage
const database1 = new Database();
const database2 = new Database();
console.log(database1 === database2); // result is – true here both database refer to the same instance
console.log(database1.testConnection()); // result is -Here Database connection is created
console.log(database2.testConnection()); // result is -Here Database connection is created
Explanation of Singleton pattern.
It ensures in a database class that only one instance is created in the current class. If an instance already exists in the JavaScript class program, it returns the existing instance to the program.
In JavaScript program class instances are stored in Database.instance, which is checked or analysed before a new one is created.
Module pattern in JavaScript.
The Module pattern in JavaScript program development is a software design pattern tool used to arrange the developed program source code into separate, reusable small modules or units. The Module pattern in JavaScript is used to create self-contained small program module units of program code, it helps to recover from damage to the global namespace in JavaScript, and it helps to better maintain or organize the program source code in software development.
Use case of Module pattern.
When you need to create program modules in a JavaScript program that combine their special capabilities. But they only expose a public API.
Example of the Module pattern.
// here new Module Pattern created
const TestModule = (function() {
let increase = 3; // here increase Private variable created
// hare Private inreament function created
function increment() {
increase++;
}
// here Public API
return {
increment: increment,
testincrease: function() {
return increase;
}
};
})();
// let multiple tesmdule Usage
TestModule.increment();
TestModule.increment();
console.log(TestModule.testincrease()); // result is – 5
Explanation of the Module pattern.
Where the module in a JavaScript program is an Immediate Executable Function Expression (IIFE). Which creates a module with private data (count) and private functions like increment.
Where increment and getCount are exposed in the public API, but here count and increment are not directly accessible from outside the module.
Summary of the Factory Pattern, Singleton Pattern, Module Pattern multiple class Patterns.
Pattern | Purpose | When to Use | Example Use Case |
Factory Pattern | It uses to Creates objects without specifying the class | When you need to creating multiple types of objects with similar type interfaces | It uses to Creating different types of class object |
Singleton Pattern | It uses to Restricts a class to a single instance class object | When you need create a single shared instance across the entire active application | It used to create Database connection or Logger service |
Module Pattern | It uses to Encapsulates code into self-contained small class modules | You only use When you want to keep your code organized and avoid polluting the global namespace | It allows Utility libraries or configuration of class objects |
Conclusion of Factory Pattern, Singleton Pattern, Module Pattern in JavaScript.
The Factory pattern in the JavaScript program software development process eases the class object creation process by providing a central function that can create new class objects based on specific programming requirements or conditions.
Whereas the Singleton pattern in JavaScript programs ensures that only one instance of a class is created and provides a global access point for it. Which is an ideal choice for controlling and managing resources like database connections in the program.
The Module pattern in JavaScript programs helps in arranging the code recovered from the damage of the global namespace into re-use, merged units.