Junior — Middle
Can you tell us about the most frequently used design pattern?
sobes.tech AI
Answer from AI
One of the most commonly used design patterns is Singleton.
It guarantees that a class has only one instance and provides a global point of access to it. This is useful when you need to control access to a resource, such as configuration, logger, or connection pool.
Example in Java:
public class Singleton {
private static Singleton instance;
private Singleton() {
// private constructor prevents instantiation from outside
}
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
This pattern is simple and widely applicable, but requires caution in multithreaded environments and during testing.