简介
代码
/**
* 使用枚举单例
*
* @author liuzhihang
* @date 2018/8/17 17:34
*/
public class SingletonPattern {
private SingletonPattern() {
}
public static SingletonPattern getInstance() {
return SingleEnum.INSTANCE.getSingletonPattern();
}
private enum SingleEnum {
/**
* 单例
*/
INSTANCE;
private SingletonPattern singletonPattern;
SingleEnum() {
this.singletonPattern = new SingletonPattern();
}
public SingletonPattern getSingletonPattern() {
return singletonPattern;
}
}
}
优点
1.比双重锁检查相对简洁
2.线程安全
3.自动处理序列化
4.防止反射