单例模式
package com.quan.test;
/**
* @author : xiaoquan
* create at: 2020/3/30 10:10 下午
* @description: 单例模式
*/
public class Single {
//懒汉式(比较懒需要时在创建)
private static Single instance;
private Single(){}
public static Single getInstance(){
if (instance == null){
instance = new Single();
}
return instance;
}
}
class EHanSingle{
//饿汉式(非常饥饿,先创建好)
private static EHanSingle instance = new EHanSingle();
private EHanSingle(){}
public static EHanSingle getInstance(){
return instance;
}
}
enum EnumSingle{
//枚举式
INSTANCE;
public static EnumSingle getInstance(){
return INSTANCE;
}
}