设计模式之单例模式

单例模式

  这种类型的设计模式属于创建型模式,指在内存中只会创建且仅创建一次对象。

  1.单例类只能有一个实例

  2.单例类必须自己创建自己的唯一实例

  3.单例类必须给所有其他对象提供这一实例

  4.类有它的私有构造函数和本身的一个静态实例

  

单例模式的类型

  单例模式有两种类型:

    • 懒汉式:在真正需要使用对象时才去创建该单例类对象
    • 饿汉式:在类加载时已经创建好该单例对象,等待被程序使用

  懒汉式:创建对象的方法是在程序使用对象前,先判断该对象是否已经实例化(判空),若已实例化直接返回该类对象。,否则则先执行实例化操作。

    

  饿汉式:类加载时已经创建好该对象,在程序调用时直接返回该单例对象即可,即我们在编码时就已经指明了要马上创建这个对象,不需要等到被调用时再去创建。

    

代码示例: 

 1     /// <summary>
 2     /// 单例模式,懒汉式
 3     /// </summary>
 4     public class SingletonLazy
 5     {
 6         private static SingletonLazy _instance = null;
 7         private static readonly object _syncLook = new object();
 8 
 9         private SingletonLazy()
10         { }
11 
12         /// <summary>
13         /// 这种实现最大的问题就是不支持多线程。
14         /// 因为没有加锁 synchronized,所以严格意义上它并不算单例模式。
15         /// 这种方式 lazy loading 很明显,不要求线程安全,在多线程不能正常工作。
16         /// </summary>
17         /// <returns></returns>
18         public static SingletonLazy UnsafeInstance()
19         {
20             if (_instance == null)
21             {
22                 _instance = new SingletonLazy();
23             }
24             return _instance;
25         }
26 
27         /// <summary>
28         /// 这种方式具备很好的 lazy loading,能够在多线程中很好的工作
29         /// 但是,效率很低,99% 情况下不需要同步。
30         /// 优点:第一次调用才初始化,避免内存浪费。
31         /// 缺点:必须加锁 才能保证单例,但加锁会影响效率。
32         /// SafeInstance()的性能对应用程序不是很关键(该方法使用不太频繁)
33         /// </summary>
34         /// <returns></returns>
35         public static SingletonLazy SafeInstance()
36         {
37             if (_instance == null)
38             {
39                 lock (_syncLook)
40                 {
41                     _instance = new SingletonLazy();
42                 }
43             }
44             return _instance;
45         }
46 
47         /// <summary>
48         /// 双重检验锁,这种方式采用双锁机制,安全且在多线程情况下能保持高性能。
49         /// </summary>
50         /// <returns></returns>
51         public static SingletonLazy DoubleCheckAndLockInstance()
52         {
53             if (_instance == null)
54             {
55                 lock (_syncLook)
56                 {
57                     if (_instance == null)
58                     {
59                         _instance = new SingletonLazy();
60                     }
61                 }
62             }
63             return _instance;
64         }
65     }
66 
67     /// <summary>
68     /// 饿汉式
69     /// </summary>
70     public class SingletonHun
71     {
72         private static SingletonHun _instance = new SingletonHun();
73 
74         private SingletonHun()
75         { }
76 
77         public static SingletonHun GetInstance()
78         {
79             return _instance;
80         }
81     }
View Code C#
原文地址:https://www.cnblogs.com/hypo106/p/13369544.html