単例設計モード——怠け者式、餓漢式

1450 ワード

面接では怠け者の方が多いそうですが、実際に使う場合は餓漢の方が多いですか?
//   
//           ,                 
class Single    //    ,      ,     getInstance  ,      
{               //      
    private static Single s = null;
    private Single(){};
    public static Single getInstance()
    {
        if(s==null)
            s = new Single();
        return s;
    }
}

//   
class Single2   //    ,    
{
    private static Single2 s = new Single2();
    private Single2(){};
    public static Single2 getInstance()
    {
        return s;
    }
}