C# 单例模式(Singleton)

摘要

       在咱们平常的工做中常常须要在应用程序中保持一个惟一的实例,如:IO处理,数据库操做等,因为这些对象都要占用重要的系统资源,因此咱们必须限制这些实例的建立或始终使用一个公用的实例,这就是咱们今天要介绍的——单例模式(Singleton)。html

       使用频率clip_image001数据库

       单件模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点。设计模式

正文

singleton

图1单例模式(Singleton)结构图安全

 

       单例模式(Singleton)是几个建立模式中最对立的一个,它的主要特色不是根据用户程序调用生成一个新的实例,而是控制某个类型的实例惟一性,经过 上图咱们知道它包含的角色只有一个,就是Singleton,它拥有一个私有构造函数,这确保用户没法经过new直接实例它。除此以外,该模式中包含一个 静态私有成员变量instance与静态公有方法Instance()。Instance()方法负责检验并实例化本身,而后存储在静态成员变量中,以确 保只有一个实例被建立。服务器

 

clip_image002

图2单例模式(Singleton)逻辑模型多线程

 

       接下来咱们将介绍6中不一样的单例模式(Singleton)的实现方式。这些实现方式都有如下的共同点:less

 

    1. 有一个私有的无参构造函数,这能够防止其余类实例化它,并且单例类也不该该被继承,若是单例类容许继承那么每一个子类均可以建立实例,这就违背了Singleton模式“惟一实例”的初衷。
    2. 单例类被定义为sealed,就像前面提到的该类不该该被继承,因此为了保险起见能够把该类定义成不容许派生,但没有要求必定要这样定义。
    3. 一个静态的变量用来保存单实例的引用。
    4. 一个公有的静态方法用来获取单实例的引用,若是实例为null即建立一个。

版本一线程不安全

/// <summary>
/// A simple singleton class implements.
/// </summary>
public sealed class Singleton
{
    private static Singleton _instance = null;

    /// <summary>
    /// Prevents a default instance of the 
    /// <see cref="Singleton"/> class from being created.
    /// </summary>
    private Singleton()
    {
    }

    /// <summary>
    /// Gets the instance.
    /// </summary>
    public static Singleton Instance
    {
        get { return _instance ?? (_instance = new Singleton()); }
    }
}
Singleton_1

  以上的实现方式适用于单线程环境,由于在多线程的环境下有可能获得Singleton类的多个实例。假如同时有两个线程去判断(null == _singleton),而且获得的结果为真,那么两个线程都会建立类Singleton的实例,这样就违背了Singleton模式“惟一实例”的初衷。dom

版本二线程安全

/// <summary>
/// A thread-safe singleton class.
/// </summary>
public sealed class Singleton
{
    private static Singleton _instance = null;
    private static readonly object SynObject = new object();

    Singleton()
    {
    }

    /// <summary>
    /// Gets the instance.
    /// </summary>
    public static Singleton Instance
    {
        get
        {
            // Syn operation.
            lock (SynObject)
            {
                return _instance ?? (_instance = new Singleton());
            }
        }
    }
}
Singleton_2

  以上方式的实现方式是线程安全的,首先咱们建立了一个静态只读的进程辅助对象,因为lock是确保当一个线程位于代码的临界区时,另外一个线程不能进入临界 区(同步操做)。若是其余线程试图进入锁定的代码,则它将一直等待,直到该对象被释放。从而确保在多线程下不会建立多个对象实例了。只是这种实现方式要进 行同步操做,这将是影响系统性能的瓶颈和增长了额外的开销。ide

Double-Checked Locking

  前面讲到的线程安全的实现方式的问题是要进行同步操做,那么咱们是否能够下降经过操做的次数呢?其实咱们只需在同步操做以前,添加判断该实例是否为null就能够下降经过操做的次数了,这样是经典的Double-Checked Locking方法。函数

/// <summary>
/// Double-Checked Locking implements a thread-safe singleton class
/// </summary>
public sealed class Singleton
{
    private static Singleton _instance = null;
    // Creates an syn object.
    private static readonly object SynObject = new object();

    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            // Double-Checked Locking
            if (null == _instance)
            {
                lock (SynObject)
                {
                    if (null == _instance)
                    {
                        _instance = new Singleton();
                    }
                }
            }
            return _instance;
        }
    }
}
Singleton_3

  在介绍第四种实现方式以前,首先让咱们认识什么是,当字段被标记为beforefieldinit类型时,该字段初始化能够发生在任什么时候候任何字段被引用以前。这句话听起了有点别扭,接下来让咱们经过具体的例子介绍。

/// <summary>
/// Defines a test class.
/// </summary>
class Test
{
    public static string x = EchoAndReturn("In type initializer");

    public static string EchoAndReturn(string s)
    {
        Console.WriteLine(s);
        return s;
    }
}

