zoukankan      html  css  js  c++  java
  • 设计模式:单件模式

    Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点。
    
    1. 单线程时方法
    
    public sealed class Singlton 
    { 
        static Singlton instance = null; 
        Singlton() 
        { } 
      
        public static Singlton Instance 
        { 
            get
            { 
                if (instance == null) 
                { 
                    return new Singlton(); 
                } 
                return instance; 
            }     
        }     
    } 
    这句if (instance == null)不是线程安全的,可能产生多个实例。
    
    2.线程安全的
    public sealed class Singlton 
    { 
        static Singlton instance = null; 
      
        static readonly object o = new object(); 
      
        Singlton() 
        { } 
      
        public static Singlton Instance 
        { 
            get
            { 
                lock (o) 
                { 
                    if (instance == null) 
                    { 
                        return new Singlton(); 
                    } 
                    return instance; 
                } 
            }     
        }     
    } 
    对象实例由最先进入的那个线程创建,后来的线程在进入时(instence == null)为假,不会再去创建对象实例了。但是这种实现方式增加了额外的开销,损失了性能。
    
    3. 双重锁定
     public sealed class Singlton 
    { 
        static Singlton instance = null; 
      
        static readonly object o = new object(); 
      
        Singlton() 
        { } 
      
        public static Singlton Instance 
        { 
            get
            { 
                if (instance == null) 
                { 
                    lock (o) 
                    { 
                        if (instance == null) 
                        { 
                            return new Singlton(); 
                        } 
                    } 
                } 
                return instance; 
            } 
        } 
    } 
    避免了每个 Instance 属性方法的调用中都出现独占锁定。
    
    4. 静态初始化
    public sealed class Singlton 
    { 
        static readonly Singlton instance = new Singlton(); 
      
        static Singlton() 
        { } 
      
        public static Singlton Instance 
        { 
            get
            { 
                return instance; 
            } 
        } 
    } 
    5. 延迟静态初始化
    
     public sealed class Singlton 
    {        
      
        static Singlton() 
        { } 
          
        public static Singlton Instance 
        { 
            get
            { 
                return CreateSinglton.instance; 
            } 
        } 
          
        class CreateSinglton 
        { 
            internal static readonly Singlton instance = new Singlton(); 
          
            static CreateSinglton() { } 
        } 
    } 
  • 相关阅读:
    P1268 树的重量
    P2419 [USACO08JAN]牛大赛Cow Contest
    P1306 斐波那契公约数
    P2905 [USACO08OPEN]农场危机Crisis on the Farm
    P1081 开车旅行
    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods
    P1550 [USACO08OCT]打井Watering Hole
    P2746 [USACO5.3]校园网Network of Schools
    P1613 跑路
    【BZOJ4868】期末考试(整数三分)
  • 原文地址:https://www.cnblogs.com/profession/p/5073069.html
Copyright © 2011-2022 走看看