Junior — Middle
What design patterns have you used in your practice?
sobes.tech AI
Answer from AI
In Java practice, the following design patterns are often used:
- Singleton — ensures that a class has only one instance and provides a global point of access to it.
- Factory Method — defines an interface for creating an object, allowing subclasses to decide which class to instantiate.
- Observer — allows objects to subscribe to events of another object and receive notifications of changes.
- Decorator — dynamically adds responsibilities to objects by wrapping them.
- Strategy — defines a family of algorithms, encapsulates each one, and makes them interchangeable.
Example of Singleton:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
These patterns help write flexible, extensible, and maintainable code.