/**//** * 模式名和分类:创建型模式 * * 意图:(1.设计模式是做什么的?) * 保证一个类仅有一个实例,并提供一个访问他的全局访问点 * (2.它的基本原理和意图是什么?) * (3.它解决的是什么样的特定的设计问题?) * 别名:(模式的其他名称.) * Singleton Pattern * 动机:(用于说明一个设计问题以及如何用模式的类、对象来解决该问题的特定情景。 * 该情景会帮助你理解后对模式更抽象的描述。) * * 适用性:(1.什么情况下可以使用该设计模式? * 2.该模式可以用来改进哪些不良的设计? * 3.你怎样识别这些情况?) * 1.1:当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。 * 1.2:当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。 * * 结构:(采用基于对象建模技术(OMT)的表示法对模式种的类进行图形描述。 * 我们也使用了交互图来说明对象之间的请求序列和协作关系。) * ┌----------------------┐ * │ Singleton │ * ├----------------------┤ * │ -instance:Singleton │ * ├----------------------┤ * │ -Singleton() │ * │ +Instance():Singleton│ * └----------------------┘ * * 参与者:(指设计模式的类或对象以及他们各自的职责。) * * 协作:(模式的参与者怎样协作以及实现他们的职责) * * 效果:(1.模式怎样支持它的目标? * 2.使用模式的效果和所需要的权衡取舍? * 3.系统结构的哪些方面可以独立改变?) * * 实现:(实现模式时需要知道的一些提示、技术要点以及应避免的缺陷, * 以及是否存在某些特定于实现语言的问题。) * * 代码示例:(用来说明怎样用特定语言实现该模式的代码片段.) * * 已知应用:(实际系统种发现的模式的例子。每个模式至少包括了两个不同领域的实例。) * 1.总统选举 * ┌----------------------┐ * │ 总统 │ * ├----------------------┤ * │ -选举:总统 ---------┼-返回唯一实例 * ├----------------------┤ * │ -总统() │ * │ +选举():总统 │ * └----------------------┘ * * 相关模式:(1.与这个模式紧密相关的模式有哪些? * 2.其间重要的不同之处是什么? * 3.这个模式应与哪些其他模式一起使用?) * **///Singleton pattern --Structural exampleusing System;using System.Collections.Generic;using System.Text;namespace DesignPattern.SingletonPattern.Structural...{ //MainApp test application class MainApp ...{ static void Main() ...{ //Constructor is protected --cannot use new Singleton s1 = Singleton.Instance(); Singleton s2 = Singleton.Instance(); if (s1 == s2) ...{ Console.WriteLine("Object are the same instance"); } //wait for user; Console.Read(); } } class Singleton ...{ private static Singleton instance; //Note:Constructor is 'protected' protected Singleton() ...{ } public static Singleton Instance() ...{ //Use 'Lazy initialization' if (instance == null) ...{ instance = new Singleton(); } return instance; } };} 总统选举示例: /**//** * 模式名和分类:Singleton pattern * * 意图:保证一个类仅有一个实例,并提供一个访问他的全局访问点 * * 别名:创建型模板 * * 动机: * * 适用性: * 1.1:当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。 * 1.2:当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。 * * 结构: * 总统选举例子 * ┌----------------------┐ * │ 总统 │ * ├----------------------┤ * │ -选举:总统 ---------┼-返回唯一实例 * ├----------------------┤ * │ -总统() │ * │ +选举():总统 │ * └----------------------┘ * * 参与者: * * 协作: * * 效果: * * 实现: * * 代码示例:见Structural.cs * * 已知应用:见LoadBalancer.cs * * 相关模式: * **/using System;using System.Collections.Generic;using System.Text;using System.Threading;using System.Collections;namespace DesignPattern.SingletonPattern.RealWorld.LoadBalancer...{ //MainApp test application class MainApp ...{ static void Main() ...{ LoadBalancer b1 = LoadBalancer.GetLoadBalancer(); LoadBalancer b2 = LoadBalancer.GetLoadBalancer(); LoadBalancer b3 = LoadBalancer.GetLoadBalancer(); LoadBalancer b4 = LoadBalancer.GetLoadBalancer(); //Same instance? if (b1 == b2 && b2 == b3 && b3 == b4) ...{ Console.WriteLine("Same instance "); } //All are tehe same instance --use b1 arbitrarily //Load balance 15 server requests for (int i = 0; i < 15; i++) ...{ Console.WriteLine("b1.Server"); } //wait for user Console.Read(); } } //"Singleton" class LoadBalancer ...{ private static LoadBalancer instance; private ArrayList servers = new ArrayList(); private Random random = new Random(); //Lock synchronization object(锁定同一对象) private static object synclock = new object(); //Constructor (protected) //构造器 protected LoadBalancer() ...{ //List of available servers servers.Add("ServerI"); servers.Add("ServerII"); servers.Add("ServerIII"); servers.Add("ServerIV"); servers.Add("ServerV"); } public static LoadBalancer GetLoadBalancer() ...{ //Support multithreaded applications through //'Double checked locking' pattern which(once the instance exits) avoids locking each time the method is invoked if (instance == null) ...{ lock (synclock) ...{ if (instance == null) ...{ instance = new LoadBalancer(); } } } return instance; } //Simple,but effective random load balancer public string Server ...{ get ...{ int r = random.Next(servers.Count); return servers[r].ToString(); } } };}