Sobes.tech
Junior — Middle

What design patterns have you used in your projects and how did they help solve problems?

sobes.tech AI

Answer from AI

In my projects, I have used the following design patterns:

  • Singleton — for creating a single instance of a class, for example, to manage application configuration.
  • Factory Method — for creating objects without specifying the exact class, which facilitates extending functionality.
  • Observer — for implementing a subscriber and notification system, for example, when the state of a model changes in the UI.
  • Decorator — for dynamically adding functionality to objects without changing their code.

Example of using Singleton in Java:

public class Configuration {
    private static Configuration instance;
    private Properties props;

    private Configuration() {
        props = new Properties();
        // load settings
    }

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

    public String getProperty(String key) {
        return props.getProperty(key);
    }
}

This pattern helped avoid creating multiple instances of the configuration and provided centralized access to settings.

What design patterns have you used in your projects… - sobes.tech