上面咱们定义了一个包含静态字段和方法的类Test,但要注意咱们并无定义静态的构造函数。

 

singleton2

图3 Test类的IL代码

class Test
{
    public static string x = EchoAndReturn("In type initializer");

    // Defines a parameterless constructor.
    static Test()
    {
    }

    public static string EchoAndReturn(string s)
    {
        Console.WriteLine(s);
        return s;
    }
}

上面咱们给Test类添加一个静态的构造函数。

 

   singleton3

图4 Test类的IL代码

 

       经过上面Test类的IL代码的区别咱们发现,当Test类包含静态字段,并且没有定义静态的构造函数时,该类会被标记为beforefieldinit。

       如今也许有人会问:“被标记为beforefieldinit和没有标记的有什么区别呢”?OK如今让咱们经过下面的具体例子看一下它们的区别吧!

class Test
{
    public static string x = EchoAndReturn("In type initializer");

    static Test()
    {
    }

    public static string EchoAndReturn(string s)
    {
        Console.WriteLine(s);
        return s;
    }
}

class Driver
{
    public static void Main()
    {
        Console.WriteLine("Starting Main");
        // Invoke a static method on Test
        Test.EchoAndReturn("Echo!");
        Console.WriteLine("After echo");
        Console.ReadLine();

        // The output result:
        // Starting Main
        // In type initializer
        // Echo!
        // After echo            
    }
}

  我相信你们均可以获得答案,若是在调用EchoAndReturn()方法以前,须要完成静态成员的初始化,因此最终的输出结果以下:

 

singleton4

图5输出结果

    接着咱们在Main()方法中添加string y = Test.x,以下:

public static void Main()
{
    Console.WriteLine("Starting Main");
    // Invoke a static method on Test
    Test.EchoAndReturn("Echo!");
    Console.WriteLine("After echo");

    //Reference a static field in Test
    string y = Test.x;
    //Use the value just to avoid compiler cleverness
    if (y != null)
    {
        Console.WriteLine("After field access");
    }
    Console.ReadKey();

    // The output result:
    // In type initializer
    // Starting Main
    // Echo!
    // After echo
    // After field access

}

singleton5

图6 输出结果

        经过上面的输出结果,你们能够发现静态字段的初始化跑到了静态方法调用以前,Wo不可思议啊!

        最后咱们在Test类中添加一个静态构造函数以下:

class Test
{
    public static string x = EchoAndReturn("In type initializer");

    static Test()
    {
    }

    public static string EchoAndReturn(string s)
    {
        Console.WriteLine(s);
        return s;
    }
}

singleton6

图7 输出结果

 

       理论上,type initializer应该发生在”Echo!”以后和”After echo”以前,但这里却出现了不惟一的结果,只有当Test类包含静态构造函数时,才能确保type initializer的初始化发生在”Echo!”以后和”After echo”以前。

因此说要确保type initializer发生在被字段引用时,咱们应该给该类添加静态构造函数。接下来让咱们介绍单例模式的静态方式。

静态初始化

public sealed class Singleton
{
    private static readonly Singleton _instance = new Singleton();

    // Explicit static constructor to tell C# compiler
    // not to mark type as beforefieldinit
    static Singleton()
    {
    }

    /// <summary>
    /// Prevents a default instance of the 
    /// <see cref="Singleton"/> class from being created.
    /// </summary>
    private Singleton()
    {
    }

    /// <summary>
    /// Gets the instance.
    /// </summary>
    public static Singleton Instance
    {
        get
        {
            return _instance;
        }
    }
}
Singleton_4

  以上方式实现比以前介绍的方式都要简单,但它确实是多线程环境下,C#实现的Singleton的一种方式。因为这种静态初始化的方式是在本身的字段被引用时才会实例化。

     让咱们经过IL代码来分析静态初始化。

 

singleton7

图8静态初始化IL代码

 

        首先这里没有beforefieldinit的修饰符,因为咱们添加了静态构造函数当静态字段被引用时才进行初始化,所以即使不少线程试图引用_instance,也须要等静态构造函数执行完并把静态成员_instance实例化以后可使用。

延迟初始化

/// <summary>
/// Delaies initialization.
/// </summary>
public sealed class Singleton
{
    private Singleton()
    {
    }

    /// <summary>
    /// Gets the instance.
    /// </summary>
    public static Singleton Instance { get { return Nested._instance; } }

    private class Nested
    {
        // Explicit static constructor to tell C# compiler
        // not to mark type as beforefieldinit
        static Nested()
        {
        }

        internal static readonly Singleton _instance = new Singleton();
    }
}
Singleton_5

  这里咱们把初始化工做放到Nested类中的一个静态成员来完成,这样就实现了延迟初始化。

Lazy<T> type

