Singleton是软件设计开发中很常用到的一种设计模式。在C#语言中实现Singleton的常见方法有下面一些,关于更详细的信息可以参阅《细颗粒度Singleton模式实现》一文。
public class Singleton
{
private static Singleton instance; // 唯一实例
protected Singleton() { } // 封闭客户程序的直接实例化
public static Singleton Instance
{
get
{
if (instance == null)
instance = new Singleton();
return instance;
}
}
}
public class Singleton
{
protected Singleton() { }
private static volatile Singleton instance = null;
/// Lazy方式创建唯一实例的过程
public static Singleton Instance()
{
if (instance == null) // 外层if
lock (typeof(Singleton)) // 多线程中共享资源同步
if (instance == null) // 内层if
instance = new Singleton();
return instance;
}
}
class Singleton
{
private Singleton() { }
public static readonly Singleton Instance = new Singleton();
}
[ThreadStatic]
public static readonly Singleton Instance = new Singleton();
public class Singleton
{
protected Singleton() { }
// Return an instance of Singleton
public static Singleton Instance { get { return SingletonCreator.Instance; } }
private sealed class SingletonCreator
{
// Retrieve a single instance of a Singleton
private static readonly Singleton s_instance = new Singleton();
// Return an instance of the class
public static Singleton Instance { get { return s_instance; } }
}
}
#region StaticSingleton
/// <summary>
/// Implementing the Singleton Pattern in C#
/// Thread-safety without using locks
/// 所管理的对象将在第一时间由框架自动初始化并持续。
/// </summary>
/// <ref>http://www.yoda.arachsys.com/csharp/singleton.html</ref>
/// <example><code>
/// class Demo
/// {
/// public static Demo Instance { get { return StaticSingleton<Demo>.Instance; } }
/// }
/// </code></example>
public sealed class StaticSingleton<T> where T : class, new()
{
/// <remarks>Explicit static constructor to tell C# compiler not to mark type as beforfieldinit.</remarks>
static StaticSingleton() { }
private static readonly T instance = new T();
public static T Instance { get { return instance; } }
}
#endregion
#region LazySingleton
/// <summary>
/// Implementing the Singleton Pattern in C#
/// Thread-safety using double-check locking
/// 所管理对象的初始化和释放用户可控
/// </summary>
/// <example><code>
/// class Demo
/// {
/// private static readonly LazySingleton<Demo> singleton = new LazySingleton<Demo>(() => new Demo());
/// public static Demo Instance { get { return singleton.GetInstance(); } }
/// private Demo(){}
/// }
/// </code></example>
public sealed class LazySingleton<T> where T : class
{
private Func<T> ctor;
private T instance;
private readonly object instanceLock = new object();
public LazySingleton(Func<T> factoryMethod) { ctor = factoryMethod; }
/// <summary>确保得到对象单件实例,绝不会返回null</summary>
public T GetInstance()
{
if (instance == null)
{
lock (instanceLock)
{
if (instance == null)
NewInstance();
}
}
Debug.Assert(instance != null);
return instance;
}
/// <summary>尝试得到对象单件实例,若尚未构造则返回null</summary>
public T TryGetInstance() { return instance; }
/// <summary>创建单件实例</summary>
public T NewInstance()
{
Debug.Assert(instance == null);
instance = ctor();
return instance;
}
/// <summary>将给定的实例指定为单件实例</summary>
/// <param name="instance">要指定为单件实例的实例</param>
public T NewInstance(T instance)
{
Debug.Assert(this.instance == null);
this.instance = instance;
return instance;
}
/// <summary>删除单件实例</summary>
public T DeleteInstance()
{
T ret = instance;
instance = null;
return ret;
}
}
#endregion
评论