当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 设计模式-创建型-单例模式

设计模式-创建型-单例模式

2019年09月12日  | 移动技术网IT编程  | 我要评论

单例模式:对于类的单例模式设计,就是采取一定的方法保证在整个软件系统中,对某个类只能存在一个对象实例,并且该类只提供一个取得其对象实例的方法(静态方法)。

单例模式有8种方式:

  1、饿汉式(静态常量)

  // 2、饿汉式(静态代码块)

  3、懒汉式(线程不安全)

  4、懒汉式(线程安全,同步方法)

  5、懒汉式(线程安全,同步代码块)

  6、双重检查double check

  7、静态内部类

  // 8、枚举

★饿汉式(静态常量)

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton instance1 = singleton.getinstance();
 7         singleton instance2 = singleton.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton
16 {
17     /// <summary>
18     /// 1、私有化构造函数,外部不能new
19     /// </summary>
20     private singleton()
21     {
22     }
23 
24     // 2、本类的内部创建对象实例
25     private readonly static singleton instance = new singleton();
26 
27     // 3、提供一个公有的静态方法,返回对象实例
28     public static singleton getinstance()
29     {
30         return instance;
31     }
32 }

  优缺点说明:

    1、优点:写法简单,就是在类状态的时候就完成了实例化,避免线程同步问题。

    2、缺点:在类状态的时候完成实例化,没有达到lazy loading的效果。如果从始至终从未使用过这个实例,则会造成内存的浪费。

    3、这种方式基于classloader机制避免了多线程的同步问题,不过,instance在类装载时就实例化,在单例模式中大多数都是调用getinstance方法,但是导致类装载的原因有很多,因此不能确定有其他的方式导致装载,这时候初始化instance就没有达到lazy loading的效果。

  结论:这种单例模式可用,可能造成内存浪费。

★饿汉式(静态代码块)

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton instance1 = singleton.getinstance;
 7         singleton instance2 = singleton.getinstance;
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton
16 {
17     /// <summary>
18     /// 1、私有化构造函数,外部不能new
19     /// </summary>
20     private singleton()
21     {
22     }
23 
24     public static singleton getinstance { get; private set; } = new singleton();
25 }

★懒汉式(线程不安全)

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton1 instance1 = singleton1.getinstance();
 7         singleton1 instance2 = singleton1.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton1
16 {
17     // 私有的静态变量
18     private static singleton1 instance;
19 
20     /// <summary>
21     /// 1、私有化构造函数,外部不能new
22     /// </summary>
23     private singleton1()
24     {
25     }
26 
27     /// <summary>
28     /// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
29     /// </summary>
30     /// <returns></returns>
31     public static singleton1 getinstance()
32     {
33         if (instance == null)
34         {
35             instance = new singleton1();
36         }
37         return instance;
38     }
39 }

  优缺点说明:

    1、起到了lazy loading效果,但是只能在单线程下使用。

    2、若在多线程下,一个线程进入if(singleton==null)判断语句块,还未来得及往下执行,另一个线程也通过了该判断,就会产生多个实例。所以在多线程环境下不可使用这种方式。

  结论:在实际开发中,不要使用这种方式。

★懒汉式(线程安全,同步方法) 

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton3 instance1 = singleton3.getinstance();
 7         singleton3 instance2 = singleton3.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton3
16 {
17     // 定义一个静态变量来保存类的实例
18     private static singleton3 instance;
19 
20     // 定义一个标识确保线程同步
21     private static readonly object locker = new object();
22 
23     /// <summary>
24     /// 私有化构造函数,外部不能new
25     /// </summary>
26     private singleton3()
27     {
28     }
29 
30     /// <summary>
31     /// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
32     /// </summary>
33     /// <returns></returns>
34     public static singleton3 getinstance()
35     {
36         // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
37         // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
38         // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
39         lock (locker)
40         {
41             if (instance == null)
42             {
43                 instance = new singleton3();
44             }
45         }
46         return instance;
47     }
48 }

  优缺点说明:

    1、解决了线程不安全的问题。

    2、效率太低,每个线程都想获取类实例的时候,执行getinstance()时都需要进行同步,而该方法只需要执行一次实例化代码就够了,后面想获取该类的实例直接return就可以了。

  结论:在实际开发中,不推荐使用这种方式。    

★懒汉式(线程安全,同步代码块) 

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton4 instance1 = singleton4.getinstance();
 7         singleton4 instance2 = singleton4.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton4
