Java Design Pattern – Singleton Pattern
Kushal Paudyal December 12th, 2009
“In software engineering, the singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This concept is also sometimes generalized to restrict the instance to more than one object, as for example, we can restrict the number of instances to five objects. This is useful when exactly one object is needed to coordinate actions across the system. Sometimes it is generalized to systems that operate more efficiently when only one or a few objects exist. It is also considered an anti-pattern by some people, who feel that it is overly used, introducing unnecessary limitations in situations where a sole instance of a class is not actually required.” [Source Wikipedia]
In the following demo, I have tried to explain via code, how Singleton Pattern can be implemented in Java. The comments are self explanatory. I have also attached a class diagram for your easy understanding.
package com.kushal.patternsexample;
/**
* Main class for testing singleton pattern.
*
*/
public class SingletonTestMain
{
public static void main(String args [])
{
/**This creates a fresh object*
* Constructor will be called.
*/
JavaSingletonObject obj=JavaSingletonPattern.getJavaSingletonObject();
obj.printMe();
/**
* We already have object created.
* This method call wont call constructor.
*/
obj=JavaSingletonPattern.getJavaSingletonObject();
obj.printMe();
/**
* Let's destroy the method ourselves.
*/
JavaSingletonPattern.nullifyJavaSingletonObject();
/**
* Now, we try to create object again.
* This time, construction will be called, as the object is null.
*/
obj=JavaSingletonPattern.getJavaSingletonObject();
obj.printMe();
}
}
/**
* This is some kind of factory class, it creates
* objects of different classes using singleton pattern.
*/
class JavaSingletonPattern {
private static JavaSingletonObject javaSingletonObject;
/**
* The constructor is private.
*/
private JavaSingletonPattern() {
/* We can have optional code here*/
}
public static JavaSingletonObject getJavaSingletonObject() {
if (javaSingletonObject == null) {
javaSingletonObject = new JavaSingletonObject();
}
return javaSingletonObject;
}
/**Just a helper method to nullify the object ourselves.*/
public static void nullifyJavaSingletonObject()
{
javaSingletonObject=null;
}
}
/**
* @author kushalp
* This is a demo class, whose object we want to create
* using singleton pattern.
*/
class JavaSingletonObject
{
public JavaSingletonObject()
{
System.out.print("Constructor called. ");
}
public void printMe()
{
System.out.println("I Am JavaSingletonObject.");
}
}
Sanjaal.com is owned and maintained by Sanjaal Corps, Nepal. The company offers Webhosting and Domain Registration Services, IT Solutions and Business Analysis. Sanjaal.com website features H1B Visa Information, Entertainment Portal, Link Directory Service, Free Articles, Free Open Source Tutorials on Java and J2EE Platform, Digital Photography, High Resolution Picture Gallery and Free Reliable Image Hosting Services. Future plan includes Open Source Software Development Portal, Technical Solutions and Customizable Movie and Music Arena. We would be introducing data backup, data recovery, data hosting and voip solutions. Stay free from phishing – our website does not ask for your credit card and banking information. Happy Surfing!
Originally posted 2008-09-02 16:57:53.

