.NET设计模式(2):单件模式(Singleton Pattern)

2023-10-29 11:08

本文主要是介绍.NET设计模式(2):单件模式(Singleton Pattern),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概述

Singleton模 式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。这就提出了一个问题:如何绕过常规的构造器,提供一种机制来保证一个类只有一个实例?客户程 序在调用某一个类时,它是不会考虑这个类是否只能有一个实例等问题的,所以,这应该是类设计者的责任,而不是类使用者的责任。

从另一个角度来说,Singleton模式其实也是一种职责型模式。因为我们创建了一个对象,这个对象扮演了独一无二的角色,在这个单独的对象实例中,它集中了它所属类的所有权力,同时它也肩负了行使这种权力的职责!

意图

保证一个类仅有一个实例,并提供一个访问它的全局访问点。

模型图

逻辑模型图:

.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

物理模型图:

.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

生活中的例子

美国总统的职位是Singleton,美国宪法规定了总统的选举,任期以及继任的顺序。这样,在任何时刻只能由一个现任的总统。无论现任总统的身份为何,其头衔"美利坚合众国总统"是访问这个职位的人的一个全局的访问点。

.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

五种实现

1.简单实现

 

 

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 public   sealed   class  Singleton
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 3.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static Singleton instance=null;
 4.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 5.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    Singleton()
 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public static Singleton Instance
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        get
12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            if (instance==null)
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            {
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                instance = new Singleton();
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            }

17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return instance;
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

这种方式的实现对于线程来说并不是安全的,因为在多线程的环境下有可能得到Singleton类的多个实例。如果同时有两个线程去判断(instance == null),并且得到的结果为真,这时两个线程都会创建类Singleton的实例,这样就违背了Singleton模式的原则。实际上在上述代码中,有可能在计算出表达式的值之前,对象实例已经被创建,但是内存模型并不能保证对象实例在第二个线程创建之前被发现。

 

该实现方式主要有两个优点:

l         由于实例是在 Instance 属性方法内部创建的,因此类可以使用附加功能(例如,对子类进行实例化),即使它可能引入不想要的依赖性。

l         直到对象要求产生一个实例才执行实例化;这种方法称为“惰性实例化”。惰性实例化避免了在应用程序启动时实例化不必要的 singleton

2.安全的线程

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 public   sealed   class  Singleton
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 3.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static Singleton instance=null;
 4.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static readonly object padlock = new object();
 5.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    Singleton()
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public static Singleton Instance
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        get
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            lock (padlock)
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            {
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                if (instance==null)
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                {
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                    instance = new Singleton();
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                }

20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                return instance;
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            }

22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

24.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

25 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
26 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

 

这 种方式的实现对于线程来说是安全的。我们首先创建了一个进程辅助对象,线程在进入时先对辅助对象加锁然后再检测对象是否被创建,这样可以确保只有一个实例 被创建,因为在同一个时刻加了锁的那部分程序只有一个线程可以进入。这种情况下,对象实例由最先进入的那个线程创建,后来的线程在进入时(instence == null)为假,不会再去创建对象实例了。但是这种实现方式增加了额外的开销,损失了性能。

3.双重锁定

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 public   sealed   class  Singleton
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 3.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static Singleton instance=null;
 4.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static readonly object padlock = new object();
 5.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    Singleton()
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public static Singleton Instance
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        get
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            if (instance==null)
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            {
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                lock (padlock)
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                {
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                    if (instance==null)
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                    {
20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                        instance = new Singleton();
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                    }

22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                }

23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            }

24.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return instance;
25.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

26.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

27.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

28 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

这种实现方式对多线程来说是安全的,同时线程不是每次都加锁,只有判断对象实例没有被创建时它才加锁,有了我们上面第一部分的里面的分析,我们知道,加锁后还得再进行对象是否已被创建的判断。它解决了线程并发问题,同时避免在每个 Instance 属性方法的调用中都出现独占锁定。它还允许您将实例化延迟到第一次访问对象时发生。实际上,应用程序很少需要这种类型的实现。大多数情况下我们会用静态初始化。这种方式仍然有很多缺点:无法实现延迟初始化。

4.静态初始化

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 public   sealed   class  Singleton
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 3.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static readonly Singleton instance=new Singleton();
 4.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 5.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    static Singleton()
 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    Singleton()
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public static Singleton Instance
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        get
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return instance;
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

21 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

看到上面这段富有戏剧性的代码,我们可能会产生怀疑,这还是Singleton模式吗?在此实现中,将在第一次引用类的任何成员时创建实例。公共语言运行库负责处理变量初始化。该类标记为 sealed 以阻止发生派生,而派生可能会增加实例。此外,变量标记为 readonly,这意味着只能在静态初始化期间(此处显示的示例)或在类构造函数中分配变量。

