Singleton pattern used when we want to allow only a single instance of a class can be created inside our application. Using this pattern ensures that a class only have a single instance by protecting the class creation process, by setting the class constructor into private access modifier. To get the class instance, the singleton class can provide a method for example a getInstance() method, this will be the only method that can be accessed to get the instance. There are some rules that need to be followed when we want to implement a singleton.
public class SingletonPattern
{
private static SingletonPattern instance;
private SingletonPattern()
{
}
public static synchronized SingletonPattern getInstance()
{
if (instance == null)
{
instance = new SingletonPattern();
}
return instance;
}
}
Click Here to See Answer .....
Subscribe to my RSS feed and get more JAVA Question, and Guideline, Plus a lot more great advice to help your Software Career.
0 comments:
Post a Comment