标签:
单例模式1:
/// <summary> /// 单例 /// </summary> public sealed class Singleton { /// <summary> /// 锁 /// </summary> private static object padlock = new object(); /// <summary> /// 单例字段 /// </summary> private static Singleton instance = null; /// <summary> /// 私有构造函数 /// </summary> private Singleton() { } /// <summary> /// 实例 /// </summary> public static Singleton Instance { get { if (instance == null) { lock (padlock) { if (instance == null) { instance = new Singleton(); } } } return instance; } } }
单例模式2:
/// <summary> /// 单例 /// </summary> public sealed class Singleton { /// <summary> /// 实例 /// </summary> private static Singleton instance = new Singleton(); /// <summary> /// 私有构造函数 /// </summary> private Singleton() { } /// <summary> /// 实例 /// </summary> public static Singleton Instance { get { return instance; } } }
标签:
原文地址:http://www.cnblogs.com/fengjin/p/4471125.html