[Design mode] 1. Singleton single piece (Creation Mode)

Source: Internet
Author: User

I recently studied the design model and watched the video tutorial "C # Object Oriented Design Model overview" by Li Jianzhong (Shanghai Zhucheng technology advanced training lecturer, this tutorial is one of the webcast series tutorials provided by Microsoft msdn and can be downloaded from verycd.

Download link: http://www.verycd.com/topics/148585/

 

After each lectureCodeMake comments and share them out. If any errors are inappropriate, please criticize and correct them.

 

 

 

1st implementation methods

  Namespace  Singleton
{
/// <Summary>
/// 1st implementation methods
/// </Summary>
Public Class Singleton1
{
/// <Summary>
/// Private constructor, which restricts external construction of instances
/// </Summary>
Private Singleton1 ()
{
}

/// <Summary>
/// The compiler will fine-tune the code order when compiling the code. You can use volatile
/// Ensure that the compiler will not fine-tune the following
/// </Summary>
Private Static Volatile Singleton1 _ instance;

/// <Summary>
/// Helper, not involved in real construction
/// </Summary>
Private Static Object _ lockhelper = New Object ();

/// <Summary>
/// In this case, attributes are used and parameter passing is not supported.
/// You can change attributes to methods to support parameter passing.
/// </Summary>
Public Static Singleton1 instance
{
Get
{
// A single thread does not need to use double check
If (_ Instance = Null )
{
// Use lock to avoid Multithreading
Lock (_ Lockhelper)
{
If (_ Instance = Null )
{
_ Instance = New Singleton1 ();
}
// Else
// {
// If the singleton needs to implement parameter transfer and pass the parameter value to its own attribute,
// Change this attribute to a method, and assign a value to the else statement.
// The scaling mode is not fixed and can be flexibly processed.
// }
}

}
Return _ Instance;
}
}
}
}

 

 

 

2nd implementation methods

  Namespace  Singleton
{
/// <Summary>
/// 2nd implementation methods
/// This method does not support parameter passing, but can be extended.
/// </Summary>
Public Class Singleton2
{
Private Singleton2 ()
{
}

Public Static Readonly Singleton2 _ instance = New Singleton2 ();
}

}

 

 

 

 

3rd implementation methods

  Namespace  Singleton
{
/// <Summary>
/// 3rd implementation methods, similar to the second method
/// </Summary>
Public Class Singleton3
{
Private Singleton3 ()
{
}

/// <Summary>
/// 1. The static constructor neither has an access modifier nor a parameter.
/// 2. When the first class instance is created or any static member is referenced,. Net automatically calls the static constructor to initialize the class.
/// -- In other words, we cannot directly call static constructors, nor can we know when static constructors will be called.
/// 3. A class can have only one static constructor.
/// 4. A non-parameter constructor can coexist with a static constructor.
/// -- Although the parameter list is the same, one belongs to the class and the other belongs to the instance, so there is no conflict.
/// 5. Run only once at most.
/// 6. Static constructors cannot be inherited.
/// 7. If no static constructor is written and the class contains static members with an initial value, the compiler automatically generates the default static constructor.
/// </Summary>
Static Singleton3 ()
{
_ Instance = New Singleton3 ();
}

/// <Summary>
/// The static field must be valid only after the static constructor of the current class is initialized.
/// For example, if this field is used externally, the compiler will first execute the static constructor.
/// </Summary>
Public Static Readonly Singleton3 _ instance;
}
}

 

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.