C#小笔记:单例模式
双重锁定:
public class Singleton { private static Singleton instance; private static readonly object syncRoot = new object(); private Singleton() {} public static Singleton GetInstance() { if(instance == null) { lock(syncRoot) { if(instance == null) { instance = new Singleton(); } } } return instance; } //Operation funtion() }
静态初始化:
public sealed class Singleton { private static readonly Singleton instance = new Singleton(); private Singleton(){} public static Singleton GetInstance() { return instance; } //Operation funtion() }
更多信息请参考:大话设计模式!