该实现与前面的示例类似,不同之处在于它依赖公共语言运行库来初始化变量。它仍然可以用来解决 Singleton 模式试图解决的两个基本问题:全局访问和实例化控制。公共静态属性为访问实例提供了一个全局访问点。此外,由于构造函数是私有的,因此不能在类本身以外实例化 Singleton 类;因此,变量引用的是可以在系统中存在的唯一的实例。

由于 Singleton 实例被私有静态成员变量引用,因此在类首次被对 Instance 属性的调用所引用之前,不会发生实例化。

这种方法唯一的潜在缺点是,您对实例化机制的控制权较少。在 Design Patterns 形式中,您能够在实例化之前使用非默认的构造函数或执行其他任务。由于在此解决方案中由 .NET Framework 负责执行初始化,因此您没有这些选项。在大多数情况下,静态初始化是在 .NET 中实现 Singleton 的首选方法。

5.延迟初始化

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 public   sealed   class  Singleton
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 3.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    Singleton()
 4.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 5.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public static Singleton Instance
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        get
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return Nested.instance;
12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    class Nested
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        static Nested()
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        internal static readonly Singleton instance = new Singleton();
22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

24 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

这里,初始化工作有Nested类的一个静态成员来完成,这样就实现了延迟初始化,并具有很多的优势,是值得推荐的一种实

现方式。

实现要点

l        Singleton模式是限制而不是改进类的创建。

l         Singleton类中的实例构造器可以设置为Protected以允许子类派生。

l         Singleton模式一般不要支持Icloneable接口,因为这可能导致多个对象实例,与Singleton模式的初衷违背。

l         Singleton模式一般不要支持序列化,这也有可能导致多个对象实例,这也与Singleton模式的初衷违背。

l         Singleton只考虑了对象创建的管理,没有考虑到销毁的管理,就支持垃圾回收的平台和对象的开销来讲,我们一般没必要对其销毁进行特殊的管理。

l         理解和扩展Singleton模式的核心是“如何控制用户使用new对一个类的构造器的任意调用”。

 

 

l         可以很简单的修改一个Singleton,使它有少数几个实例,这样做是允许的而且是有意义的

优点

l         实例控制:Singleton 会阻止其他对象实例化其自己的 Singleton 对象的副本,从而确保所有对象都访问唯一实例

l         灵活性:因为类控制了实例化过程,所以类可以更加灵活修改实例化过程

缺点

l         开销:虽然数量很少,但如果每次对象请求引用时都要检查是否存在类的实例,将仍然需要一些开销。可以通过使用静态初始化解决此问题,上面的五种实现方式中已经说过了。

l          可能的开发混淆:使用 singleton 对象(尤其在类库中定义的对象)时,开发人员必须记住自己不能使用 new 关键字实例化对象。因为可能无法访问库源代码,因此应用程序开发人员可能会意外发现自己无法直接实例化此类。

l         对象的生存期:Singleton 不能解决删除单个对象的问题。在提供内存管理的语言中(例如基于 .NET Framework 的语言),只有 Singleton 类能够导致实例被取消分配,因为它包含对该实例的私有引用。在某些语言中(如 C++),其他类可以删除
对象实例,但这样会导致 Singleton 类中出现悬浮引用。

适用性

l         当类只能有一个实例而且客户可以从一个众所周知的访问点访问它时。

l         当这个唯一实例应该是通过子类化可扩展的,并且客户应该无需更改代码就能使用一个扩展的实例时。

应用场景

l         每台计算机可以有若干个打印机,但只能有一个Printer Spooler,避免两个打印作业同时输出到打印机。
(摘自吕震宇的
C#设计模式(7)-Singleton Pattern

l         PC机中可能有几个串口,但只能有一个COM1口的实例。

l         系统中只能有一个窗口管理器。

l         .NET Remoting中服务器激活对象中的Sigleton对象,确保所有的客户程序的请求都只有一个实例来处理。

完整示例

这是一个简单的计数器例子,四个线程同时进行计数。

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System;
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System.Threading;
 3 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 4 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 namespace  SigletonPattern.SigletonCounter
 5 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 6.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// <summary>
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 功能:简单计数器的单件模式
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 编写:Terrylee
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 日期:2005年12月06日
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// </summary>

11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public class CountSigleton
12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        ///存储唯一的实例
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        static CountSigleton uniCounter = new CountSigleton();  
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        ///存储计数值
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        private int totNum = 0;  
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        private CountSigleton() 
20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            ///线程延迟2000毫秒
23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Thread.Sleep(2000);
24.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }
 
25.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
26.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        static public CountSigleton Instance() 
27.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
28.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
29.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
30.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return uniCounter; 
31.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
32.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }
 
33.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
34.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        ///计数加1
35.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public void Add()
36.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
37.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            totNum ++;
38.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }
  
