Skip to content

Latest commit

 

History

History
62 lines (44 loc) · 2.71 KB

singleton-pattern-creational-imperative.md

File metadata and controls

62 lines (44 loc) · 2.71 KB

Singleton Pattern in JavaScript (Imperative Style)

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.

Example Code

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

Explanation

  1. Singleton Class: The Singleton class has a private static variable instance that holds the single instance of the class. The constructor ensures that only one instance of the class is created.
  2. 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.
  3. Usage: Clients access the singleton instance using the getInstance method and can call methods on it as needed.

Usage

The Singleton Pattern is used in scenarios where:

  1. Single Instance Requirement: Only one instance of a class should exist throughout the application's lifecycle.
  2. Global Access Point: A single point of access is needed to a shared resource or service.
  3. Resource Management: Limited resources (e.g., database connections, thread pools) need to be shared among multiple parts of the system.

Common Use Cases

  • 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.

Summary

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.