Explanation
- The **Singleton Pattern** is a creational design pattern that ensures a class has only one instance, and provides a global point of access to that instance.
- **"Single Instance"**: The Singleton ensures that only one object of the class is created throughout the application.
- **Use Case**: Useful when you need to control access to shared resources, like database connections or logging.
-
Steps
- Make the constructor of the class private to prevent direct instantiation.
- Provide a static method that will return the single instance of the class.
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super(Singleton, cls).__new__(cls)
return cls._instance
# Singleton usage
singleton1 = Singleton()
singleton2 = Singleton()
print(singleton1 is singleton2) # Output: True (Both are the same instance)