单列模式
来源:互联网 发布:淘宝创业团队 编辑:程序博客网 时间:2024/06/07 03:56
http://www.cnblogs.com/libingql/archive/2012/04/16/2451608.html
/// <summary> /// 懒汉式 /// </summary> public class Singleton { private static Singleton instance; private Singleton () { } private static object synclock = new object(); public static Singleton GetInstance() { if (instance == null) { lock (synclock) { if (instance == null) { instance = new Singleton(); } } } return instance; } } /// <summary> /// 饿汉式 /// </summary> public sealed class Singleton2 { private Singleton2() { } private static readonly Singleton2 instance = new Singleton2(); public static Singleton2 GetInstance() { return instance; } }
阅读全文