Sobes.tech
Junior — Middle

Can you show an example where design patterns are used in code?

sobes.tech AI

Answer from AI

Example of using the Singleton design pattern in Python — when you need to ensure that a class has only one instance:

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, both objects are the same instance

This pattern is useful, for example, for managing database connections or application configuration.

Can you show an example where design patterns are… - sobes.tech