SINGLETON PATTERN – RIPPED

Provides only single instance and global point of access for that instance.  It falls under Creation Design Pattern category.

Pattern can be implemented for scenarios

  • Driver Class
  • Logging Class
  • Caching
  • Thread Pooling
  • Resource Pooling

Will discuss the various creation mechanics and their pros and cons

  1. Eager/Early Initialization
  2. Static Block Initialization
  3. Lazy/Double Lock Initialization
  4. Bill Pugh Method or Static Inner Class
  5. Serialization
  6. Enumeration

Eager/Early Initialization

public class Singleton {

private static volatile Singleton instance = new Singleton();

//private constructor
private Singleton() { }

public static Singleton getInstance() {
return instance; }}

Disadvantage

  • No exception handling during instance creation
  • Initialized even before it is required

Static Block Initialization

public class SingletonStatic {
private static volatile SingletonStatic instance = null;

static {
try {
instance = new SingletonStatic();
} catch (Exception e) {
e.printStackTrace();}
}

private SingletonStatic () {}

public static SingletonStatic getInstance() {
return instance;  }}

Disadvantage

  • Initialized before the requirement

Lazy and Double Locked

public class LazyDoubleLocked {
private static volatile LazyDoubleLocked instance = null;

private LazyDoubleLocked () {}

public static LazyDoubleLocked getInstance() {
if (instance == null) {
synchronized(SingletonStatic.class) {
if (instance == null ) {
instance = new LazyDoubleLocked();
} } }
return instance;
}}


Bill-Pugh/Static Inner Class – Inner class

public class BillPugh {

private static class SingleTonHolder {
private static final BillPugh instance = new BillPugh();}

public static BillPugh getInstance () {
return SingleTonHolder.instance;}}

Advantage

  • Inner class is only initialized when getInstance() method is invoked

Serialization

Deserialization always create a copy of the original object as it internally implements Deep Copy . To avoid multiple object created readResolve() method has to be overwritten

public class SingletonSerializable implements Serializable{
private static final long serialVersionUID = 1L;
private static class SingleTonStat {
private static volatile SingletonSerializable instance = new SingletonSerializable();}
public static SingletonSerializable getInstance() {
return SingleTonStat.instance;}

protected Object readResolve() throws ObjectStreamException {
return getInstance();}}


Enumeration

Enum allows only one instance of the object and since it is like global constants the objects are globally accessible.

But the drawback is that it is not flexible in object creation. The objects are created beforehand . Basically no lazy loading support

public enum EnumExample {
OBJ_INSTANCE;
public static void printStack(){
System.out.println(“Print my stack”);

private EnumExample() {}
}}

Advantage

  • Serialization by default
  • Guaranteed one instance.  Even reflection cannot create multiple instance
  • Thread Safe by default

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Cheat Sheet To JAVA Latest Technology

%d bloggers like this: