您的位置:首页 > 其它

设计模式系列:单件模式

2011-11-16 09:09 323 查看


[align=center] [/align]



using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; //设计模式之单件模式 /* 单件设计模式适用性:1、当类只能有一个实例而且客户可以弄一个众所周知的访问点访问它时 * 1、当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时 * 应用场景: * 1、每台计算机可以有若干个打印机,但只能有一个Printer Spooler.避免两个打印作业同时输出到打印机。 * 2、pc机中可能有几个串口,但只能有一个com1口的实例 * 3、系统中只能有一个窗口管理器 * 4、.net Remoting中服务器激活对象中的sigleton对象,确保所有的客户程序的请求都只有一个实例来处理。 * */ namespace She_Ji_Mo_Shi { public sealed class Singleton_Pattern1 { static Singleton_Pattern1 instance = null; public static Singleton_Pattern1 Instace { get { if (instance == null) { instance = new Singleton_Pattern1(); } return instance; } } } //这种方式的实现对于线程来说并不是安全的,因为在多线程的环境下有可能得到Singleton_Pattern1类的多个实例, //如果同时有两个线程去判断instance == null;并且得到的结果为真,这时两个线程都会创建类的实例,这样肯定违背了单件模式的原则

public sealed class Singleton_Pattern { static Singleton_Pattern instance = null; static readonly object Padlock = new object();//系统实例化是加锁 public static Singleton_Pattern Instance { get { if (instance == null) { lock (Padlock) { if (instance == null) { instance = new Singleton_Pattern(); }

} } return instance; } } } //这种方法的实现对于线程来说是安全的,我们首相创建了一个进程辅助的对象,线程在进入时先加锁,然后在检测对象时候被创建, //这样确保只有一个实例被创建,这是双重锁定,在线程调用时先判断是否已经创建实例。 public sealed class Singleton_Pattern2 { static readonly Singleton_Pattern2 instance = new Singleton_Pattern2(); static Singleton_Pattern2() { } public static Singleton_Pattern2 Instance { get { return instance; } } } //在此实现中,将在第一次引用类的任何成员是创建实例,公共语言运行库负责处理变量的初始化。该类标记为seled //以阻止发生派生,而派生可能会增加实例,此为,变量标记为redonly,这意味着只能在静态初始化期间(次出显示的实例) //或在类构造函数中分配变量。 //该实现与前面的实例类似,不同之处在于它依赖的公共语言运行库来初始化变量,他仍然可以用来解决singleton类模式试图解决的问题 //全局访问和实例化控制。公共静态属性访问为访问实例提供了一个全局的访问点。因此此函数是私有的,因此不能在类的本身以外实例化 //singleton类,因此,变量引用的是可以在系统中存在的唯一的实例。 //由于该实例被私有静态属性引用,因此在类首次被对instance属性的调用所应用之前,不会被实例化。 public sealed class Singleton { Singleton() { } public static Singleton Instance { get { return Nested.instance; } } class Nested { internal static readonly Singleton instance = new Singleton(); }

} //这个方法是将类的初始化放到Nested这个类中进行操作,并具有很多的优势,很好的一种方法

///////////////////////////////////////////////////////////////////////////////////////////////// //完整的实例代码 //编写于2010年8月3号 public class Count_Singleton { //存储唯一的实例 static Count_Singleton unCounter = new Count_Singleton(); private int Totle = 0; private Count_Singleton() { Thread.Sleep(2000); } static public Count_Singleton Instance() { return unCounter; } /// 计数器加1
public void Add() { Totle++; } public int GetCounter() { return Totle; } } public class CountMutilThread { /// <summary> /// 线程工作 /// </summary> public static void DoSomeWork() { //构造显示结果的字符串 string Result = ""; //创建一个sigleton实例 Count_Singleton My_Count = Count_Singleton.Instance(); object oj = new object();//用来给系统加锁 //循环调用四次 for (int i = 0; i < 5; i++) { lock (oj) { //开始计数 My_Count.Add(); Result += "线程"; Result += Thread.CurrentThread.Name.ToString() + "------->"; Result += "当前计数"; Result += My_Count.GetCounter().ToString(); Result += "\n"; Console.WriteLine(Result); //清空字符串 Result = ""; } } } public void StartMain() { Thread thread0 = Thread.CurrentThread; thread0.Name = "Thread 0"; Thread thread1 = new Thread(new ThreadStart(DoSomeWork)); thread1.Name = "Thread 1"; Thread thread2 = new Thread(new ThreadStart(DoSomeWork)); thread2.Name = "Thread 2"; Thread thread3 = new Thread(new ThreadStart(DoSomeWork)); thread3.Name = "Thread 3"; thread1.Start(); thread2.Start(); thread3.Start(); //线程0也执行相同的工作 DoSomeWork();

} }

}

下面是主函数调用:

class Program { static void Main(string[] args) { ///功能:实现多线程计数器的客户端 CountMutilThread cmt = new CountMutilThread(); cmt.StartMain(); Console.ReadLine();

} }

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: