Java单例设计模式
单例设计模式 懒汉式/饿汉式
class Single//懒汉式的(多线程)安全定义方法(面试常考)
{
private static Single s=null;
private Single(){}
public static Single getInstance()
{
if(a==null)
{
synchronized(Single.class)
{
if(s==null)
s=new Single();
}
}
return s;
}
}//懒汉式用于延迟加载
class Single//饿汉式(开发中常用)
{
private static final Single s=new Single();
private static Single getInstance()
{
return s;
}
}