📌 Module, Factory, and Singleton in JavaScript
JavaScript provides different design patterns for organizing and structuring code
efficiently. Among them, Module, Factory, and Singleton are widely used for code
reusability, encapsulation, and efficient memory usage.
🔹 1. Module Pattern
📌 What is it?
The Module Pattern is used to encapsulate private and public methods inside a
function using closures. It prevents global scope pollution and allows controlled
access to variables.
📌 Example: Creating a Counter Module
const Counter = (function () {
let count = 0; // Private variable
return {
increment: function () {
count++;
[Link](`Count: ${count}`);
},
decrement: function () {
count--;
[Link](`Count: ${count}`);
},
getCount: function () {
return count;
},
};
})();
[Link](); // Count: 1
[Link](); // Count: 2
[Link]([Link]()); // 2
[Link] = 100; // ❌ Cannot modify count (private variable)
✅ Benefits:
Encapsulation: Private variables are not accessible directly.
Prevents Global Scope Pollution: Keeps code modular and clean.
Reusability: Can be used in multiple parts of the application.
🔹 2. Factory Pattern
📌 What is it?
The Factory Pattern is used to create multiple objects using a common function
without using the new keyword. It helps avoid code duplication and makes
object creation more dynamic.
📌 Example: Creating User Objects Using a Factory
function UserFactory(name, role) {
return {
name,
role,
greet: function () {
[Link](`Hello, I am ${[Link]} and I am a ${[Link]}`);
},
};
const user1 = UserFactory("Dikshyant", "Admin");
const user2 = UserFactory("Alice", "Editor");
[Link](); // Hello, I am Dikshyant and I am an Admin
[Link](); // Hello, I am Alice and I am an Editor
✅ Benefits:
Reusable Object Creation: Generates objects dynamically.
Avoids the new keyword: Makes code simpler and more readable.
Encapsulation: Objects remain independent.
🔹 3. Singleton Pattern
📌 What is it?
The Singleton Pattern ensures that a class or function has only ONE instance
and provides a global point of access to it.
📌 Example: Database Connection Singleton
const Database = (function () {
let instance; // Private instance variable
function createInstance() {
return { connection: "Database Connected!" };
return {
getInstance: function () {
if (!instance) {
instance = createInstance();
return instance;
},
};
})();
const db1 = [Link]();
const db2 = [Link]();
[Link](db1 === db2); // true (Both are the same instance)
[Link]([Link]); // Database Connected!
✅ Benefits:
Ensures a Single Instance: Prevents redundant object creation.
Saves Memory: Multiple calls return the same instance.
Used for Shared Resources: Best for database connections, caching, etc.
🔥 Summary
Pattern Purpose Example Use Case
Module Encapsulates private variables Creating self-contained modules
Pattern and methods (e.g., a Counter)
Pattern Purpose Example Use Case
Factory Creates multiple objects without Creating users, products, or services
Pattern new dynamically
Singleton Managing database connections or
Ensures only one instance exists
Pattern app settings
🚀 Conclusion
✅ Use Module Pattern for encapsulation and private variables.
✅ Use Factory Pattern for dynamic object creation.
✅ Use Singleton Pattern for shared resources like a database
connection.
Would you like a diagram explaining these patterns visually? 🚀