16 {
17     // 定义一个静态变量来保存类的实例
18     private static singleton4 instance;
19 
20     // 定义一个标识确保线程同步
21     private static readonly object locker = new object();
22 
23     /// <summary>
24     /// 私有化构造函数,外部不能new
25     /// </summary>
26     private singleton4()
27     {
28     }
29 
30     /// <summary>
31     /// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
32     /// </summary>
33     /// <returns></returns>
34     public static singleton4 getinstance()
35     {
36         if (instance == null)
37         {
38             lock (locker)
39             {
40                 instance = new singleton4();
41             }
42         }
43         return instance;
44     }
45 }
 

  优缺点说明:

    1、这种方式本意上时对上述方式的改进,改为同步产生实例化的代码块。

    2、但是这种方式没有解决线程安全问题。  

  结论:强烈不推荐。

★双重检查 

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton5 instance1 = singleton5.getinstance();
 7         singleton5 instance2 = singleton5.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton5
16 {
17     // 定义一个静态变量来保存类的实例
18     private static singleton5 instance;
19 
20     // 定义一个标识确保线程同步
21     private static readonly object locker = new object();
22 
23     /// <summary>
24     /// 私有化构造函数,外部不能new
25     /// </summary>
26     private singleton5()
27     {
28     }
29 
30     /// <summary>
31     /// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
32     /// </summary>
33     /// <returns></returns>
34     public static singleton5 getinstance()
35     {
36         // 当第一个线程运行到这里时,此时会对locker对象 "加锁",
37         // 当第二个线程运行该方法时,首先检测到locker对象为"加锁"状态,该线程就会挂起等待第一个线程解锁
38         // lock语句运行完之后(即线程运行完之后)会对该对象"解锁"
39         // 双重锁定只需要一句判断就可以了
40         if (instance == null)
41         {
42             lock (locker)
43             {
44                 if (instance == null)
45                 {
46                     instance = new singleton5();
47                 }
48             }
49         }
50         return instance;
51     }
52 }

  结论:推荐使用。 

★静态内部类 

 1 internal class program
 2 {
 3     private static void main(string[] args)
 4     {
 5         // 测试
 6         singleton2 instance1 = singleton2.getinstance();
 7         singleton2 instance2 = singleton2.getinstance();
 8 
 9         console.writeline(instance1 == instance2);
10         console.writeline($"instance1.gethashcode={instance1.gethashcode()}");
11         console.writeline($"instance2.gethashcode={instance2.gethashcode()}");
12     }
13 }
14 
15 internal class singleton2
16 {
17     /// <summary>
18     /// 1、私有化构造函数,外部不能new
19     /// </summary>
20     private singleton2()
21     {
22     }
23 
24     private static class singletoninstance
25     {
26         public readonly static singleton2 instance = new singleton2();
27     }
28 
29     /// <summary>
30     /// 提供一个静态的公有方法,当使用到该方法时,才去创建instance,即懒汉式
31     /// </summary>
32     /// <returns></returns>
33     public static singleton2 getinstance()
34     {
35         return singletoninstance.instance;
36     }
37 }

  优缺点说明:

    1、这种方式采用了类装载的机制来保证初始化实例时只有一个线程。

    2、静态内部类方式在singleton类被装载时并不会立即实例化,而是在需要实例化时,通过getinstance方法才会装载singletoninstance类,从而完成singleton实例化。

    3、类的静态属性只会在类第一次装载的时候初始化。

    4、避免了线程不安全,利用静态内部类特点实现延迟加载,效率高。

  结论:推荐使用。 

★枚举

   

  在java中提供该方式。 

c#中实现了单例模式的类:

 1 // 该类不是一个公开类
 2 // 但是该类的实现应用了单例模式
 3 internal sealed class sr
 4 {
 5     private static sr loader;
 6     internal sr()
 7     {
 8     }
 9     // 主要是因为该类不是公有,所以这个全部访问点也定义为私有的了
10      // 但是思想还是用到了单例模式的思想的
11      private static sr getloader()
12     {
13          if (loader == null)
14          {
15              sr sr = new sr();
16              interlocked.compareexchange<sr>(ref loader, sr, null);
17          }
18         return loader;
19     }
20 
21     // 这个公有方法中调用了getloader方法的
22     public static object getobject(string name)
23     {
24          sr loader = getloader();
25          if (loader == null)
26          {
27                return null;
28          }
29          return loader.resources.getobject(name, culture);
30     }
31 }

参考:https://www.cnblogs.com/zhili/p/singletonpatterm.html

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网