C# Singleton Pattern Demo

public class CustomerManager
{
        private static volatile CustomerManager _instance;
        private static object syncRoot = new Object();

        private CustomerManager()
        {
        }

        public static CustomerManager Current
        {
            get
            {
                if (_instance == null)
                {
                    lock (syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new CustomerManager();
                        }
                    }
                }
                return _instance;
            }
        }
}

 

泛型化后的代码:

using System;
using System.Reflection;

namespace EPMPortal
{
	/// <summary>
	/// Manages the single instance of a class.
	/// </summary>
	/// <remarks>
	/// Generic variant of the strategy presented here : http://geekswithblogs.net/akraus1/articles/90803.aspx.
	/// Prefered to http://www.yoda.arachsys.com/csharp/singleton.html, where static initialization doesn't allow
	/// proper handling of exceptions, and doesn't allow retrying type initializers initialization later
	/// (once a type initializer fails to initialize in .NET, it can't be re-initialized again).
	/// </remarks>
	/// <typeparam name="T">Type of the singleton class.</typeparam>
	public static class Singleton<T>
		where T : class
	{
		#region Fields

		/// <summary>
		/// The single instance of the target class.
		/// </summary>
		/// <remarks>
		/// The volatile keyword makes sure to remove any compiler optimization that could make concurrent 
		/// threads reach a race condition with the double-checked lock pattern used in the Instance property.
		/// See http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx
		/// </remarks>
		static volatile T _instance;

		/// <summary>
		/// The dummy object used for locking.
		/// </summary>
		static object _lock = new object();

		#endregion Fields

		#region Constructors

		/// <summary>
		/// Type-initializer to prevent type to be marked with beforefieldinit.
		/// </summary>
		/// <remarks>
		/// This simply makes sure that static fields initialization occurs 
		/// when Instance is called the first time and not before.
		/// </remarks>
		static Singleton()
		{
		}

		#endregion Constructors

		#region Properties

		/// <summary>
		/// Gets the single instance of the class.
		/// </summary>
		public static T Instance
		{
			get
			{
				if (_instance == null)
					lock (_lock)
					{
						if (_instance == null)
						{
							ConstructorInfo constructor = null;

							try
							{
								// Binding flags exclude public constructors.
								constructor = typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[0], null);
							}
							catch (Exception exception)
							{
								throw new SingletonException(exception);
							}

							if (constructor == null || constructor.IsAssembly) // Also exclude internal constructors.
								throw new SingletonException(string.Format("A private or protected constructor is missing for '{0}'.", typeof(T).Name));

							_instance = (T)constructor.Invoke(null);
						}
					}

				return _instance;
			}
		}

		#endregion Properties
	}
}


 

namespace EPMPortal
{
   /// <summary>
   /// Represents errors that occur while creating a singleton.
   /// </summary>
   /// <remarks>
   /// http://msdn.microsoft.com/en-us/library/ms229064(VS.80).aspx
   /// </remarks>
   [Serializable]
   public class SingletonException
      : Exception
   {
      /// <summary>
      /// Initializes a new instance.
      /// </summary>
      public SingletonException()
      {
      }

      /// <summary>
      /// Initializes a new instance with a specified error message.
      /// </summary>
      /// <param name="message">The message that describes the error.</param>
      public SingletonException(string message)
         : base(message)
      {
      }

      /// <summary>
      /// Initializes a new instance with a reference to the inner 
      /// exception that is the cause of this exception.
      /// </summary>
      /// <param name="innerException">
      /// The exception that is the cause of the current exception, 
      /// or a null reference if no inner exception is specified.
      /// </param>
      public SingletonException(Exception innerException)
         : base(null, innerException)
      {
      }

      /// <summary>
      /// Initializes a new instance with a specified error message and a 
      /// reference to the inner exception that is the cause of this exception.
      /// </summary>
      /// <param name="message">The message that describes the error.</param>
      /// <param name="innerException">
      /// The exception that is the cause of the current exception, 
      /// or a null reference if no inner exception is specified.
      /// </param>
      public SingletonException(string message, Exception innerException)
         : base(message, innerException)
      {
      }

#if !WindowsCE
      /// <summary>
      /// Initializes a new instance with serialized data.
      /// </summary>
      /// <param name="info">
      /// The <see cref="System.Runtime.Serialization.SerializationInfo"/> that holds the 
      /// serialized object data about the exception being thrown.
      /// </param>
      /// <param name="context">
      /// The <see cref="System.Runtime.Serialization.StreamingContext"/> that contains 
      /// contextual information about the source or destination.
      /// </param>
      /// <exception cref="System.ArgumentNullException">The info parameter is null.</exception>
      /// <exception cref="System.Runtime.Serialization.SerializationException">The class name is null or System.Exception.HResult is zero (0).</exception>
      protected SingletonException(SerializationInfo info, StreamingContext context)
         : base(info, context)
      {
      }
#endif
   }
}



 

### C# 中单例模式的实现方法 在 C# 中,单例模式是一种常见的设计模式,用于确保一个类只有一个实例,并提供一个全局访问点。通过使用 `Lazy<T>` 类,可以方便地实现线程安全的单例模式[^1]。 以下是一个基于 `Lazy<T>` 的单例模式实现示例: ```csharp public class Singleton { private static readonly Lazy<Singleton> lazyInstance = new Lazy<Singleton>(() => new Singleton()); private Singleton() { } public static Singleton Instance { get { return lazyInstance.Value; } } } ``` 在这个实现中,`Lazy<T>` 确保了实例的延迟初始化(即只有在第一次访问 `Instance` 属性时才会创建实例)。此外,由于 `Lazy<T>` 内部实现了线程安全机制,因此不需要额外的同步代码[^1]。 另一种实现方式是使用静态构造函数。虽然这种方式不如 `Lazy<T>` 明确,但它也能保证线程安全: ```csharp public sealed class Singleton { private static readonly Singleton instance = new Singleton(); private Singleton() { } public static Singleton Instance { get { return instance; } } } ``` 在这种实现中,静态字段 `instance` 在类加载时被初始化,并且由于静态构造函数的特性,它只会在首次访问时被调用一次[^3]。 需要注意的是,无论哪种实现方式,单例模式都依赖于私有构造函数来防止外部实例化。然而,反射可能会破坏这种限制[^2]。如果需要更严格的控制,可以考虑使用枚举类型来实现单例模式。 ### 使用单例模式的场景 单例模式通常用于需要全局访问某个对象的场景,例如配置管理器、日志记录器或数据库连接池等。通过将单例类的实例存储为静态成员变量,可以避免重复创建对象,从而提高性能和资源利用率[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值