Tuesday 13 May 2014

Singleton Design Pattern

Singleton Design Pattern:-
               This design pattern is basically to control the creation of number of object, and earlier trend was to create at max one object, but we can create fixed number of object. Generally we make constructor as private to make sure that outside world can not create object at all and providing one static method which simply returns the object and it creates object only when there is no creation before.
We can see the various implementations as below:
1)
public class MySingleton {

      private static final MySingleton mySingleton = new MySingleton();
     
      private MySingleton(){}
     
      public static MySingleton getinstance(){
            return mySingleton;
      }
}


2)

class MySingleton1 {

      private static MySingleton1 mySingleton = new MySingleton1();
      private MySingleton1(){}

      public static MySingleton1 getinstance(){
            if(null == mySingleton) {
                  mySingleton = new MySingleton1();
            }
            return mySingleton;
      }
}


3)

class MySingleton2 {

      private static MySingleton2 mySingleton;
      private MySingleton2(){}

      public static MySingleton2 getinstance(){
            synchronized(MySingleton2.class) {
                  if(null == mySingleton) {
                        mySingleton = new MySingleton2();
                  }
            }    
            return mySingleton;
      }
}


4)

class MySingleton3 {

      private static MySingleton3 mySingleton;
      private MySingleton3(){}

      public static MySingleton3 getinstance(){
            synchronized(mySingleton) {
                  if(null == mySingleton) {
                        mySingleton = new MySingleton3();
                  }
            }    
            return mySingleton;
      }
}

5)

class MySingleton4 {

      private static MySingleton4 mySingleton;
     
      public MySingleton4() throws Exception{
            if(null == mySingleton){
                  mySingleton = new MySingleton4();
            }else{
                  throw new Exception("Its a singelton.. dont expect more object to get produced");
            }
      }

      public static MySingleton4 getinstance() throws Exception{
            synchronized(mySingleton) {
                  if(null == mySingleton) {
                        mySingleton = new MySingleton4();
                  }
            }    
            return mySingleton;
      }
}

Use:-
               There are several places where it is advised to use Singleton pattern for example: Logging, Caching, Load Balancer, Configuration, Communication (IO or remote to avoid poor performance) and DB Connection-pooling.

Loopholes:-
               Before writing the Singleton class just think if you really need it and consider the below de-merits-
ü  They cause tightly coupled code
ü  They are in control of their own lifecycle
ü  You can mock or test them easily
You cannot extend them (open/closed principle)

No comments:

Post a Comment