Singleton设计模式
2014-12-03 17:54
239 查看
实现方法:
使用方法:
/// <summary> /// 单实例设计模式 /// </summary> /// <typeparam name="T">需要实例化的类</typeparam> public class SingleTon<T> where T : class, new() { protected SingleTon() { } private static T _instance; private static readonly object _syncRoot = new object(); /// <summary> /// 获取当前实例 /// </summary> public static T Instance { get { if (_instance == null) { lock (_syncRoot) { //Double-Check双重检查锁定,对于多线程访问时控制 if (_instance == null) { _instance = new T(); } } } return _instance; } } }
使用方法:
public class BusinessManager : SingleTon<BusinessManager> { public void SayHello() { Console.WriteLine("Hello"); } }方法调用:
BusinessManager.Instance.SayHello();
相关文章推荐
- 设计模式袖珍版 连续转载之 - Singleton(单态)
- 设计模式笔记之 - Singleton & Monostate
- 设计模式单件(Singleton)---对象创建型模式
- Java设计模式:Singleton(单态模式)
- 设计模式---单子(Singleton)
- Singleton设计模式的C#实现(下)
- 设计模式之单件(Singleton)的应用
- 设计模式-->Singleton(单例模式)
- 在Java中应用设计模式--Singleton
- java设计模式之创建模式--singleton
- singleton设计模式的C#实现(上)
- 设计模式入门(1)------单件(Singleton)
- C++设计模式:Singleton的模板实现之一
- java设计模式之创建模式--singleton (demo)
- [用设计模式锻炼C#系列]之Singleton、Bridge
- 设计模式(4)-单例模式(Singleton)
- 设计模式之Singleton(单态)
- 设计模式之Singleton模式
- 设计模式、用Delphi实现---->Singleton 模式
- 从C#的Singleton设计模式实现看.NET Framework特性对开发者的重要性