1.
Lazy Initialization
An instance created for a class when it is required, is called as Lazy Initialization.
[java]
public class LazyInitializedSingleton
{
private static LazyInitializedSingleton instance;
private LazyInitializedSingleton()
{
}
public static LazyInitializedSingleton getInstance()
{
if(instance == null)//instance is checked
{
instance = new LazyInitializedSingleton();
}
return instance;
}
}[/java]
2.
Eager Initialization
An instance created for a class at load time, is called as Early Initialization.
[java]
public class EagerInitializedSingleton
{
private static final EagerInitializedSingleton instance = new EagerInitializedSingleton();
private EagerInitializedSingleton()
{
}
public static EagerInitializedSingleton getInstance()//returns the instance
{
return instance;
}
}
[/java]
3.
Static block Initialization
The "static member function accessor" approach will not support the subclass feature of the Singleton class.
[java]
public class StaticBlockSingleton
{
private static StaticBlockSingleton instance;
private StaticBlockSingleton(){}
static
{
try//exception may occur
{
instance = new StaticBlockSingleton();
}catch(Exception e)//exception is caught
{
throw new RuntimeException("Exception occured in creating singleton instance");
}
}
public static StaticBlockSingleton getInstance()
{
return instance;
}
}
[/java]
4.
Enum singleton
Enum value can be instantiated only once in a Java program. Since Java Enum values are globally accessible, it is easy for singleton. The drawback is that the enum type is inflexible. For example, it does not support lazy initialization.
[java]
public enum EnumSingleton//enum is a user defined data type
{
INSTANCE;
public static void doSomething()
{
}
}
[/java]
5. Thread safe Singleton
Thread-safety interferes with the performance due to the cost associated with the synchronized method. Although, it is required only for the first few threads, separate instances might be created. To avoid this overhead every time, double checked locking principle can be used. In this approach, the synchronized block is used inside the if condition block with an additional check to ensure that only one instance of the singleton class is created.
[java]
public class ThreadSafeSingleton
{
private static ThreadSafeSingleton instance;
private ThreadSafeSingleton()
{
}
public static synchronized ThreadSafeSingleton getInstance()
{
if(instance == null)//instance is checked
{
instance = new ThreadSafeSingleton();
}
return instance;
}
}
[/java]
6. Using reflection to destroy singleton pattern
Reflections are used for destroying the singleton pattern.
[java]
public class ReflectionSingletonTest
{
public static void main(String[] args)//is a main method
{
EagerSingleton instanceOne =EagerSingleton.getInstance();
EagerSingleton instanceTwo = null;
try {
Constructor c[] = EagerSingleton.class.getDeclaredConstructors();
for (Constructor constructor : c)
{
constructor.setAccessible(true);
instanceTwo = (EagerSingleton) c.newInstance();
break;
}
} catch (Exception e)
{
e.printStackTrace();
}
System.out.println(instanceOne.hashCode());
System.out.println(instanceTwo.hashCode());
}
}[/java]