用枚举实现signleton模式
传统方式
public enum Singleton {
INSTANCE {
public void test(){
System.out.println("just a enum singleton test");
}
};
public abstract void test();
}
public class MainClass {
public static void main(String[] args){
Singleton.INSTANCE.test();
}
}
传统方式
public class Singleton {
private Singleton() {}
private static final class INSTANCE{
static final Singleton instance = new Singleton();
}
public static Singleton getInstance() {
return INSTANCE.instance;
}
}