定义 :确保一个类只有一个实例,并提供一个全局访问点。
 
思路
 
将类的默认工造函数私有化,在类中实例化,再提供一个全局访问点。
 
代码
 
     
    
    
    public class Singleton
    {
        
        private static Singleton uniqueInstance
;
        
        private static readonly object locker 
= new object();
        
        private Singleton()
        {
        }
        
        
        
        
        public static Singleton GetInstance()
        {
            
            
            
            
            if (uniqueInstance 
== null)
            {
                lock (locker
)
                {
                    
                    if (uniqueInstance 
== null)
                    {
                        uniqueInstance 
= new Singleton();
                    }
                }
            }
            return uniqueInstance
;
        }
    }