Problem Statement
When multiple parts of a program create their own instances of a class, it can lead to inconsistent states and wasted resources. This causes bugs that are hard to track and inefficient memory usage.
This diagram shows two clients accessing the same Singleton instance, ensuring only one object exists and is shared.
### Before Singleton (no pattern) class Logger: def __init__(self): self.logs = [] logger1 = Logger() logger2 = Logger() print(logger1 is logger2) # False ### After Singleton pattern class SingletonLogger: _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) cls._instance.logs = [] return cls._instance logger1 = SingletonLogger() logger2 = SingletonLogger() print(logger1 is logger2) # True