C # design mode (1) -- Singleton Mode

Source: Internet
Author: User

I. Introduction

Recently, some of the content in the design pattern is mainly referenced in the book Head First design pattern. In the course of study, I also read some articles about the design pattern in many blog parks, here I will record some of my study notes. One is to help me better understand the design patterns, and the other is to give some reference to some new design patterns. First, I will introduce a simple mode in the design mode-singleton mode (because only one class is involved here)

II. Introduction to singleton Mode

When talking about the singleton mode, the first response should be-what is the singleton mode ?, It literally means that a class has only one instance, therefore, the singleton mode ensures that only one instance of a class is implemented. (The design mode is actually a solution to the actual development process, this method is used to reduce the Coupling Degree between objects. However, there are many solutions. Therefore, our predecessors have summarized some common solutions as books, so that this book is called the design mode)The following is an official definition of the singleton mode:Make sure that a class has only one instance and provides a global access point.To help you better understand the singleton mode, you can use the following class diagrams to understand the singleton mode and analyze the implementation ideas of Singleton mode later:

)

Through the above analysis, we believe that it is easy for everyone to write the implementation code of the singleton mode. Let's look at the specific implementation code (you will be surprised after reading it: this is really the case !) :

                                                                       (uniqueInstance == = 

The implementation of the Singleton mode above is indeed perfect in a single thread, but multiple Singleton instances will be obtained in the case of multiple threads, because when the GetInstance method is run in both threads at the same time, at this time, when both threads determine (uniqueInstance = null) This condition returns true, then both threads will create Singleton instances, which violates the original intention of the Singleton mode, since the above implementation will run multiple threads for executionOur solution to multithreading is naturally to make the GetInstance method run only one thread at a time.That is, the thread synchronization problem (for thread synchronization, you can also refer to my thread synchronization Article). The specific code to solve multithreading is as follows:

                                 locker =                                                                   (uniqueInstance == = 

The above solution can solve the problem of multithreading,The above code locks the thread-assisted object locker in each thread and then checks whether the instance exists. This operation is completely unnecessary because after the first thread creates an instance of this class, at this time, the subsequent threads only need to directly judge (uniqueInstance = null) as false. At this time, there is no need to lock the auxiliary thread object before judging, therefore, the above implementation method adds additional overhead and reduces performance. To improve the defects of the above implementation method, we only need to add a sentence (uniqueInstance = null) before the lock statement) to avoid the additional overhead of the lock. This implementation method is called "double lock"To see the specific implementation code:

                                  locker =                                                   (uniqueInstance ==                      (uniqueInstance == = 
V. classes that implement the singleton mode in C #

After understanding the singleton mode, cainiao went on to ask: Is there any Singleton mode implementation in the. NET FrameWork class library?

After viewing ,. NET class library does exist in the singleton mode implementation class, but this class is not public. Let's take a look at a specific implementation of this class (this class exists in System. dll assembly, namespace is System, you can use the reflection tool Reflector to view the source code ):

                  (loader == = <SR>( loader, sr,            GetObject(= (loader ==  
Vi. Summary

Here, we have finished introducing the singleton mode of the design mode. I hope you can have a deeper understanding of Singleton mode in this article, I also hope that my friends who have never been familiar with the singleton mode or who are unfamiliar with the singleton mode will be amazed at the fact that this is the case!

 

Related Article

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.