The Singleton Pattern ensures that a class has only one instance and provides a global point of access to that instance. It is useful when exactly one object is needed to coordinate actions across the system.
Here's an example implementation of the Singleton Pattern in JavaScript using an imperative programming style:
// Singleton class
class Singleton {
constructor() {
if (!Singleton.instance) {
Singleton.instance = this;
}
return Singleton.instance;
}
static getInstance() {
if (!Singleton.instance) {
Singleton.instance = new Singleton();
}
return Singleton.instance;
}
someMethod() {
console.log('Some method of the singleton instance');
}
}
// Usage
const singleton1 = Singleton.getInstance();
const singleton2 = Singleton.getInstance();
console.log(singleton1 === singleton2); // Output: true
singleton1.someMethod(); // Output: Some method of the singleton instance
- Singleton Class: The
Singleton
class has a private static variableinstance
that holds the single instance of the class. The constructor ensures that only one instance of the class is created. - getInstance Method: The
getInstance
method provides a global point of access to the singleton instance. It creates the instance if it doesn't exist and returns it. - Usage: Clients access the singleton instance using the
getInstance
method and can call methods on it as needed.
The Singleton Pattern is used in scenarios where:
- Single Instance Requirement: Only one instance of a class should exist throughout the application's lifecycle.
- Global Access Point: A single point of access is needed to a shared resource or service.
- Resource Management: Limited resources (e.g., database connections, thread pools) need to be shared among multiple parts of the system.
- Configuration Management: Providing global access to application configuration settings.
- Logger Instances: Ensuring that only one instance of a logger is used for logging throughout the application.
- Cache Management: Maintaining a single cache instance to store frequently accessed data.
The Singleton Pattern ensures that a class has only one instance and provides a global point of access to that instance. In an imperative programming style, the singleton class manages its single instance through a static method (getInstance
) and ensures that only one instance is created. This pattern is commonly used in scenarios where a single instance of a class is needed to coordinate actions across the system and to provide a shared resource or service.