Big talk design mode-Notes (Singleton Mode)

Source: Internet
Author: User

Singleton mode: ensures that a class has only one instance and provides a global point for access.
Other advantages: the singleton mode is the only instance of the plug-in, so that it can strictly control how customers access it and when to access it.
In short, it is controlled access to a unique instance.

 

/// <Summary> <br/> // defines a getinstance operation to allow the customer to access its unique instance, getinstance is a static method <br/> // It is mainly used to create its own unique instance <br/> /// </Summary> <br/> class Singleton <br/> {<br/> Private Static Singleton instance; <br/> /// <summary> <br/> // The constructor is used to make it private. This indicates that new () cannot be used by the outside world () <br/> /// </Summary> <br/> private Singleton () <br/>{}< br/> /// <summary> <br/> // obtain the unique global access point of the instance <br/> /// </ summary> <br/> // <returns> </returns> <br/> Public static Singleton getinstance () <br/>{< br/> If (instance = NULL) <br/>{< br/> instance = new Singleton (); <br/>}< br/> return instance; <br/>}< br/>}

 

 

Call:

 

Class Program <br/>{< br/> static void Main (string [] args) <br/>{< br/> Singleton singleton0 = Singleton. getInstance (); <br/> Singleton singleton1 = Singleton. getInstance (); <br/> if (singleton0 = singleton1) <br/>{< br/> Console. writeLine ("two objects are the same instance"); <br/>}< br/> Console. read (); <br/>}< br/>}

 

The preceding methods are insufficient.

In a multi-threaded program, multiple threads access the singleton class at the same time and call the GetInstance () method, which may lead to the creation of multiple instances.
In this case, add a lock for processing. (Lock)

Singleton for Multithreading

 

Now we have improved the Singleton class.

 

 

/// <Summary> <br/> // define a GetInstance operation in the thread to allow the customer to access its unique instance, getInstance is a static method <br/> // It is mainly used to create its own unique instance <br/> /// </summary> <br/> class SynSingleton <br/> {<br/> private static SynSingleton synsingleton; <br/> private static readonly object synroot = new object (); // create a static read-only process auxiliary object <br/> /// <summary> <br/> // The constructor to make it private, this indicates that new () cannot be used to create objects. <br/> /// </summary> <br/> private SynSingleton () <br/>{< br/>}< br/> public static SynSingleton GetInstance () <br/>{< br/> lock (synroot) // only one thread can enter after being locked at the same time <br/>{< br/> if (synsingleton = null) <br/>{< br/> synsingleton = new SynSingleton (); <br/>}< br/> return synsingleton; <br/>}< br/>}

 

 

Lock ensures that when one thread is located in the Code critical section, the other thread does not enter the critical section. If other threads attempt to enter the lock code,
It will wait until the object is released.
The thread lock used in the SynSingleton class can achieve the goal, but it has an impact on performance.
You can use Double Locking to improve the performance.

 

As follows:

 

/// <Summary> <br/> // (with double lock) the thread defines a getinstance operation, allowing the customer to access its unique instance, getinstance is a static method <br/> // mainly used to create a unique instance <br/> class doublesynsingleton <br/>{< br/> Private Static doublesynsingleton doublesyninstance; <br/> Private Static readonly object doublesynroot = new object (); <br/> private doublesynsingleton () <br/>{}< br/> Public static doublesynsingleton getinstance () <br/>{< br/> If (doublesyninstance = NULL) <br/>{< br/> lock (doublesynroot) <br/>{< br/> If (doublesyninstance = NULL) <br/>{< br/> doublesyninstance = new doublesynsingleton (); <br/>}< br/> return doublesyninstance; <br/>}< br/>}
 

 

Note:
In practical applications, the C # and common language runtime libraries also provide a "static initialization" method, which does not require developers to show the need to compile thread-safe code.
It is not safe in a multi-threaded environment.
For example:
// The key word sealed step-by-step derivation to avoid multiple instances
Public sealed class Singleton
{
Private Static readonly Singleton instance = new Singleton (); // create an instance when any member of the class is referenced for the first time. The public language library processes variable initialization.

Private Singleton ()
{}

Public static Singleton getinstance ()
{
Return instance;
}

}
The preceding method is also called the lazy Singleton mode.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.