EagerSingleton.java:
package gendwang.cisco.com;
public class EagerSingleton
{
private static final EagerSingleton instance = new EagerSingleton();
private EagerSingleton()
{
}
public static EagerSingleton getInstance()
{
return instance;
}
}
LasySingleton.java:
package gendwang.cisco.com;
public class LasySingleton
{
private static LasySingleton instance;
private LasySingleton()
{
}
public static LasySingleton getInstance()
{
if(instance == null)
{
instance = new LasySingleton();
}
return instance;
}
}
ThreadSafeSingleton.java:
package gendwang.cisco.com;
public class ThreadSafeSingleton
{
private static ThreadSafeSingleton instance;
private static final Object lock = new Object();
private ThreadSafeSingleton()
{
}
public static ThreadSafeSingleton getInstance()
{
if(instance == null)
{
synchronized(lock)
{
if(instance == null)
{
instance = new ThreadSafeSingleton();
}
}
}
return instance;
}
}