Middle
What are design patterns? Which ones do you know and have used?
sobes.tech AI
Answer from AI
Design patterns are proven solutions to common problems in software development. They help create more flexible, maintainable, and understandable code.
Main categories of patterns:
- Creational (e.g., Singleton, Factory, Builder)
- Structural (Adapter, Decorator, Composite)
- Behavioral (Observer, Strategy, Command)
In Python, I often used:
- Singleton to restrict the creation of a single class instance.
- Factory to create objects with different configurations.
- Decorator for dynamically extending the functionality of functions or classes.
- Observer to implement event-driven models.
Example of Singleton in Python:
class Singleton:
_instance = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance