饿汉式
class Single
{
private Single(){}
private static Single s=new Single();
public static Single getInstance()
{
return s;
}
}
懒汉式
class Single
{
private Single(){}
private static Single s=null;
public static Single getInstance()
{
if(s==null)
s=new Single();
return s;
}
饿汉式:先初始化对象
懒汉式:判断后再初始化对象,相比饿汉式效率低一些,还容易出错 |
|