黑马程序员技术交流社区

标题: 单利设计模式 懒汉式和饿汉式 [打印本页]

作者: wingtheu    时间: 2014-11-6 09:01
标题: 单利设计模式 懒汉式和饿汉式
本帖最后由 wingtheu 于 2014-11-6 09:01 编辑

//饿汉式
class Single
{
        private static Single s = new Single();
        private Single(){}
        public static Single getInstance()
        {
                      return s;
         }
}
//懒汉式
class Single
{
           private stactic Single s = null;
           private Single(){}
           public static Single getInstance()
            {
                    if(s==null)
                       {                     
                            synchronized(Single.class)
                              {
                                      if(s==null)
                                             s = new Single();
                               }

                         }
                        return s;
            }

}

作者: 下一秒温存    时间: 2014-11-6 09:35
饿货来个士力架




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2