Design Patterns in Python
Singleton Pattern
Ensuring only one instance of a class exists
Interview: System design interviews
Singleton
The Singleton pattern ensures a class has only one instance and provides a global point of access to it. In Python, this is commonly used for database connections, configuration managers, and logging.
Pythonic Implementations
- Module-level variable: Simplest Python singleton — modules are imported once
- __new__ override: Classic OOP approach
- Metaclass: Enforces singleton at class creation time