Singleton Design Pattern — Basic Implementation
Singleton Design Pattern in C++: A Simple Explanation
The Singleton Design Pattern is a popular and commonly used design pattern in software development. It is used to ensure that a class has only one instance and that there is a global point of access to that instance. The Singleton Design Pattern is commonly used for tasks such as logging, caching, and managing database connections or a file system.
In this article, we will explore how to implement the Singleton Design Pattern in C++. We will start by discussing the basic concept of the pattern, and then we will dive into the code to see how it works in practice.
In C++, the Singleton pattern can be implemented in various ways, but the most common approaches are using a static member variable or the Meyers Singleton approach. We’ll see static member variable approach in this article.
Steps to implement Singleton Design Pattern:
The Singleton class must have the following features:
- A private constructor, which prevents the creation of objects of the class from outside the class.
- A private static variable that holds the only instance of the class.
- A public static method that provides access to the single instance of the class.
- Delete the copy and move constructors and the assignment operator
Now that we understand the basic concept of the Singleton Design Pattern, let’s see how it can be implemented in C++.
Implementing the Singleton Design Pattern in C++:
Get CodeDesignHub’s stories in your inbox
Join Medium for free to get updates from this writer.
To implement the Singleton Design Pattern in C++, we will create a class that has a private constructor, a private static variable that holds the only instance of the class, and a public static method that provides access to the single instance of the class.
Class Diagram:
Source Code:
Here’s an example of how this can be done:
class Singleton
{
private:
static Singleton* instance;
Singleton() {} // Private constructor
public:
static Singleton* getInstance() { // Public static method
if (instance == nullptr) { // Check if instance is null
instance = new Singleton(); // Create new instance if null
}
return instance; // Return instance
}
// Delete the copy and move constructors and the assignment operator
Singleton(const Singleton&) = delete;
Singleton& operator=(const Singleton&) = delete;
Singleton(Singleton&&) = delete;
Singleton& operator=(Singleton&&) = delete;
};
Singleton* Singleton::instance = nullptr; // Initialize static variable
int main()
{
Singleton* inst1 = Singleton::getInstance();
Singleton* inst2 = Singleton::getInstance();
if (inst1 == inst2 ) {
std::cout << "Both instances are the same" << std::endl;
}
else {
std::cout << "Instances are different" << std::endl;
}
return 0;
}In this code, the Singleton class has a private constructor that prevents the creation of objects of the class from outside the class. The class also has a private static variable instance that holds the only instance of the class.
The copy and move constructors, as well as the assignment operator, are deleted to prevent copying or moving the Singleton instance.
The public static method getInstance() provides access to the single instance of the class. This method checks whether instance is null and creates a new instance if it is. If instance is not null, the method simply returns the existing instance.
In the main() function, we create two instances of the Singleton class using the getInstance() method. We then check whether these instances are the same using a simple if-else statement.
Conclusion
The Singleton Design Pattern is a simple and powerful design pattern that ensures that only one instance of a class is created and that there is a global point of access to that instance. In this article, we discussed the basic concept of the pattern and how it can be implemented in C++.
Note:
However, this implementation has a potential issue related to the static initialization order fiasco, which can occur when the Singleton instance is used before its static member variable is initialized. To avoid this issue, another approach can be used, known as the Meyers Singleton approach. We’ll discuss about this in the next article.





