0% found this document useful (0 votes)
2 views

Module, Factory, Singleton

The document explains three JavaScript design patterns: Module, Factory, and Singleton. The Module Pattern encapsulates private methods and variables, the Factory Pattern creates objects without the 'new' keyword, and the Singleton Pattern ensures a single instance of a class or function. Each pattern is accompanied by examples and benefits, highlighting their use for code reusability, encapsulation, and efficient memory usage.

Uploaded by

dikshyant dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Module, Factory, Singleton

The document explains three JavaScript design patterns: Module, Factory, and Singleton. The Module Pattern encapsulates private methods and variables, the Factory Pattern creates objects without the 'new' keyword, and the Singleton Pattern ensures a single instance of a class or function. Each pattern is accompanied by examples and benefits, highlighting their use for code reusability, encapsulation, and efficient memory usage.

Uploaded by

dikshyant dash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

📌 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++;

console.log(`Count: ${count}`);

},

decrement: function () {

count--;

console.log(`Count: ${count}`);

},

getCount: function () {

return count;

},

};

})();

Counter.increment(); // Count: 1

Counter.increment(); // Count: 2

console.log(Counter.getCount()); // 2

Counter.count = 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 () {

console.log(`Hello, I am ${this.name} and I am a ${this.role}`);

},

};

const user1 = UserFactory("Dikshyant", "Admin");

const user2 = UserFactory("Alice", "Editor");

user1.greet(); // Hello, I am Dikshyant and I am an Admin

user2.greet(); // 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 = Database.getInstance();

const db2 = Database.getInstance();

console.log(db1 === db2); // true (Both are the same instance)

console.log(db1.connection); // 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? 🚀

You might also like