Singleton Pattern
The Singleton pattern is a creational design pattern that restricts the instantiation of a class to one single object. In JavaScript, this pattern is especially useful when you need to coordinate actions across the system using a single shared resource. Common use cases include managing a single database connection; handling configuration settings; or maintaining a single logging instance. By ensuring that only one instance exists, you avoid issues such as conflicting states or unnecessary resource consumption.
1234567891011121314151617181920212223242526// Simple Singleton implementation using an object const Singleton = (function() { let instance; function createInstance() { return { message: "I am the only instance!" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })(); // Usage const singletonA = Singleton.getInstance(); const singletonB = Singleton.getInstance(); console.log(singletonA === singletonB); // true console.log(singletonA.message); // "I am the only instance!"
This Singleton implementation works by using a closure to keep the instance variable private. When you call getInstance(), it checks if an instance already exists. If not, it creates one and stores it. Any subsequent calls to getInstance() will return the same object. This ensures that throughout your code, there is only one instance of the Singleton, and any changes to it are shared everywhere it is accessed.
1. What is the main purpose of the Singleton pattern?
2. In JavaScript, a Singleton is often implemented using:
Tack för dina kommentarer!
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 7.14
Singleton Pattern
Svep för att visa menyn
The Singleton pattern is a creational design pattern that restricts the instantiation of a class to one single object. In JavaScript, this pattern is especially useful when you need to coordinate actions across the system using a single shared resource. Common use cases include managing a single database connection; handling configuration settings; or maintaining a single logging instance. By ensuring that only one instance exists, you avoid issues such as conflicting states or unnecessary resource consumption.
1234567891011121314151617181920212223242526// Simple Singleton implementation using an object const Singleton = (function() { let instance; function createInstance() { return { message: "I am the only instance!" }; } return { getInstance: function() { if (!instance) { instance = createInstance(); } return instance; } }; })(); // Usage const singletonA = Singleton.getInstance(); const singletonB = Singleton.getInstance(); console.log(singletonA === singletonB); // true console.log(singletonA.message); // "I am the only instance!"
This Singleton implementation works by using a closure to keep the instance variable private. When you call getInstance(), it checks if an instance already exists. If not, it creates one and stores it. Any subsequent calls to getInstance() will return the same object. This ensures that throughout your code, there is only one instance of the Singleton, and any changes to it are shared everywhere it is accessed.
1. What is the main purpose of the Singleton pattern?
2. In JavaScript, a Singleton is often implemented using:
Tack för dina kommentarer!