Sobes.tech
Junior — Middle

What design patterns have you applied in your code?

sobes.tech AI

Answer from AI

In my practice, I have used several common design patterns:

  • Singleton — to ensure a single instance of a class, for example, for configuration management or logging.
  • Factory Method — to create objects without specifying the exact class, which simplifies extensibility.
  • Observer — to implement event subscription, for example, in UI or when the state changes.
  • Decorator — to dynamically add functionality to objects.

Example of Singleton in Java:

public class Singleton {
    private static Singleton instance;

    private Singleton() {}

    public static synchronized Singleton getInstance() {
        if (instance == null) {
            instance = new Singleton();
        }
        return instance;
    }
}

This pattern guarantees that the class will have only one instance.