饿汉式:
public class singleton1 {
private singleton1(){}
private static singleton1 sg1=new singleton1();
public static singleton1 getInstance()
{
return sg1;
}
}
懒汉式:
public class singleton2 {
private singleton2(){}
private static singleton2 sg2=null;
public static singleton2 getInstance()
{
if(sg2==null)
sg2=new singleton2();
return sg2;
}
} |
|