设计模式学习笔记--单例模式

本文介绍了C#中单例模式的四种实现方法,包括简单的静态初始化方式,以及为确保线程安全而采用的锁机制等。通过这些不同的实现方式,可以更好地理解单例模式在实际开发中的应用。
 1 using System;
 2 
 3 namespace Singleton
 4 {
 5     /// <summary> 
 6     /// 作者:bzyzhang
 7     /// 时间:2016/5/30 22:10:39 
 8     /// 博客地址:http://www.cnblogs.com/bzyzhang/
 9     /// Singleton说明:本代码版权归bzyzhang所有,使用时必须带上bzyzhang博客地址 
10     /// </summary> 
11     public class Singleton
12     {
13         ///实现一
14         //private static Singleton instance;
15 
16         //private Singleton()
17         //{
18         //}
19 
20         //public static Singleton GetInstance()
21         //{
22         //    if (instance == null)
23         //    {
24         //        instance = new Singleton();
25         //    }
26         //    return instance;
27         //}
28 
29         /// <summary>
30         /// 实现二,多线程时的单例
31         /// </summary>
32         //private static Singleton instance;
33 
34         //private static readonly object syncRoot = new object();
35 
36         //private Singleton() { }
37 
38         //public static Singleton GetInstance()
39         //{
40         //    lock (syncRoot)
41         //    {
42         //        if (instance == null)
43         //        {
44         //            instance = new Singleton();
45         //        }
46         //    }
47         //    return instance;
48         //}
49 
50         /// <summary>
51         /// 实现三,双重锁定
52         /// </summary>
53         //private static Singleton instance;
54 
55         //private static readonly object syncRoot = new object();
56 
57         //private Singleton() { }
58 
59         //public static Singleton GetInstance()
60         //{
61         //    if (instance == null)
62         //    {
63         //        lock (syncRoot)
64         //        {
65         //            if (instance == null)
66         //            {
67         //                instance = new Singleton();
68         //            }
69         //        }
70         //    }
71         //    return instance;
72         //}
73 
74         /// <summary>
75         /// 实现四,静态初始化
76         /// </summary>
77         private static readonly Singleton instance = new Singleton();
78 
79         private Singleton() { }
80 
81         public static Singleton GetInstance()
82         {
83             return instance;
84         }
85     }
86 }
View Code
 1 using System;
 2 
 3 namespace Singleton
 4 {
 5     class Program
 6     {
 7         static void Main(string[] args)
 8         {
 9             Singleton s1 = Singleton.GetInstance();
10             Singleton s2 = Singleton.GetInstance();
11 
12             if (s1 == s2)
13             {
14                 Console.WriteLine("两个对象是相同的实例。");
15             }
16         }
17     }
18 }
View Code

 

转载于:https://www.cnblogs.com/bzyzhang/p/5544136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值