From the Singleton design mode of C #

Source: Internet
Author: User

Recently, I have read some materials about how to use design patterns in the C # language. The Singleton design patterns have attracted my attention.

All developers who have learned the design mode know the Singleton mode. I would like to briefly explain that this design pattern is intended for those who have not yet learned the design pattern. The Singleton design pattern shows you how to create a global object for a unique class example in your application. That is to say, this object can only be instantiated once, this object also provides a global access point to access it. Such objects, such as the window manager in the application, printing off-line, database connection pool, and so on.

Now let's take a look at the C ++ implementation of the Singleton mode in the design patterns book (for convenience of comparison, I write the implemented code in an inline way ):

 

Class Singleton

{

Public:

Static Singleton * Instance ()

{

If (_ instance = 0)

{

_ Instance = new Singleton;

}

Return _ instance;

}

Protected:

Singleton ();

Private:

Static Singleton * _ instance;

};

 

Stington * Singleton: _ instance = 0;

 

I don't want to explain the above Code here. You can refer to the references provided later in this article.

To better understand C #, I will also present the Singleton mode implementation code in Java (isn't C # considered as a Java plagiarism? J ):

 

Class Singleton

{

Public Singleton Instance ()

{

If (_ instace = null)

{

Synchronized (Class. forName ("Singleton "))

{

If (_ instance = null)

{

_ Instance = new Singleton ();

}

}

}

Return _ instance;

}

Protected Singleton (){}

Private static Singleton _ instance = null;

}

 

The preceding Java code uses a dual detection mechanism to avoid Singleton instantiation of multiple threads. Similarly, I do not want to explain the above Code.

 

First, let's take a look at the implementation similar to Java in C:

 

Class Singleton

{

Public static Singleton Instance ()

{

If (_ instance = null)

{

Lock (typeof (Singleton ))

{

If (_ instace = null)

{

_ Instance = new Singleton ();

}

}

}

Return _ instance;

}

Protected Singleton (){}

Private static volatile Singleton _ instance = null;

}

 

Now, it's time for us to open our eyes. The following code uses the advantages of the. NET Framework Platform to implement the Singleton mode:

 

Sealed class Singleton

{

Private Singleton ();

Public static readonly Singleton Instance = new Singleton ();

}

 

Not only is the code much reduced, but it also solves the performance loss caused by the thread problem. This avoids Compiler optimization. Can the above Code work properly? If so, how does it work?

Note that the Singleton class is declared as sealed to ensure that it will not be inherited by itself. Second, without the Instance method, the original _ instance member variable is changed to public readonly, it is initialized during declaration. Through these changes, we do get the Singleton mode, because during JIT processing, if the static attribute in the class is used by any method ,. NET Framework will initialize this property, so the Singleton class Instance can be created and loaded while initializing the Instance property. The private constructor and readonly ensure that Singleton will not be instantiated again, which is exactly the intent of Singleton's design pattern.

This article does not discuss design patterns, but just wants to use this example to illustrate the importance of mastering the. NET Framework and using it flexibly for practical application development.

 

References:

-- Design Mode: the basis for reusable object-oriented software [GOF]

-- Design Pattern in C # [Jame W Cooper]

-- Copying ing the Singleton Design Pattern

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.