Junior — Middle
Kui palju vastutusi rakendab Singleton muster?
sobes.tech AI
Vastus AI-lt
The Singleton pattern implements a primary responsibility: to ensure that a class has only one instance and to provide a global access point to that instance.
In other words, Singleton is responsible for:
- Controlling the creation of the object to prevent multiple instances.
- Providing a single object for use throughout the application.
Example in Java:
public class Singleton {
private static Singleton instance;
private Singleton() { }
public static synchronized Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Thus, the pattern solves the problem of managing a single state or resource in the application.