从来也没专门学习研究过设计模式,但其实以往经历的项目中几乎无处不在地包含着各种设计模式的概念。久而久之,我虽然不知道各种设计模式的具体概念,但其实遇到具体问题的时候,我也会做出相应的设计,包括使用各种我“不知道”的设计模式,这种“模仿能力”我觉得就是所谓的经验。
单例模式是一种非常常用的设计模式,通过单例模式可以保证系统中一个类只有一个实例而且该实例易于外界访问,从而方便对实例个数的控制并节约系统资源。通常是这样的实现:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Singleton { #region [Singleton Instance] /// <summary> /// The singleton instance of Singleton. /// </summary> private static Singleton m_Instance; /// <summary> /// Get the singleton object. /// </summary> public static Singleton Default { get { return m_Instance ?? (m_Instance = new Singleton()); } } #endregion #region [Constructor] /// <summary> /// Initialize a new instance of <see cref="Singleton"/>. /// </summary> private Singleton() { } #endregion }
以上