Skip to content

Latest commit

 

History

History
26 lines (21 loc) · 1015 Bytes

File metadata and controls

26 lines (21 loc) · 1015 Bytes

Singleton Pattern

Summary:

The Singleton pattern ensures a class has only one instance and provides a global point of access to that instance.

Example:

Let's say we have a configuration manager that should be accessed from various parts of our application, but we want to ensure there's only one instance.

class ConfigurationManager {
    constructor() {
        if (!ConfigurationManager.instance) {
            this.data = 'Configuration Data';
            ConfigurationManager.instance = this;
        }
        return ConfigurationManager.instance;
    }
}

// Usage
const config1 = new ConfigurationManager();
const config2 = new ConfigurationManager();

console.log(config1 === config2); // Output: true (both refer to the same instance)

In this example, the ConfigurationManager class ensures that only one instance is created. Subsequent attempts to create a new instance return the existing instance. This ensures that there's only one point of access to the configuration data.