Saturday, October 03, 2009

Ways by which singleton can be created

Singleton classes can be created in 3 ways

Case 1:

package org.vijayan.sample;

public class Singleton {

/**
* Advanced Initialization
*/
public static final Singleton SINGLETON=new Singleton();
private Singleton(){
}

public static void main(String[] args) {
for(int i=0;i<4;i++){
System.out.println(Singleton.SINGLETON);
}
}
}


In this case the statically created object is made final and publicly exposed to everyone.

Case 2:


package org.vijayan.sample;

public class Singleton {

/**
* Advanced Initialization
*/
private static Singleton singleton=new Singleton();
private Singleton(){
}
public static Singleton getInstance(){
return singleton;
}
public static void main(String[] args) {
for(int i=0;i<4;i++){
System.out.println(Singleton.getInstance());
}
}
}


Case 3:


package org.vijayan.sample;

public class Singleton {

/**
* Lazy Initialization
*/
private static Singleton singleton=null;
private Singleton(){
}

public static Singleton getInstance(){
if(singleton==null){
singleton=new Singleton();
}
return singleton;
}

public static void main(String[] args) {
for(int i=0;i<4;i++){
System.out.println(Singleton.getInstance());
}
}
}


In this third case is preferable because it creates the object on-demand. But it can create issue when it is used in multi threaded environment.

So the code can be changed to case 4

Case 4:


package org.vijayan.sample;

public class Singleton {

/**
* Lazy Initialization
*/
private static Singleton singleton=null;
private Singleton(){
}
/**
*
* Will take care multi threaded environment but it expensive
*/
public static synchronized Singleton getInstance(){
if(singleton==null){
singleton=new Singleton();
}
return singleton;
}
public static void main(String[] args) {
for(int i=0;i<4;i++){
System.out.println(Singleton.getInstance());
}
}
}


Though it takes care multi threaded this will become a costly method. to make it a light-weight we can change the code to case 5

Case 5:


package org.vijayan.sample;

public class Singleton {

/**
* Lazy Initialization
*/
private static Singleton singleton=null;
private Singleton(){
}
/**
*
* Will take care multithreaded environment also less expensive
*/
public static Singleton getInstance(){
if(singleton==null){
synchronized (Singleton.class) {
if(singleton==null){
singleton=new Singleton();
}
}
}
return singleton;
}

public static void main(String[] args) {
for(int i=0;i<4;i++){
System.out.println(Singleton.getInstance());
}
}

}


This method is an efficient but complex one, this is also called as double-null checking method. more info on this can be found in http://en.wikipedia.org/wiki/Double-checked_locking

Output:

org.vijayan.sample.Singleton@10b62c9
org.vijayan.sample.Singleton@10b62c9
org.vijayan.sample.Singleton@10b62c9
org.vijayan.sample.Singleton@10b62c9

Please note all 4 times it returns the same object reference.

No comments: