Junior — Middle
What design patterns are used in Java for application development?
sobes.tech AI
Answer from AI
In Java, the following design patterns are often used for application development:
- Singleton — to ensure a class has only one instance.
- Factory Method — to create objects without specifying the exact class.
- Observer — to implement subscription and notification of changes.
- Decorator — to dynamically add functionality to objects.
- MVC (Model-View-Controller) — to separate logic, presentation, and control.
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;
}
}