Singleton Design Pattern

Your One-and-Only Object

In essence, the Singleton Design Pattern acts as your city’s mayor’s office, ensuring centralized control, efficient resource management, and a single point of contact for vital functionalities within your application.

Review

  1. Singleton class creation: The class itself ensures that only one instance is ever created, often using private constructors and static access methods.
  2. Global access point: The class provides a publicly accessible method to retrieve the single instance, often called getInstance().
  3. Client interaction: Clients use the getInstance() method to access the singleton object and utilize its functionalities.
  4. Controlled state: The singleton object can maintain any necessary state, accessible only through its methods, ensuring consistency throughout the program.

Benefits

Example

class Singleton:
    _instance = None  # Class variable to hold the instance

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self, value):
        self.value = value

# Client code
singleton1 = Singleton(10)
singleton2 = Singleton(20)  # Won't create a new instance

print(singleton1.value)  # Output: 10
print(singleton2.value)  # Output: 10 (same instance)
print(singleton1 is singleton2)  # Output: True (same object)

Remember