Design Patterns in Python
Design patterns are reusable solutions to common software problems. They improve code readability and maintainability.
Singleton Pattern
class Singleton:
_instance = None
def __new__(cls):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
Factory Pattern
def factory(shape):
if shape == "circle":
return "Circle created"
Observer Pattern
✔ Used in event systems
✔ GUI frameworks
✔ Messaging apps
✔ GUI frameworks
✔ Messaging apps
📝 Practice: Implement a simple Factory pattern.