Design Mode of Learning Series -C # singleton pattern

Original link: http://www.cnblogs.com/wysky/archive/2008/07/23/csharp_singleton.html

.Net today learned of the singleton pattern mainly into three.

1, a single mode single-threaded environment to achieve

 PS. In the absence of private, the compiler will automatically generate construction of a public. It is necessary to declare a private constructor private.

using  System;

namespace  Singleton
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Singleton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Singleton() { }

        
private static Singleton instance;
        
public static Singleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
if(instance == null)
                    instance 
= new Singleton();
                
return instance;
            }

        }

    }

}

 

2, to achieve the double locking of multiple threads

a using  System;

namespace  SigletonPattern.Sigleton
ExpandedBlockStart.gifContractedBlock.gif
{ / ** / /// <the Summary> ///  function: C # using double-lock mode to achieve a single piece ///  write: TerryLee ///  Date: December 6, 2005 / // </ Summary> public class  DoubLockSigleton { // volatile that the compiler can not optimize automatic debugging statements (statement to avoid adjusting the position and sequence resulting in incorrect initialization time point .sky). Private static volatile  DoubLockSigleton instance; / ** / // / <Summary> ///  secondary lock object itself meaningless /// </ Summary> Private static Object  SyncRoot  = new new
ExpandedSubBlockStart.gifContractedSubBlock.gif
 



 

 
ExpandedSubBlockStart.gifContractedSubBlock.gif



  

ExpandedSubBlockStart.gifContractedSubBlock.gif
 

 

    Object();

ExpandedSubBlockStart.gifContractedSubBlock.gif
/**//// <summary>
/// 构造方法改为Private
/// </summary>

private DoubLockSigleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif
{

}


public static DoubLockSigleton Instance
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
get
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
if (instance == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
lock (syncRoot)
ExpandedSubBlockStart.gifContractedSubBlock.gif
{
if (instance == null)
instance 
= new DoubLockSigleton();
}

}


return instance;
}

}


}

}

3, the legend of the method in the MSDN article provided treatment singleton pattern realization of the principle of using the C # static properties and static constructor.

 

using  System;

namespace  Singleton
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Singleton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private static readonly Singleton instance = new Singleton();
ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Singleton() { }
    }

}

 

编译后查看il可以发现其实是等同于下面的代码的

using  System;

namespace  Singleton
ExpandedBlockStart.gifContractedBlock.gif
{
    
public class Singleton
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
private static readonly Singleton instance;
        
static Singleton()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            instance 
= new Singleton();
        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
private Singleton() { }
    }

}

 

Because C # static properties and static constructor mechanism, call the static attribute instance, when will first perform a static constructor of the class, but also to ensure that only one thread can execute static constructors. So to achieve a singleton pattern.

 

 

 

Reproduced in: https: //www.cnblogs.com/wysky/archive/2008/07/23/csharp_singleton.html

Guess you like

Origin blog.csdn.net/weixin_30764771/article/details/94961448