Sobes.tech
Junior — Middle

What design patterns do you apply in your development?

sobes.tech AI

Answer from AI

In my Python development, I often use the following design patterns:

  • Singleton — for creating a single instance of a class, for example, for configuration or logging.
  • Factory Method — for creating objects with different implementations through a common interface.
  • Decorator — for dynamically extending the functionality of objects without changing their code.
  • Observer — for implementing event subscription and notifying listeners.

Example of a simple Singleton in Python:

class Singleton:
    _instance = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super().__new__(cls)
        return cls._instance

# Usage
s1 = Singleton()
s2 = Singleton()
print(s1 is s2)  # True

The choice of pattern depends on the task and the architecture of the project.