C++ Singleton Design Pattern Example

1. Definition

The Singleton Design Pattern ensures that a class has only one instance and provides a global point of access to this instance. This pattern restricts the instantiation of a class to one object and is used to provide a global point of access to the object.

2. Problem Statement

There are certain scenarios where it's essential to ensure that a class is instantiated only once. For instance, configurations, thread pools, or cache might be areas where only a single instance of a class should exist. If multiple instances are allowed, it could lead to discrepancies in application behavior.

3. Solution

The Singleton pattern solves this problem by creating a private static instance of the class within itself and provides a public method to access this instance. The constructor of the class is made private to prevent further instantiation from other classes.

4. Real-World Use Cases

1. Database connections where you want to ensure that only one connection is active.

2. Logger classes that write logs to a file and should maintain a single instance to ensure synchronized access.

3. Configuration manager which holds settings used across an application.

5. Implementation Steps

1. Declare a private static instance of the class.

2. Make the constructor of the class private.

3. Provide a public static method to get the instance of the class.

6. Implementation in C++

#include<iostream>
class Singleton {
private:
    // Private static instance of the class
    static Singleton* instance;
    // Private constructor to prevent instantiation
    Singleton() {}
public:
    // Public static method to get the instance of the class
    static Singleton* getInstance() {
        if(!instance)
            instance = new Singleton();
        return instance;
    }
    // Method for demonstration
    void showMessage() {
        std::cout << "Hello from Singleton!" << std::endl;
    }
};
// Initialize static member of class Singleton
Singleton* Singleton::instance = 0;
int main() {
    // Fetch the Singleton instance and use it
    Singleton::getInstance()->showMessage();
    return 0;
}

Output:

Hello from Singleton!

Explanation:

1. The Singleton class has a private static member instance which holds the only instance of the class.

2. The constructor of the Singleton class is private which ensures that no other class can create an instance.

3. The getInstance method checks if the instance is null, creates one if it doesn't exist, and then returns the instance.

4. The showMessage method is a simple demonstration method.

5. In the main function, the Singleton instance is fetched and used to call the showMessage method.

7. When to use?

Use the Singleton pattern when:

1. You want to ensure a class has only one instance, and it provides a global point to access it.

2. The single instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code.

3. The instance operation requires access to a static variable (e.g., counting the number of instances).


Comments