C # singleton

Singleton

Sometimes we do not create too many instances in one program. Just want to use the example of a global and a possible access point. So we need a single case class.

Because it is a singleton class, the constructor must be private.

Terms to Understand

Lazy style

As the name suggests. When to use it and when to go to new instance.

such as:

class Singleton
{
    private static Singleton instance = null;

    private Singleton()
    {

    }

    public static Singleton Instance
    {
        get
        {
            if(instance == null)
            {
                instance = new Singleton();
            }

            return instance;
        }
    }
}

Hungry Chinese-style

Have not used, there were already instances of objects

Altered, such as the above code:

class Singleton
{
    private Singleton()
    {

    }

    public static Singleton Instance { get; } = new Singleton();
}

But equally, so we do not know in the end it is time to construct.

Thread safe / secure

After all, a new object is a dynamic process. Step or a little of. If multiple threads access the same instance. Because of the shared variable. The former may result in a not good, there is a case of coming back. Finally, how many threads, how many instances you create. This is called thread-safe.

On the contrary, if not of the same class affected. It is safe.

The first code snippet is unsafe. The second segment is the security code (before the thread has not appeared instantiated, thus starving threading must be safe).

Thread safe lazy style

Because we fear shared variables. So lock up.

class Singleton
{
    private static Singleton instance;
    private readonly static object lockobj = new object();
    private Singleton()
    {

    }

    public static Singleton Instance
    {
        get
        {
            lock (lockobj)
            {
                if (instance == null)
                {
                    instance = new Singleton();
                }
            }

            return instance;
        }
    }
}

But each time the lock, it must be very slow. Thus, only the first time intends to create a lock.

class Singleton
{
    private static Singleton instance;
    private readonly static object lockobj = new object();
    private Singleton()
    {

    }

    public static Singleton Instance
    {
        get
        {
            if(instance == null)
            {
                lock (lockobj)
                {
                    if (instance == null)
                    {
                        instance = new Singleton();
                    }
                }
            }

            return instance;
        }
    }
}

If you still do not know when to be afraid to build. Internal class can be used. Ensure it

public class Singleton
{
    class Nested
    {
        internal static readonly Singleton instance = new Singleton();
    }
    private Singleton() { }
    public static Singleton Instance { get { return Nested.instance; } }
}

Example achieve a single base class

The last is to look at others.

public  abstract  class the Singleton <T> WHERE T: class 
{ 
    class the Nested 
    { 
        // create a template class instance, parameter 2 is set to true private constructors support 
        Internal  static  Readonly T = the Activator.CreateInstance instance ( typeof (T), true ) AS T; 
    } 
    Private  static T instance = null ;
     public  static T instance { GET { return Nested.instance;}} 
} 

class TestSingleton: the Singleton <TestSingleton> 
{
    // constructors privatized to prevent foreign created by the new new 
    Private TestSingleton () {} 
}

 

Guess you like

Origin www.cnblogs.com/yinghualuowu/p/11850997.html