/**
* Created by yanwen.ayw on 2016/10/14.
* desc:静态内部类实现
*/
public class Singleton {
private static Singleton ourInstance = new Singleton();
private static class InstanceHolder{
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return InstanceHolder.INSTANCE;
}
private Singleton() {
}
private int counter = 0;
}
/**
* Created by yanwen.ayw on 2016/10/14.
* desc:枚举类实现
*/
public enum EnumSingleton {
INSTANCE;
public void doAnything(){}
}
/**
* Created by yanwen.ayw on 2016/10/14.
* desc:双重校验锁实现
*/
public class DoubleCheckSingleton {
private static final String TAG = "DoubleCheckSingleton";
private static volatile DoubleCheckSingleton instance ;
private DoubleCheckSingleton(){}
public DoubleCheckSingleton getInstance() {
if(instance == null) {
synchronized (DoubleCheckSingleton.class) {
if(instance == null) {
instance = new DoubleCheckSingleton();
}
}
}
return instance;
}
}
/**
* Created by yanwen.ayw on 2016/10/14.
* desc:恶汉式实现
*/
public class HungurySingleton {
private static final String TAG = "HungurySingleton";
private static HungurySingleton instance = new HungurySingleton();
private HungurySingleton(){}
public static HungurySingleton getInstance() {
return instance;
}
}
/**
* Created by yanwen.ayw on 2016/10/14.
* desc:懒汉式
*/
public class LazySingleton {
private static final String TAG = "LazySingleton";
private static LazySingleton instance;
private LazySingleton() {}
public static LazySingleton getInstance() {
if(instance == null){
instance = new LazySingleton();
}
return instance;
}
}