//懒汉式单例类,在第一次调用时的时候实例化自己
public class Singleton{
private Singleton(){}
private static Singleton single = null;
//静态工厂方法
public static Singleton getInstance(){
if(single==null){
single = new Singleton();
}
return single;
}
}
//线程安全的懒汉式单例类,在第一次调用时的时候实例化自己
public class Singleton{
private Singleton(){}
private static Singleton single = null;
//静态工厂方法
public static synchronized Singleton getInstance(){
if(single==null){
single = new Singleton();
}
return single;
}
}
//饿汉式单例类,在类初始化时,已经自行实例化
public class Singleton1{
private Singleton1(){}
private static final Singleton1 single = new Singleton1();
//静态工厂方法
public static Singleton1 getInstance(){
return single;
}
}