zoukankan      html  css  js  c++  java
  • 【设计模式】单例模式 Singleton Pattern

    通常我们在写程序的时候会碰到一个类只允许在整个系统中只存在一个实例(Instance)  的情况, 比如说我们想做一计数器,统计某些接口调用的次数,通常我们的数据库连接也是只期望有一个实例。Windows系统的系统任务管理器也是始终只有一个,如果你打开了windows管理器,你再想打开一个那么他还是同一个界面(同一个实例), 还有比如 做.Net平台的人都知道,AppDomain 对象,一个系统中也只有一个,所有的类库都会加载到AppDomain中去运行。只需要一个实例对象的场景,随处可见,那么有么有什么好的解决方法来应对呢? 有的,那就是 单例模式。

    一、单例模式定义

    单例模式(Singleton Pattern):确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例,这个类称为单例类,它提供全局访问的方法。单例模式是一种对象创建型模式。

    二、单例模式结构图

    image

    • Singleton(单例):在单例类的内部实现只生成一个实例,同时它提供一个静态的GetInstance()工厂方法,让客户可以访问它的唯一实例;为了防止在外部对其实例化,将其构造函数设计为私有(private);在单例类内部定义了一个Singleton类型的静态对象,作为外部共享的唯一实例。

    三、 单例模式典型代码

    public class Singleton
    {
        private static Singleton instance;
        private Singleton()
        {
        }
        public static Singleton GetInstance()
        {
            if(instance==null)
            {
                instance=new Singleton();
            }
    
            return instance;
        }
    }

    客户端调用代码:

    static void Main(string[] args)
    {
        Singleton singleto = Singleton.GetInstance();
    }

    在C#中经常将统一访问点暴露出一个只读的属性供客户端程序使用,这样代码就变成了这样:

    public class Singleton
    {
        private static Singleton instance;
        private Singleton()
        {
        }
        public static Singleton GetInstance
        {
            get
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
    
                return instance;
            }
        }
    }

    客户端调用:

    static void Main(string[] args)
    {
        Singleton singleton = Singleton.GetInstance;
    }

    四、单例模式实例

    1. 懒汉模式

    假如我们要做一个程序计数器,一旦程序启动无论多少个客户端调用这个 计数器计数的结果始终都是在前一个的基础上加1,那么这个计数器类就可以设计成一个单例模式的类。

    public class SingletonCounter
    {
        private static SingletonCounter instance;
        private static int number=0;
        private SingletonCounter() { }
        public static SingletonCounter Instance
        {
            get
            {
                if (instance == null) instance = new SingletonCounter();
    
                number++;
                return instance;
            }
        }
    
        public int GetCounter(){
            return number;
        }
    }

    客户端调用:

    static void Main(string[] args)
    {
        //App A call the counter;
        SingletonCounter singletonA = SingletonCounter.Instance;
        int numberA = singletonA.GetCounter();
        Console.WriteLine("App A call the counter get number was:" + numberA);
    
        //App B call the counter;
        SingletonCounter singletonB = SingletonCounter.Instance;
        int numberB = singletonA.GetCounter();
        Console.WriteLine("App B call the counter get number was:" + numberB);
    
        Console.ReadKey();
    }

    输出结果:

    image

    这个实现是线程不安全的,如果有多个线程同时调用,并且又恰恰在计数器初始化的瞬间多个线程同时检测到了 instance==null为true情况,会怎样呢?这就是下面要讨论的 “加锁懒汉模式”

    2、加锁懒汉模式

    多个线程同时调用并且同时检测到 instance == null 为 true的情况,那后果就是会出现多个实例了,那么就无法保证唯一实例了,解决这个问题就是增加一个对象锁来确保在创建的过程中只有一个实例。(锁可以确保锁住的代码块是线程独占访问的,如果一个线程占有了这个锁,其它线程只能等待该线程释放锁以后才能继续访问)。

    public class SingletonCounter
    {
        private static SingletonCounter instance;
        private static readonly object locker = new object();
        private static int number = 0;
        private SingletonCounter() { }
        public static SingletonCounter Instance
        {
            get
            {
                lock (locker)
                {
                    if (instance == null) instance = new SingletonCounter();
    
                    number++;
                    return instance;
                }
            }
        }
    
        public int GetCounter()
        {
            return number;
        }
    }

    客户端调用代码:

    static void Main(string[] args)
    { 
        for (int i = 1; i < 100; i++)
        {
            var task = new Task(() =>
            {
                SingletonCounter singleton = SingletonCounter.Instance;
                int number = singleton.GetCounter();
    
                Console.WriteLine("App  call  the counter get number was:" +  number);
    
            });
            task.Start();
        }
        Console.ReadKey();
    }

    输出结果:

    image

    这种模式是线程安全,即使在多线程的情况下仍然可以保持单个实例。那么这种模式会不会有什么问题呢?假如系统的访问量非常大,并发非常高,那么计数器就会是一个性能瓶颈,因为对锁会使其它的线程无法访问。在访问量不大,并发量不高的系统尚可应付,如果高访问量,高并发的情况下这样做肯定是不行的,那么有什么办法改进呢?这就是下面要讨论的“双检查加锁懒汉模式”。

    3、双检查加锁懒汉模式

    加锁懒汉模式虽然保证了系统的线程安全,但是却为系统带来了新能问题,主要的性能来自锁带来开销,双检查就是解决这个锁带来的问题,在锁之前再做一次 instance==null的检查,如果返回true就直接返回 单例对象了,避开了无谓的锁, 我们来看下,双检查懒汉模式代码:

    public class DoubleCheckLockSingletonCounter
    {
        private static DoubleCheckLockSingletonCounter instance;
        private static readonly object locker = new object();
        private static int number = 0;
        private DoubleCheckLockSingletonCounter() { }
        public static DoubleCheckLockSingletonCounter Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new DoubleCheckLockSingletonCounter();
                        }
                    }
                }
                number++;
                return instance;
            }
        }
    
        public int GetCounter()
        {
            return number;
        }
    }

    客户端调用代码和“懒汉加锁模式”相同,输出结果也相同。

    4、饿汉模式

    单例模式除了我们上面讲的三种懒汉模式外,还有一种叫“饿汉模式”的实现方式,“饿汉模式”直接在Singleton类里实例化了当前类的实例,并且保存在一个静态对象中,因为是静态对象,所以在程序启动的时候就已经实例化好了,后面直接使用,因此不存在线程安全的问题。

    下面是“饿汉模式”的代码实现:

    public class EagerSingletonCounter
    {
        private static EagerSingletonCounter instance = new EagerSingletonCounter();
    
        private static int number = 0;
        private EagerSingletonCounter() { }
        public static EagerSingletonCounter Instance
        {
            get
            {
                number++;
                return instance;
            }
        }
    
        public int GetCounter()
        {
            return number;
        }
    }

    五、单例模式应用场景

    单例模式只有一个角色非常简单,使用的场景也很明确,就是一个类只需要、且只能需要一个实例的时候使用单例模式。

    六、扩展

    1、”饿汉模式“和”懒汉模式“的比较

    ”饿汉模式“在程序启动的时候就已经实例化好了,并且一直驻留在系统中,客户程序调用非常快,因为它是静态变量,虽然完美的保证线程的安全,但是如果创建对象的过程很复杂,要占领系统或者网络的一些昂贵的资源,但是在系统中使用的频率又极低,甚至系统运行起来后都不会去使用该功能,那么这样一来,启动之后就一直占领着系统的资源不释放,这有些得不偿失。

    “懒汉模式“ 恰好解决了”饿汉模式“这种占用资源的问题,”懒汉模式”将类的实例化延迟到了运行时,在使用时的第一次调用时才创建出来并一直驻留在系统中,但是为了解决线程安全问题, 使用对象锁也是 影响了系统的性能。这两种模式各有各的好处,但是又各有其缺点。

    有没有一种折中的方法既可以避免一开始就实例化且一直占领系统资源,又没有性能问题的Singleton呢? 答案是:有的。

    2、第三种选择

    “饿汉模式“类不能实现延迟加载,不管用不用始终占据内存;”懒汉式模式“类线程安全控制烦琐,而且性能受影响。我们用一种折中的方法来解决这个问题,针对主要矛盾, 即:既可以延时加载又不影响性能。

    在Singleton的内部创建一个私有的静态类用于充当单例类的”初始化器“,专门用来创建Singleton的实例:

    public class BestPracticeSingletonCounter
    {
        private static class SingletonInitializer{
            public static BestPracticeSingletonCounter instance = new BestPracticeSingletonCounter();
        } 
        
        private static int number = 0;
        private BestPracticeSingletonCounter() { }
        public static BestPracticeSingletonCounter Instance
        {
            get
            {
                number++;
                return SingletonInitializer.instance;
            }
        }
    
        public int GetCounter()
        {
            return number;
        }
    }
    

    这种模式兼具了”饿汉“和”懒汉“模式的优点有摒弃了其缺点,可以说是一个完美的实现。

  • 相关阅读:
    优先级队列
    内存拷贝
    快速排序
    ubuntu上编译Thrift
    Hadoop 学习总结之一:HDFS简介(zz写的很不错)
    hbase 详细解析(转载)
    HBase技术介绍简介 NOSQL数据库 入门(转载)
    SQL到NOSQL的思维转变
    Hadoop分布式文件系统:架构和设计(zz)
    zz我对技术方向的一些反思
  • 原文地址:https://www.cnblogs.com/vaiyanzi/p/9424419.html
Copyright © 2011-2022 走看看