/// <summary>
/// .NET 4's Lazy<T> type
/// </summary>
public sealed class Singleton
{
    private static readonly Lazy<Singleton> lazy =
        new Lazy<Singleton>(() => new Singleton());

    public static Singleton Instance { get { return lazy.Value; } }

    private Singleton()
    {
    }
}
Singleton_6

  这种方式的简单和性能良好,并且还提供检查是否已经建立实例的属性IsValueCreated。

具体例子

     如今让咱们使用单例模式(Singleton)实现负载平衡器,首先咱们定义一个服务器类,它包含服务器名和IP地址以下:

/// <summary>
/// Represents a server machine
/// </summary>
class Server
{
    // Gets or sets server name
    public string Name { get; set; }

    // Gets or sets server IP address
    public string IP { get; set; }
}

  因为负载平衡器只提供一个对象实例供服务器使用,因此咱们使用单例模式(Singleton)实现该负载平衡器。

/// <summary>
/// The 'Singleton' class
/// </summary>
sealed class LoadBalancer
{
    private static readonly LoadBalancer _instance =
        new LoadBalancer();

    // Type-safe generic list of servers
    private List<Server> _servers;
    private Random _random = new Random();

    static LoadBalancer()
    {
    }

    // Note: constructor is 'private'
    private LoadBalancer()
    {
        // Load list of available servers
        _servers = new List<Server> 
            { 
              new Server{ Name = "ServerI", IP = "192.168.0.108" },
              new Server{ Name = "ServerII", IP = "192.168.0.109" },
              new Server{ Name = "ServerIII", IP = "192.168.0.110" },
              new Server{ Name = "ServerIV", IP = "192.168.0.111" },
              new Server{ Name = "ServerV", IP = "192.168.0.112" },
            };
    }

    /// <summary>
    /// Gets the instance through static initialization.
    /// </summary>
    public static LoadBalancer Instance
    {
        get { return _instance; }
    }


    // Simple, but effective load balancer
    public Server NextServer
    {
        get
        {
            int r = _random.Next(_servers.Count);
            return _servers[r];
        }
    }
}
负载平衡器类LoadBalancer咱们使用静态初始化方式实现单例模式(Singleton)
static void Main()
{
    LoadBalancer b1 = LoadBalancer.Instance;
    b1.GetHashCode();
    LoadBalancer b2 = LoadBalancer.Instance;
    LoadBalancer b3 = LoadBalancer.Instance;
    LoadBalancer b4 = LoadBalancer.Instance;

    // Confirm these are the same instance
    if (b1 == b2 && b2 == b3 && b3 == b4)
    {
        Console.WriteLine("Same instance\n");
    }

    // Next, load balance 15 requests for a server
    LoadBalancer balancer = LoadBalancer.Instance;
    for (int i = 0; i < 15; i++)
    {
        string serverName = balancer.NextServer.Name;
        Console.WriteLine("Dispatch request to: " + serverName);
    }

    Console.ReadKey();
}

clip_image002[9]

图9 LoadBalancer输出结果

总结

单例模式的优势:

单例模式(Singleton)会控制其实例对象的数量,从而确保访问对象的惟一性。

  1. 实例控制:单例模式防止其它对象对本身的实例化,确保全部的对象都访问一个实例。
  2. 伸缩性:由于由类本身来控制实例化进程,类就在改变实例化进程上有相应的伸缩性。

 

单例模式的缺点:

  1. 系统开销。虽然这个系统开销看起来很小,可是每次引用这个类实例的时候都要进行实例是否存在的检查。这个问题能够经过静态实例来解决。
  2. 开发混淆。当使用一个单例模式的对象的时候(特别是定义在类库中的),开发人员必需要记住不能使用new关键字来实例化对象。由于开发者看不到在类库中的源代码,因此当他们发现不能实例化一个类的时候会很惊讶。
  3. 对象生命周期。单例模式没有提出对象的销毁。在提供内存管理的开发语言(好比,基于.NetFramework的语言)中,只有单例模式对象 本身才能将对象实例销毁,由于只有它拥有对实例的引用。在各类开发语言中,好比C++,其它类能够销毁对象实例,可是这么作将致使单例类内部的指针指向不 明。

 

单例适用性

使用Singleton模式有一个必要条件:在一个系统要求一个类只有一个实例时才应当使用单例模式。反之,若是一个类能够有几个实例共存,就不要使用单例模式。

不要使用单例模式存取全局变量。这违背了单例模式的用意,最好放到对应类的静态成员中。

不要将数据库链接作成单例,由于一个系统可能会与数据库有多个链接,而且在有链接池的状况下,应当尽量及时释放链接。Singleton模式因为使用静态成员存储类实例,因此可能会形成资源没法及时释放,带来问题。

 

 

========================================================

单例模式(Singleton)

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

C#设计模式(1)——单例模式

相关文章
相关标签/搜索