This pattern want to make sure that there will be only one instance of the class and you provide access to it. For this we make the default constructor private and also we create a static object of Singleton that we initialize in the method getInstance() or we return it if it has been already initialized.
public class Singleton
{
private Singleton(){}
private static Singleton instance;
public static Singleton getInstance()
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
No comments:
Post a Comment