彩虹天堂
技术源于生活
posts - 0,  comments - 2,  trackbacks - 0

这个Bob Lee可是相当不简单的说……

他写的一个Singleton模式的写法前不久还得到了OnJava编辑的推荐呢:

代码
  1. public class Singleton {   
  2.   
  3.   static class SingletonHolder {   
  4.     static Singleton instance = new Singleton();   
  5.   }   
  6.   
  7.   public static Singleton getInstance() {   
  8.     return SingletonHolder.instance;   
  9.   }   
  10.   
  11. }  

在加载singleton时并不加载它的内部类SingletonHolder,而在调用getInstance()时调用SingletonHolder时才加载SingletonHolder,从而调用singleton的构造函数,实力化singleton,从而达到lazy loading的效果。

posted on 2008-05-05 22:13 bcterry 阅读(415) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 

<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿

文章档案

搜索

  •  

最新评论