笨小孩做开发

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
public class Singleton { private static Singleton _instance = null; private Singleton(){} public static Singleton CreateInstance() { if(_instance == null) { _instance = new Singleton(); } return _instance; } } 第二种考虑了线程安全,不过有点烦,但绝对是正规写法,经典的一叉 public class Singleton { private volatile static Singleton _instance = null; private static readonly object lockHelper = new object(); private Singleton(){} public static Singleton CreateInstance() { if(_instance == null) { lock(lockHelper) { if(_instance == null) _instance = new Singleton(); } } return _instance; } } 第三种可能是C#这样的高级语言特有的,实在懒得出奇 public class Singleton { private Singleton(){} public static readonly Singleton instance = new Singleton(); } 哦,shit!
posted on 2015-08-27 13:56  笨小孩做开发  阅读(231)  评论(0编辑  收藏  举报