C # single-sample mode-reprint

Source: Internet
Author: User

Address: http://www.cnblogs.com/cpetcoandy/archive/2011/11/10/2244407.html

The Singleton mode ensures that the specified class has only one instance at any time during the entire application lifecycle, and provides the client program with a global access point for the instance.

I. Classic Mode:

Publicclass Singleton {privatestatic Singleton instance;
Private Singleton (){
}
Publicstatic Singleton getinstance () {If (instance = NULL) {instance = new Singleton ();} return instance ;}}

Resolution:

1) First, the singleton constructor must be private to ensure that the client program will not generate an instance through the new () operation, so as to achieve the singleton goal;

2) because the lifecycle of static variables is the same as that of the entire application, you can define a Private Static global variable instance to save the unique instance of the class;

3) You must provide a global function to access the instance and provide the function to control the number of instances. That is, you can use the if statement to determine whether the instance has been instantiated, if not, you can create an instance with new (). Otherwise, an instance is directly returned to the customer.

In this classic mode, the problem of concurrent instance acquisition by threads is not taken into account. That is, when two threads obtain instance instances at the same time and the value is null, two threads create instances respectively, the Singleton rule is violated. Therefore, you need to modify the above Code.

Ii. Singleton mode with multiple threads

1. Lazy Mode

View code

 1         private static Singletion instance; 2         private Singletion() 3         { 4         } 5  6         public static Singletion GetInstance() 7         { 8             if (instance == null) 9             {10                 instance = new Singletion();11             }12             return instance;13         }

The above Code uses the dual lock method to better solve the implementation of the Singleton mode under multiple threads. First look at the if statement block in the inner layer. When this statement block is used, the lock operation is performed first to ensure that only one thread can access this statement block, and then only one instance is created. Look at the if statement block of the outer layer, so that each thread does not have to lock the instance every time it wants to obtain the instance, because the lock is required only when the instance is empty (that is, an instance needs to be created, if an instance already exists, it is returned directly, saving performance overhead.

2. Hunger Mode

This mode is characterized by active instances.

 

  

View code

1 // C # uses the hungry Chinese style... 2 Private Static readonly singletion instance = new singletion (); 3 private singletion () 4 {5} 6 // provide external site 7 public static singletion getinstance () 8 {9 return instance; 10}

 

The preceding readonly key can be used with static to specify that the constant is of class level. Its Initialization is implemented by the static constructor and can be compiled at runtime. In this mode, the CLR will solve the thread security issue for us. It can be seen that this class will be automatically instantiated when it is loaded, instead of generating a unique singleton object after the first call of getinstance.

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.