C # Using Generics to implement single-piece instances

Source: Internet
Author: User
C # Using Generics to implement single-piece instances
[13:15:00 | by: Chen Qingyang]
 

2 recommended http://blog.csai.cn/user1/16236/archives/2007/17475.html

C # Using Generics to implement single-piece instances

In multi-threaded applications, implementing the single-piece object (gof Singleton mode) is a common design method. For example, for multi-threaded server applications that need to maintain the same resource, it is a common design method to implement the single-piece mode for objects expressing the resource. In C #, the common practice is: public class myclass {
Private Static volatile myclass instance __;
Private Static object syncroot _ = new object (); Private singletongeneric ()
{
} Public static myclass instance
{
Get
{
If (instance _ = NULL)
{
Lock (syncroot __)
{
If (instance _ = NULL)
{
Instance _ = new myclass ();
}
}
}
Return instance __;
}
} This is a standard method in C # to implement the single-piece mode. Imagine if we have multiple classes that need to apply the single-piece mode, do we need to write the above Code repeatedly? In fact, we can use the generic type in C #2.0 to define a single piece, and make the "instance single-piece": public class singletongeneric <t> where T
: New ()
{
Private Static t instance _ = default (t );
Private Static object syncroot _ = new object (); Private singletongeneric ()
{
} Public static t instance
{
Get
{
If (instance _ = NULL)
{
Lock (syncroot __)
{
If (instance _ = NULL)
{
Instance _ = new T ();
}
}
}
Return instance __;
}
}
} In this case, we only need "singletongeneric <myclass>. There are three advantages to doing so: 1. When defining a single-piece category, we can define the class in the normal way, without considering the specific implementation of the single-piece pattern, apply singletongeneric <myclass> directly wherever it is actually needed. 2. When the single-piece mode is changed, we only need to modify the singletongeneric <t> class, without modifying all the single-piece classes; 3. This method also makes the system not a single-piece class of single-piece type single-piece, for example, my server application uses a hashtable object to save the data dictionary. Then, I only need to use the singletongeneric Pattern-Oriented Software Architecture (Volume 2: Pattern for concurrent and networked objects)This is a conventional pattern mentioned in the book. When using this method in C #, there is a potential risk that we cannot use the volatile keyword on the generic T. This keyword ensures that the specified type object is not optimized when being processed by the compiler, so that the value stored by this object is up-to-date at any time. At present, I have no way to prove whether the volatile keyword is not used when a single piece is used in C #, and whether there are other alternatives to solve the problem.

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.