一、单例模式-singleton
1、饿汉式(线程安全,调用效率高,但是不能延时加载) 简单实用
public class Singleton{
private static final Singleton singleton = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return singleton;
}
}
2.懒汉式(线程不安全,调用效率不高,但是能延时加载)
public class Singleton{
private static Singleton singleton;//未初始化
private Singleton(){}
public static Singleton getInstance(){
if(singleton == null){
singleton = new Singleton();
}
return singleton;
}
}