39.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
40.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        ///获得当前计数值
41.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public int GetCounter()
42.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        
43.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            return totNum;
44.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }
 
45.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
46.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

47.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

48 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

 

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System;
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System.Threading;
 3 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System.Text;
 4 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 5 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 namespace  SigletonPattern.SigletonCounter
 6 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// <summary>
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 功能:创建一个多线程计数的类
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 编写:Terrylee
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 日期:2005年12月06日
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// </summary>

12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public class CountMutilThread
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public CountMutilThread()
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        /// <summary>
20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        /// 线程工作
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        /// </summary>

22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public static void DoSomeWork()
23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
24.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            ///构造显示字符串
25.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            string results = "";
26.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
27.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            ///创建一个Sigleton实例
28.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            CountSigleton MyCounter = CountSigleton.Instance();
29.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
30.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            ///循环调用四次
31.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            for(int i=1;i<5;i++)
32.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            {
33.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                ///开始计数
34.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                MyCounter.Add();
35.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                
36.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results +="线程";
37.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results += Thread.CurrentThread.Name.ToString() + "——〉";
38.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results += "当前的计数:";
39.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results += MyCounter.GetCounter().ToString();
40.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results += "\n";
41.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
42.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                Console.WriteLine(results);
43.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                
44.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                ///清空显示字符串
45.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页                results = "";
46.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            }

47.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

48.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
49.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public void StartMain()
50.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
51.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
52.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Thread thread0 = Thread.CurrentThread; 
53.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
54.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread0.Name = "Thread 0"
55.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
56.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Thread thread1 =new Thread(new ThreadStart(DoSomeWork)); 
57.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
58.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread1.Name = "Thread 1"
59.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
60.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Thread thread2 =new Thread(new ThreadStart(DoSomeWork)); 
61.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
62.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread2.Name = "Thread 2"
63.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
64.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Thread thread3 =new Thread(new ThreadStart(DoSomeWork)); 
65.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
66.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread3.Name = "Thread 3"
67.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
68.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread1.Start(); 
69.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
70.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread2.Start(); 
71.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页   
72.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            thread3.Start(); 
73.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            
74.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            ///线程0也只执行和其他线程相同的工作
75.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            DoSomeWork(); 
76.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

77.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

78.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

79 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

 

 1 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System;
 2 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System.Text;
 3 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 using  System.Threading;
 4 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
 5 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 namespace  SigletonPattern.SigletonCounter
 6 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页 {
 7.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// <summary>
 8.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 功能:实现多线程计数器的客户端
 9.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 编写:Terrylee
10.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// 日期:2005年12月06日
11.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    /// </summary>

12.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    public class CountClient
13.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    {
14.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        public static void Main(string[] args)
15.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        {
16.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页       CountMutilThread cmt = new CountMutilThread();
17.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
18.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            cmt.StartMain();
19.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页
20.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页            Console.ReadLine();
21.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页        }

22.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页    }

23.NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页}

24 .NET设计模式(2):单件模式(Singleton Pattern) - qiuguangchun - sandea的个人主页

 

 

总结

Singleton 设计模式是一个非常有用的机制,可用于在面向对象的应用程序中提供单个访问点。文中通过五种实现方式的比较和一个完整的示例,完成了对 Singleton 模式的一个总结和探索。用一句广告词来概括 Singleton 模式就是“简约而不简单”。

这篇关于.NET设计模式(2):单件模式(Singleton Pattern)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/300217

相关文章

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

.NET利用C#字节流动态操作Excel文件

《.NET利用C#字节流动态操作Excel文件》在.NET开发中,通过字节流动态操作Excel文件提供了一种高效且灵活的方式处理数据,本文将演示如何在.NET平台使用C#通过字节流创建,读取,编辑及保... 目录用C#创建并保存Excel工作簿为字节流用C#通过字节流直接读取Excel文件数据用C#通过字节

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

如何在Visual Studio中调试.NET源码

今天偶然在看别人代码时,发现在他的代码里使用了Any判断List<T>是否为空。 我一般的做法是先判断是否为null,再判断Count。 看了一下Count的源码如下: 1 [__DynamicallyInvokable]2 public int Count3 {4 [__DynamicallyInvokable]5 get

2、PF-Net点云补全

2、PF-Net 点云补全 PF-Net论文链接:PF-Net PF-Net (Point Fractal Network for 3D Point Cloud Completion)是一种专门为三维点云补全设计的深度学习模型。点云补全实际上和图片补全是一个逻辑,都是采用GAN模型的思想来进行补全,在图片补全中,将部分像素点删除并且标记,然后卷积特征提取预测、判别器判别,来训练模型,生成的像

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序

迭代器模式iterator

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/iterator 不暴露集合底层表现形式 (列表、 栈和树等) 的情况下遍历集合中所有的元素