单件模式(Singleton Pattern)
2{
3 static Singleton instance=null;
4
5 Singleton()
6 {
7 }
8
9 public static Singleton Instance
10 {
11 get
12 {
13 if (instance==null)
14 {
15 instance = new Singleton();
16 }
17 return instance;
18 }
19 }
20}
2{
3 static Singleton instance=null;
4 static readonly object padlock = new object();
5
6 Singleton()
7 {
8 }
9
10 public static Singleton Instance
11 {
12 get
13 {
14 lock (padlock)
15 {
16 if (instance==null)
17 {
18 instance = new Singleton();
19 }
20 return instance;
21 }
22 }
23 }
24}
25
26
2{
3 static Singleton instance=null;
4 static readonly object padlock = new object();
5
6 Singleton()
7 {
8 }
9
10 public static Singleton Instance
11 {
12 get
13 {
14 if (instance==null)
15 {
16 lock (padlock)
17 {
18 if (instance==null)
19 {
20 instance = new Singleton();
21 }
22 }
23 }
24 return instance;
25 }
26 }
27}
28
2{
3 static readonly Singleton instance=new Singleton();
4
5 static Singleton()
6 {
7 }
8
9 Singleton()
10 {
11 }
12
13 public static Singleton Instance
14 {
15 get
16 {
17 return instance;
18 }
19 }
20}
21
2{
3 Singleton()
4 {
5 }
6
7 public static Singleton Instance
8 {
9 get
10 {
11 return Nested.instance;
12 }
13 }
14
15 class Nested
16 {
17 static Nested()
18 {
19 }
20
21 internal static readonly Singleton instance = new Singleton();
22 }
23}
24
现方式。
对象实例,但这样会导致 Singleton 类中出现悬浮引用。
2using System.Threading;
3
4namespace SigletonPattern.SigletonCounter
5{
6 /// <summary>
7 /// 功能:简单计数器的单件模式
8 /// 编写:Terrylee
9 /// 日期:2005年12月06日
10 /// </summary>
11 public class CountSigleton
12 {
13 ///存储唯一的实例
14 static CountSigleton uniCounter = new CountSigleton();
15
16 ///存储计数值
17 private int totNum = 0;
18
19 private CountSigleton()
20
21 {
22 ///线程延迟2000毫秒
23 Thread.Sleep(2000);
24 }
25
26 static public CountSigleton Instance()
27
28 {
29
30 return uniCounter;
31
32 }
33
34 ///计数加1
35 public void Add()
36 {
37 totNum ++;
38 }
39
40 ///获得当前计数值
41 public int GetCounter()
42 {
43 return totNum;
44 }
45
46 }
47}
48
2using System.Threading;
3using System.Text;
4
5namespace SigletonPattern.SigletonCounter
6{
7 /// <summary>
8 /// 功能:创建一个多线程计数的类
9 /// 编写:Terrylee
10 /// 日期:2005年12月06日
11 /// </summary>
12 public class CountMutilThread
13 {
14 public CountMutilThread()
15 {
16
17 }
18
19 /// <summary>
20 /// 线程工作
21 /// </summary>
22 public static void DoSomeWork()
23 {
24 ///构造显示字符串
25 string results = "";
26
27 ///创建一个Sigleton实例
28 CountSigleton MyCounter = CountSigleton.Instance();
29
30 ///循环调用四次
31 for(int i=1;i<5;i++)
32 {
33 ///开始计数
34 MyCounter.Add();
35
36 results +="线程";
37 results += Thread.CurrentThread.Name.ToString() + "——〉";
38 results += "当前的计数:";
39 results += MyCounter.GetCounter().ToString();
40 results += "\n";
41
42 Console.WriteLine(results);
43
44 ///清空显示字符串
45 results = "";
46 }
47 }
48
49 public void StartMain()
50 {
51
52 Thread thread0 = Thread.CurrentThread;
53
54 thread0.Name = "Thread 0";
55
56 Thread thread1 =new Thread(new ThreadStart(DoSomeWork));
57
58 thread1.Name = "Thread 1";
59
60 Thread thread2 =new Thread(new ThreadStart(DoSomeWork));
61
62 thread2.Name = "Thread 2";
63
64 Thread thread3 =new Thread(new ThreadStart(DoSomeWork));
65
66 thread3.Name = "Thread 3";
67
68 thread1.Start();
69
70 thread2.Start();
71
72 thread3.Start();
73
74 ///线程0也只执行和其他线程相同的工作
75 DoSomeWork();
76 }
77 }
78}
79
2using System.Text;
3using System.Threading;
4
5namespace SigletonPattern.SigletonCounter
6{
7 /// <summary>
8 /// 功能:实现多线程计数器的客户端
9 /// 编写:Terrylee
10 /// 日期:2005年12月06日
11 /// </summary>
12 public class CountClient
13 {
14 public static void Main(string[] args)
15 {
16 CountMutilThread cmt = new CountMutilThread();
17
18 cmt.StartMain();
19
20 Console.ReadLine();
21 }
22 }
23}
24
×××:/Files/Terrylee/SigletonPattern.rar