HCL Interview Question
Software Engineer / DevelopersThe Singleton design pattern addresses all of these concerns. With the Singleton design pattern you can:
1.Ensure that only one instance of a class is created
2.Provide a global point of access to the object
3.Allow multiple instances in the future without affecting a singleton class's clients
Ex Program:
public class ClassicSingleton {
private static ClassicSingleton instance = null;
protected ClassicSingleton() {
// Exists only to defeat instantiation.
}
public static ClassicSingleton getInstance() {
if(instance == null) {
instance = new ClassicSingleton();
}
return instance;
}
}
Singeton Class has only a single instance that is created (Refer Singleton Pattern- Static methods)
- madmax February 21, 2007