Implementation of the Singleton Pattern In C++

The Singleton pattern is a creational design pattern in C++ that ensures a class has only one instance and provides a global point of access to that instance. It is used when you want to control the instantiation of a class to ensure that there’s a single instance throughout the lifetime of your application.

Below is an example implementation of the Singleton pattern in C++:

C++




#include <iostream>
 
class Singleton {
public:
    // Static method to access the singleton instance
    static Singleton& getInstance()
    {
        // If the instance doesn't exist, create it
        if (!instance) {
            instance = new Singleton();
        }
        return *instance;
    }
 
    // Public method to perform some operation
    void someOperation()
    {
        std::cout
            << "Singleton is performing some operation."
            << std::endl;
    }
 
    // Delete the copy constructor and assignment operator
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;
 
private:
    // Private constructor to prevent external instantiation
    Singleton()
    {
        std::cout << "Singleton instance created."
                  << std::endl;
    }
 
    // Private destructor to prevent external deletion
    ~Singleton()
    {
        std::cout << "Singleton instance destroyed."
                  << std::endl;
    }
 
    // Private static instance variable
    static Singleton* instance;
};
 
// Initialize the static instance variable to nullptr
Singleton* Singleton::instance = nullptr;
 
int main()
{
    // Access the Singleton instance
    Singleton& singleton = Singleton::getInstance();
 
    // Use the Singleton instance
    singleton.someOperation();
 
    // Attempting to create another instance will not work
    // Singleton anotherInstance; // This line would not
    // compile
 
    return 0;
}


Output

Singleton instance created.
Singleton is performing some operation.






Below is the explanation of the above code:

  • We have a Singleton class with a private constructor and a private destructor, ensuring that the class can only be instantiated and destroyed from within the class itself.
  • The getInstance method is static and provides access to the single instance of the Singleton class. It uses lazy initialization, meaning it creates the instance only when getInstance is called for the first time. Subsequent calls return the existing instance.
  • We delete the copy constructor and assignment operator to prevent copying of the Singleton instance, ensuring there’s only one instance.
  • In the main function, we demonstrate how to access and use the Singleton instance. Attempting to create another instance using the regular constructor is prevented because the constructor is private.

When you run this program, you’ll see that only one instance of the Singleton is created, and attempting to create another instance is prevented. The Singleton instance is accessed through the getInstance method, making it globally accessible within your codebase.

Singleton Pattern | C++ Design Patterns

A singleton pattern is a design pattern that ensures that only one instance of a class can exist in the entire program. This means that if you try to create another instance of the class, it will return the same instance that was created earlier.

The Singleton pattern is useful when we need to have only one instance of a class, for example, a single database connection shared by multiple objects as creating a separate database connection for every object may be costly.

Important Topics for the Singleton Pattern in C++ Design Patterns

  • Implementation of the Singleton Pattern In C++
  • Diagram explaining the Singleton Pattern in C++
  • Advantages of the Singleton Pattern in C++ Design Patterns
  • Disadvantages of the Singleton Pattern in C++ Design Patterns
  • Uses of the Singleton Pattern

Similar Reads

Implementation of the Singleton Pattern In C++

The Singleton pattern is a creational design pattern in C++ that ensures a class has only one instance and provides a global point of access to that instance. It is used when you want to control the instantiation of a class to ensure that there’s a single instance throughout the lifetime of your application....

Diagram explaining the Singleton Pattern in C++

...

Advantages of the Singleton Pattern in C++ Design Patterns

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

Disadvantages of the Singleton Pattern in C++ Design Patterns

Here are the advantages of the Singleton pattern in C++:...

Uses of the Singleton Pattern

Here are some of the disadvantages of using the Singleton pattern in C++:...

Contact Us