黑马程序员技术交流社区

标题: 单例设计模式 [打印本页]

作者: world.net    时间: 2015-6-16 21:40
标题: 单例设计模式
  1. 饿汉式
  2. class Single{
  3.         private static final Single s=new Single();
  4.         private Single(){}
  5.         public static Single getInstance(){
  6.                 return s;
  7.         }
  8. }
  9. 延迟加载懒汉式
  10. class Single{
  11.         private static final Single s=null;
  12.         private Single(){}
  13.         punlic static Single getInstance(){
  14.                 if(s==null){
  15.                         synchronized(Single.class){
  16.                                 if(s==null)
  17.                                         s= new Single();
  18.                         }
  19.                 }
  20.                 return s;
  21.         }
  22. }
复制代码





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