Unknown Date

Singleton Pattern

Singleton Pattern

The Singleton Pattern is a creational design pattern that ensures a class has only one instance and provides a global point of access to that instance. This pattern is particularly useful when exactly one object is needed to coordinate actions across the system, such as managing a shared resource or controlling access to a configuration.

Key features of the Singleton Pattern:

  • single instance

  • global access point

  • lazy initialization

  • private constructor

Single Instance

  • The Singleton Pattern ensures that a class has only one instance, and it provides a mechanism to access that instance.

Global Access Point

  • The singleton instance is typically accessed through a global point of access, often referred to as the Singleton instance or method.

Lazy Initialization

  • The Singleton instance is created only when it is requested for the first time. This is known as lazy initialization and helps in optimizing resource usage.

Private Constructor

  • The singleton class usually has a private constructor to prevent direct instantiation of multiple instances.

  • class Singleton:
    _instance = None # Class variable to store the singleton instance

    class Singleton {
        private static instance: Singleton | null = null;
    
        private constructor() {}
    
        public static getInstance(): Singleton {
            if (!Singleton.instance) {
                Singleton.instance = new Singleton();
            }
            return Singleton.instance;
        }
    
        public showMessage(): void {
            console.log("Hello, I am a Singleton instance!");
        }
    }
    
    // Proof of Singleton presence
    const singleton1 = Singleton.getInstance();
    const singleton2 = Singleton.getInstance();
    
    console.log(singleton1 === singleton2); // Output: true
    singleton1.showMessage(); // Output: Hello, I am a Singleton instance!
    

Example

In this example, Singleton.getInstance() method is used to get the instance of the Singleton class. The singleton1 and singleton2 variables hold references to the same instance, demonstrating that only one instance of the Singleton class exists.

Pros and Cons

Pros:

  1. Single Instance: Ensures that a class has only one instance and provides a global point of access to that instance.

  2. Lazy Initialization: The instance is not created until it is first requested, which saves memory and resources.

  3. Global Access: Provides a global point of access to the instance, making it easy to access from anywhere in the application.

  4. Thread Safety: Can be implemented to provide thread-safe access to the singleton instance.

Cons:

  1. Global State: Can introduce a global state in the application, making it harder to track and debug.

  2. Overuse: Singleton pattern can be overused, leading to tight coupling and reduced flexibility.

  3. Testing: Can be difficult to unit test due to the global state.

← Back to Library