Singleton Design Parttern

Posted 云霄宇霁

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Singleton Design Parttern相关的知识,希望对你有一定的参考价值。

单例模式是最常见也是最简单的设计模式,保证一个类只有一个实例并且提供一个全局访问点,主要解决实例被频繁的创建和销毁可能带来内存消耗问题。

单例模式的特点:

   1、私有无参构造函数

    2、私有静态变量存储单个实例创建的引用

    3、共有静态的全局访问点(可以是方式也可以是属性)

以下介绍多种实现方式:

1、线程不安全的单例

    The multiple threads could have evaluated the test if (_intance==null) and fount it to be true, then multiple instances will be created, which violates the singleton parttern.

    public sealed class SingletonPartternDemoV1
    
        private static SingletonPartternDemoV1? _instance;
        private SingletonPartternDemoV1()  

        public static SingletonPartternDemoV1 Instance
        
            get
            
                if (_instance == null)
                    _instance = new SingletonPartternDemoV1();
                return _instance;
            
        
    
thread-unsafe

2、线程安全单例

    创建实例时加锁(lock),在同一个时刻只允许一个线程创建实例,带来的问题是效率,多个线程获取实例需要等待。

public sealed class SingletonPartternDemoV2
    
        private static readonly object _obj = new object();
        private static SingletonPartternDemoV2? _instance;
        private SingletonPartternDemoV2()  

        public static SingletonPartternDemoV2 Instance
        
            get
            
                lock (_obj)
                    if (_instance == null)
                        _instance = new SingletonPartternDemoV2();
                return _instance;
            
        
    
thread-safe

3、线程安全双重检测

   可以检测多个线程获取实例等待的问题

public sealed class SingletonPartternDemoV3
    
        private static readonly object _obj = new object();
        private static SingletonPartternDemoV3? _instance;
        private SingletonPartternDemoV3()  

        public static SingletonPartternDemoV3 Instance
        
            get
            
                if (_instance == null)
                    lock (_obj)
                        if (_instance == null)
                            _instance = new SingletonPartternDemoV3();
                return _instance;
            
        
    
thread-safe double checking

4、线程安全没有锁

   通过私有静态属性初始化实例

public sealed class SingletonDemoV4
    
        private static readonly SingletonDemoV4 instance = new SingletonDemoV4();
        static SingletonDemoV4()  
        private SingletonDemoV4()  
        public static SingletonDemoV4 Instance
        
            get
            
                return instance;
            
        
    
Thread Safe Singleton without using locks and no lazy instantiation

5、System.Lazy

/// <summary>
    /// 1 Using .Net4 or higher then you can use the System.Lazy<T> type to make the laziness really simple
    /// 2 You can pass a delegate to the constructor that calls the singleton constructor, which is done most easily with a lambda expression.
    /// 3 Allow you to check whether or not the instance has beed created with the IsValueCreated property.
    /// </summary>
    public sealed class SingletonDemoV5
    
        private SingletonDemoV5()  
        private static readonly Lazy<SingletonDemoV5> instance = new Lazy<SingletonDemoV5>(() => new SingletonDemoV5(), true);
        public static SingletonDemoV5 Instance
        
            get
            
                return instance.IsValueCreated ? instance.Value : new SingletonDemoV5();
            
        
    
System.Lazy

 

以上是关于Singleton Design Parttern的主要内容,如果未能解决你的问题,请参考以下文章

Design Pattern - Singleton

[Design Pattern] Singleton Pattern 简单案例

如何使用Singleton Design Pattern java连接到SQL数据库?

从壹开始 [ Design Pattern ] 之二 ║ 单例模式 与 Singleton

Java设计模式 Design Pattern单例模式 Singleton

[翻译] 单例(Singleton)