Singleton design pattern by example

Sometime we need to instantiate a class only once through the application.Then we use simple creational design pattern
"Singleton design Pattern".Singleton design pattern widely used creational design pattern in software designing.We use singleton
design pattern to protect a class instantiate frequently.
A object instantiate only once in an application.Singleton patterns are used in logging, caches, thread pools, configuration
settings, device driver objects. For example we use singleton design pattern for some logging system like log4j. We create a instance of Logger class like this...
    
 public static Logger log = Logger.getLogger(Main.class.getName()); 
Like this in many system using this singleton pattern.
Singleton pattern contain atleast 3 mainparts.these are


1. Static member.
2. Private Constructor.
3. Static public method.

The skeleton of Sigleton design pattern like this
public class ClassName{
  //static member
  private static ClassName instance;
  //private Constructor
  private ClassName(){}
  //public method to get instance of this class
  public static ClassName getInstance()
  {
        if(instance == null)
     {
         instance = new ClassName();
     }
     return Instance;
  }
} 
Here we can see that every time if we need a instance of the ClassName class we just call ClassName.getInstance(); .We get back the created instance.
We can define Sigleton design pattern in two ways


1. Lazy Intialization.
2. Without Lazy Initialisation.
Without Lazy initialisation
public class ClassName{
  //static member
  private static ClassName instance = new ClassName();
  //private Constructor
  private ClassName(){}
  //public method to get instance of this class
  public static ClassName getInstance()
  {
     return Instance;
  }
} 
This example creates an instance of this object when application starts and return this instance when call it from elsewhere.
But if we use lazy initialization method,Threads become a big issue for this.If two threads calls getinstance() at sametime then
return object will not exact that one thread wants. So we can use a system double locking system that will be more thread-safe.
For example
    public static SingleToneSample getSingleToneSample() {
        if (singleToneSample == null) {
            synchronized (SingleToneSample.class) {
                if (singleToneSample == null) {
                    System.out.println("getSingleToneSample() OBJECT WILL CREATE");
                    singleToneSample = new SingleToneSample();
                    System.out.println("getSingleToneSample() OBJECT IS CREATED");
                }
            }
        }
        System.out.println("INSTANCE IS AVAILABLE... :P");

        return singleToneSample;
    }
Here we check a instance is null or not in 2 times.Which is almost thread-safe.I will provide details of double locking system later.For now its enough.
Thanks...Happy coding... :P Example code are available on DesignPatternExample.

References:1. http://taskinoor.wordpress.com/2011/04/18/singleton_multithreaded/
                  2.http://idiotechie.com/gang-of-four-gof-design-pattern/
                  3.java.dzone.com/articles/singleton-design-pattern-–
Reactions

Post a Comment

0 Comments