Singleton Pattern | C++ Design Patterns

Last Updated : 18 May, 2026

The Singleton Pattern is a creational design pattern that ensures only one instance of a class exists throughout the application. It also provides a global access point to that instance, helping maintain consistency and controlled resource usage.

  • Used when only one object is required across the system, such as a logger, configuration manager, or cache manager.
  • Helps avoid the overhead of creating multiple expensive objects like database connections or thread pools.
C++
#include <iostream>

class Singleton {
private:
    static Singleton* instance;

    // Private constructor
    Singleton() {
        std::cout << "Singleton instance created.\n";
    }

    // Delete copy constructor and assignment
    Singleton(const Singleton&) = delete;
    Singleton& operator=(const Singleton&) = delete;

public:
    static Singleton* getInstance() {
        if (instance == nullptr)
            instance = new Singleton();
        return instance;
    }

    void showMessage() {
        std::cout << "Hello from Singleton!" << std::endl;
    }
};

Singleton* Singleton::instance = nullptr;

int main() {
    Singleton* s1 = Singleton::getInstance();
    s1->showMessage();

    Singleton* s2 = Singleton::getInstance();
    std::cout << (s1 == s2); // Will print 1 (true)
    return 0;
}

Output
Singleton instance created.
Hello from Singleton!
1
  • The class has a private constructor and a static pointer to ensure only one instance is created.
  • The getInstance() method checks if the instance exists; if not, it creates and returns it.
  • Copy constructor and assignment operator are deleted to prevent copying the Singleton instance.

Advantages

The Singleton Pattern provides controlled access to a single shared instance and helps optimize resource usage across the application.

  • Single Instance: Ensures that only one object of a class exists throughout the application.
  • Global Access: Provides a centralized and easy access point to the instance.
  • Lazy Initialization: Creates the object only when it is first needed, improving efficiency.
  • Efficient Resource Use: Reduces memory and resource consumption by avoiding multiple instances.
  • Avoids Global Variables: Offers a cleaner and safer alternative to using global variables.

Limitations of Singleton Pattern

Although the Singleton Pattern is useful for controlling object creation, it also has several limitations and should be used carefully.

  • Global State Problem: Since a singleton object is globally accessible, it can create hidden dependencies between different parts of the application.
  • Tight Coupling: Classes that directly depend on a singleton become tightly coupled, making the code harder to modify, maintain, or extend.
  • Difficult Unit Testing: Singleton objects are difficult to mock or replace during unit testing, which can reduce test flexibility and isolation.
  • Thread Safety Complexity: In multi-threaded applications, additional synchronization is required to ensure only one instance is created safely.
  • Reduced Flexibility: Singleton restricts object creation, which can make future design changes more difficult.

Common Use Cases

The Singleton Pattern is commonly used for managing shared resources or centralized services.

  • Logger: Maintains a single logging instance across the application.
  • Database Connection Manager: Controls and manages database connections efficiently.
  • Configuration Manager: Provides centralized access to application settings.
  • Cache Manager: Manages shared cached data for faster access.
  • Thread Pool: Controls reusable threads for concurrent task execution.
  • Game State Manager: Maintains a consistent game state throughout execution.
  • Event Dispatcher: Handles centralized event registration and notification.
  • Print Spooler: Manages print jobs using a single coordinated service.
  • Resource Manager: Controls access to shared system resources.
Comment

Explore