Single-instance pattern learning for Design patterns (C #)

Source: Internet
Author: User

Recently in the change of work, found that the interview will ask the design mode of the singleton mode, so go home to do a summary of the singleton model, if there is a problem, please comment.

An introduction to the singleton pattern

Singleton mode: is to ensure that there is only one instance of the system, called a singleton mode (singleton pattern);

The singleton model is divided into: a hungry man type and lazy type;

The rules that a singleton pattern must follow:

1. This class must have only one instance in the whole system;

2. This unique instance must be created by the class itself;

3. This instance must be made available to the entire system on its own;

The single-instance pattern implements the rules that must be followed:

1. Define a static variable to save the instance;

2. Define a private constructor that prevents the outside world from creating the instance itself;

3. Define a static public method that provides a global access point;

4. When considering thread safety, it is necessary to define an identity to ensure thread synchronization mechanism;

Typical application code for a singleton pattern:

A Hungry man type

A hungry man, do not consider the problem occurs when the user assigns null to the instance. Because every time we go in, we instantiate it;

   Public class   Singleton {   privatestatic Sinleton mysingleton=new  Sinleton ();    private  sinleton{}   publicstatic  Sinleton getintance () {  return  Mysinleton;}}
View Code

Lazy type

 Public class singleton{  privatestatic  Singleton Mysingleton;   Private singleton{}    Public Static Singleton getintance () {   if(mysingleton==null) {      Return mysingleton=new  Singleton ();} return Mysingleton;}}
View Code

Multi-threaded case in single case mode

For lazy-type, multithreading occurs under this situation: for example, two thread A and b,a run to if (Mysingleton==null), B runs to Mysingleton=new Singleton (); b Although the Mysingleton is instantiated, but for a or not instantiated, a will continue to execute mysingleton=new Singleton (); This time there is a problem, there are two instances of the problem; So the code is adapted

 Public classsingletonexample{Private Staticsingletonexample Mysingleton;Private Static ReadOnly ObjectLocker=New Object();Privatesingletonexample{}//the method of public access can overload multiple parameters; Public Staticsingletonexample getinstance () {if(mysingleton==NULL){Lock(Locker) {Mysingleton=Newsingletonexample ();}}returnMysingleton;}}
View Code

At this point, a, B runs to lock (locker) at the same time, only one thread can access it, so B. executes first, b obtains the lock, and after B executes, unlocks. A go, because there is no judgment whether mysingleton==null, so it will be instantiated, there are two instances of the case;

The double-check mode (DCL) is then present. The code is as follows:

 Public classsingletonexample{
Here is the lazy type; Not instantiated, more commonly used.
A hungry man type: private static singletonexample Mysingleton =new singletonexample (); is instantiatedPrivate Staticsingletonexample Mysingleton;Private Static ReadOnly ObjectLocker=New Object();Privatesingletonexample () {}
The method of public access can overload multiple parameters; Public Staticsingletonexample getinstance () {if(mysingleton==NULL){Lock(locker) {if(mysingleton==NULL) {Mysingleton=Newsingletonexample (); }}}returnMysingleton;}}

Attention:

1. Since this is actually a class that implements the singleton pattern, in addition to following the rules of the singleton, the other fields and properties can exist as normal classes;

On the basis of the singleton model learning is the above, there are problems, please leave a message to discuss with each other; Thx

Single-instance pattern learning for Design patterns (C #)

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.