Middle+
Tell me about design patterns. What do you know?
sobes.tech AI
Answer from AI
Design patterns are proven solutions to common problems in software development. They help create more flexible, maintainable, and extendable code.
Main categories of patterns:
- Creational — manage object creation. For example, Singleton, Factory Method, Abstract Factory.
- Structural — define how classes and objects are combined to form larger structures. For example, Adapter, Decorator, Composite.
- Behavioral — describe interactions between objects. For example, Observer, Strategy, Command.
Example of Singleton pattern in Python:
class Singleton:
_instance = None
def __new__(cls, *args, **kwargs):
if not cls._instance:
cls._instance = super().__new__(cls)
return cls._instance
s1 = Singleton()
s2 = Singleton()
print(s1 is s2) # True
Understanding patterns helps write higher quality code and communicate effectively with the team.