在咱们平常的工做中常常须要在应用程序中保持一个惟一的实例,如:IO处理,数据库操做等,因为这些对象都要占用重要的系统资源,因此咱们必须限制这些实例的建立或始终使用一个公用的实例,这就是咱们今天要介绍的——单例模式(Singleton)。html
单件模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点。设计模式
图1单例模式(Singleton)结构图安全
单例模式(Singleton)是几个建立模式中最对立的一个,它的主要特色不是根据用户程序调用生成一个新的实例,而是控制某个类型的实例惟一性,经过 上图咱们知道它包含的角色只有一个,就是Singleton,它拥有一个私有构造函数,这确保用户没法经过new直接实例它。除此以外,该模式中包含一个 静态私有成员变量instance与静态公有方法Instance()。Instance()方法负责检验并实例化本身,而后存储在静态成员变量中,以确 保只有一个实例被建立。服务器
图2单例模式(Singleton)逻辑模型多线程
接下来咱们将介绍6中不一样的单例模式(Singleton)的实现方式。这些实现方式都有如下的共同点:less
/// <summary> /// A simple singleton class implements. /// </summary> public sealed class Singleton { private static Singleton _instance = null; /// <summary> /// Prevents a default instance of the /// <see cref="Singleton"/> class from being created. /// </summary> private Singleton() { } /// <summary> /// Gets the instance. /// </summary> public static Singleton Instance { get { return _instance ?? (_instance = new Singleton()); } } }
以上的实现方式适用于单线程环境,由于在多线程的环境下有可能获得Singleton类的多个实例。假如同时有两个线程去判断(null == _singleton),而且获得的结果为真,那么两个线程都会建立类Singleton的实例,这样就违背了Singleton模式“惟一实例”的初衷。dom
/// <summary> /// A thread-safe singleton class. /// </summary> public sealed class Singleton { private static Singleton _instance = null; private static readonly object SynObject = new object(); Singleton() { } /// <summary> /// Gets the instance. /// </summary> public static Singleton Instance { get { // Syn operation. lock (SynObject) { return _instance ?? (_instance = new Singleton()); } } } }
以上方式的实现方式是线程安全的,首先咱们建立了一个静态只读的进程辅助对象,因为lock是确保当一个线程位于代码的临界区时,另外一个线程不能进入临界 区(同步操做)。若是其余线程试图进入锁定的代码,则它将一直等待,直到该对象被释放。从而确保在多线程下不会建立多个对象实例了。只是这种实现方式要进 行同步操做,这将是影响系统性能的瓶颈和增长了额外的开销。ide
前面讲到的线程安全的实现方式的问题是要进行同步操做,那么咱们是否能够下降经过操做的次数呢?其实咱们只需在同步操做以前,添加判断该实例是否为null就能够下降经过操做的次数了,这样是经典的Double-Checked Locking方法。函数
/// <summary> /// Double-Checked Locking implements a thread-safe singleton class /// </summary> public sealed class Singleton { private static Singleton _instance = null; // Creates an syn object. private static readonly object SynObject = new object(); Singleton() { } public static Singleton Instance { get { // Double-Checked Locking if (null == _instance) { lock (SynObject) { if (null == _instance) { _instance = new Singleton(); } } } return _instance; } } }
在介绍第四种实现方式以前,首先让咱们认识什么是,当字段被标记为beforefieldinit类型时,该字段初始化能够发生在任什么时候候任何字段被引用以前。这句话听起了有点别扭,接下来让咱们经过具体的例子介绍。
/// <summary> /// Defines a test class. /// </summary> class Test { public static string x = EchoAndReturn("In type initializer"); public static string EchoAndReturn(string s) { Console.WriteLine(s); return s; } }
上面咱们定义了一个包含静态字段和方法的类Test,但要注意咱们并无定义静态的构造函数。
图3 Test类的IL代码
class Test { public static string x = EchoAndReturn("In type initializer"); // Defines a parameterless constructor. static Test() { } public static string EchoAndReturn(string s) { Console.WriteLine(s); return s; } }
上面咱们给Test类添加一个静态的构造函数。
图4 Test类的IL代码
经过上面Test类的IL代码的区别咱们发现,当Test类包含静态字段,并且没有定义静态的构造函数时,该类会被标记为beforefieldinit。
如今也许有人会问:“被标记为beforefieldinit和没有标记的有什么区别呢”?OK如今让咱们经过下面的具体例子看一下它们的区别吧!
class Test { public static string x = EchoAndReturn("In type initializer"); static Test() { } public static string EchoAndReturn(string s) { Console.WriteLine(s); return s; } } class Driver { public static void Main() { Console.WriteLine("Starting Main"); // Invoke a static method on Test Test.EchoAndReturn("Echo!"); Console.WriteLine("After echo"); Console.ReadLine(); // The output result: // Starting Main // In type initializer // Echo! // After echo } }
我相信你们均可以获得答案,若是在调用EchoAndReturn()方法以前,须要完成静态成员的初始化,因此最终的输出结果以下:
图5输出结果
接着咱们在Main()方法中添加string y = Test.x,以下:
public static void Main() { Console.WriteLine("Starting Main"); // Invoke a static method on Test Test.EchoAndReturn("Echo!"); Console.WriteLine("After echo"); //Reference a static field in Test string y = Test.x; //Use the value just to avoid compiler cleverness if (y != null) { Console.WriteLine("After field access"); } Console.ReadKey(); // The output result: // In type initializer // Starting Main // Echo! // After echo // After field access }
图6 输出结果
经过上面的输出结果,你们能够发现静态字段的初始化跑到了静态方法调用以前,Wo不可思议啊!
最后咱们在Test类中添加一个静态构造函数以下:
class Test { public static string x = EchoAndReturn("In type initializer"); static Test() { } public static string EchoAndReturn(string s) { Console.WriteLine(s); return s; } }
图7 输出结果
理论上,type initializer应该发生在”Echo!”以后和”After echo”以前,但这里却出现了不惟一的结果,只有当Test类包含静态构造函数时,才能确保type initializer的初始化发生在”Echo!”以后和”After echo”以前。
因此说要确保type initializer发生在被字段引用时,咱们应该给该类添加静态构造函数。接下来让咱们介绍单例模式的静态方式。
public sealed class Singleton { private static readonly Singleton _instance = new Singleton(); // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Singleton() { } /// <summary> /// Prevents a default instance of the /// <see cref="Singleton"/> class from being created. /// </summary> private Singleton() { } /// <summary> /// Gets the instance. /// </summary> public static Singleton Instance { get { return _instance; } } }
以上方式实现比以前介绍的方式都要简单,但它确实是多线程环境下,C#实现的Singleton的一种方式。因为这种静态初始化的方式是在本身的字段被引用时才会实例化。
让咱们经过IL代码来分析静态初始化。
图8静态初始化IL代码
首先这里没有beforefieldinit的修饰符,因为咱们添加了静态构造函数当静态字段被引用时才进行初始化,所以即使不少线程试图引用_instance,也须要等静态构造函数执行完并把静态成员_instance实例化以后可使用。
/// <summary> /// Delaies initialization. /// </summary> public sealed class Singleton { private Singleton() { } /// <summary> /// Gets the instance. /// </summary> public static Singleton Instance { get { return Nested._instance; } } private class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() { } internal static readonly Singleton _instance = new Singleton(); } }
这里咱们把初始化工做放到Nested类中的一个静态成员来完成,这样就实现了延迟初始化。
/// <summary> /// .NET 4's Lazy<T> type /// </summary> public sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } }
这种方式的简单和性能良好,并且还提供检查是否已经建立实例的属性IsValueCreated。
如今让咱们使用单例模式(Singleton)实现负载平衡器,首先咱们定义一个服务器类,它包含服务器名和IP地址以下:
/// <summary> /// Represents a server machine /// </summary> class Server { // Gets or sets server name public string Name { get; set; } // Gets or sets server IP address public string IP { get; set; } }
因为负载平衡器只提供一个对象实例供服务器使用,因此咱们使用单例模式(Singleton)实现该负载平衡器。
/// <summary> /// The 'Singleton' class /// </summary> sealed class LoadBalancer { private static readonly LoadBalancer _instance = new LoadBalancer(); // Type-safe generic list of servers private List<Server> _servers; private Random _random = new Random(); static LoadBalancer() { } // Note: constructor is 'private' private LoadBalancer() { // Load list of available servers _servers = new List<Server> { new Server{ Name = "ServerI", IP = "192.168.0.108" }, new Server{ Name = "ServerII", IP = "192.168.0.109" }, new Server{ Name = "ServerIII", IP = "192.168.0.110" }, new Server{ Name = "ServerIV", IP = "192.168.0.111" }, new Server{ Name = "ServerV", IP = "192.168.0.112" }, }; } /// <summary> /// Gets the instance through static initialization. /// </summary> public static LoadBalancer Instance { get { return _instance; } } // Simple, but effective load balancer public Server NextServer { get { int r = _random.Next(_servers.Count); return _servers[r]; } } }
static void Main() { LoadBalancer b1 = LoadBalancer.Instance; b1.GetHashCode(); LoadBalancer b2 = LoadBalancer.Instance; LoadBalancer b3 = LoadBalancer.Instance; LoadBalancer b4 = LoadBalancer.Instance; // Confirm these are the same instance if (b1 == b2 && b2 == b3 && b3 == b4) { Console.WriteLine("Same instance\n"); } // Next, load balance 15 requests for a server LoadBalancer balancer = LoadBalancer.Instance; for (int i = 0; i < 15; i++) { string serverName = balancer.NextServer.Name; Console.WriteLine("Dispatch request to: " + serverName); } Console.ReadKey(); }
图9 LoadBalancer输出结果
单例模式的优势:
单例模式(Singleton)会控制其实例对象的数量,从而确保访问对象的惟一性。
单例模式的缺点:
单例适用性
使用Singleton模式有一个必要条件:在一个系统要求一个类只有一个实例时才应当使用单例模式。反之,若是一个类能够有几个实例共存,就不要使用单例模式。
不要使用单例模式存取全局变量。这违背了单例模式的用意,最好放到对应类的静态成员中。
不要将数据库链接作成单例,由于一个系统可能会与数据库有多个链接,而且在有链接池的状况下,应当尽量及时释放链接。Singleton模式因为使用静态成员存储类实例,因此可能会形成资源没法及时释放,带来问题。
========================================================