Java Singleton Design Pattern

11 08 2014
  • we can create singleton in different ways depending on the requirement/situation.
  • In Java Singleton Pattern is used to create only one instance of the particular class in the application.

 

  • Singleton Eager Initialization
  • This Approach object will initialize in the starting of the application itself.
  • so unnecessary object will be initialized even if we are not using that singleton class

/**
* @author TechSivam
* This class is for Singleton Eager Initialization Example
*/
public class SingletonEagerInitialization {

// static final member variable
private static final SingletonEagerInitialization INSTANCE = new SingletonEagerInitialization();

// private constructor
private SingletonEagerInitialization() {
}
// public static method to get the instance.
public static SingletonEagerInitialization getInstance() {
return INSTANCE;
}
}

  • Singleton Lazy Initialization
  • in this approach when we use this singleton class , only that time it will create object, otherwise it will not create object.

/**
* @author TechSivam
* This class is for Singleton Lazy Initialization Example
*/
public class SingletonLazyInitialization {
// static member variable
private static SingletonLazyInitialization INSTANCE;

// private constructor
private SingletonLazyInitialization() {
}

// public static method to get the instance.
public static SingletonLazyInitialization getInstance() {
if (INSTANCE == null) {
INSTANCE = new SingletonLazyInitialization();
}
return INSTANCE;
}
}

  • Singleton Thread Safe
  • in this approach when we use this singleton class for multi threading environment , only one instance will create.
  • we need to synchronized the get method.

/**
* @author TechSivam
* This class is for Singleton Thread Safe Example
*/
public class SingletonThreadSafe {
// static member variable
private static SingletonThreadSafe INSTANCE;

// private constructor
private SingletonThreadSafe() {
}

// public synchronized static method to get the instance.
public synchronized static SingletonThreadSafe getInstance() {
if (INSTANCE == null) {
INSTANCE = new SingletonThreadSafe();
}
return INSTANCE;
}
}

  • Singleton with Serializable
  • when we are using networking application, we are passing serialized object through network we need to use this approach

 

/**
* @author TechSivam
* This class is for Singleton with Serializable Example
*/
public class SingletonWithSerializable implements Serializable {
private static final long serialVersionUID = -5669370075648474485L;
// static member variable
private static SingletonWithSerializable INSTANCE;

// private constructor
private SingletonWithSerializable() {
}

// public static method to get the instance.
public static SingletonWithSerializable getInstance() {
if (INSTANCE == null) {
INSTANCE = new SingletonWithSerializable();
}
return INSTANCE;
}
}

  • Singleton Enum
  • by default its thread safe

/**
* @author TechSivam
* This is Enum class, by default it is singleton and thread safe
*/
public enum SingletonEnum {

INSTANCE;

public static void doSomething(){
//do something
}
}

Advertisements







%d bloggers like this: