Single case Mode--c#

Source: Internet
Author: User

Just look at the design pattern, still do not know what the role of these design patterns, but first recorded, perhaps in the future will be used!

Singleton mode: is to ensure that a class has only one instance of the implementation method

For example, there is a constructor that makes it impossible for outsiders to create instances of the class

Private class name for this class ()
{
Console.Write ("This is a singleton mode test \ n");
}

Set private to ensure that it cannot be instantiated externally,

Create a static variable in the current class to hold an instance of the class

private static class name of this class example;

Then define an identity, which is used to lock the

private static readonly Object locker = new Object ();

These variables and identities are set to private and are guaranteed not to be called externally

After that, a public method is created to instantiate the class, and an instance of this class is externally invoked by invoking this public method.

public static class name for this class getinstance ()

1. Determine if this class is instantiated

if (example==null)

2. Locking locker, is the method that the locker is not executed during the lock

Lock (Locker)

3. Re-determine if the class is instantiated

if (example==null)

4. Instantiate this class

example= new Singleton ();

5. Finally output an instance of this class

return example;

Get the full code

public class Singleton
{
Define a static variable to hold an instance of the class
private static Singleton example;

Define an identity to ensure thread synchronization
private static readonly Object locker = new Object ();

Defines a private constructor so that an instance of the class cannot be created by the outside world
Private Singleton ()
{
Console.Write ("This is a singleton mode test \ n");
}

<summary>
Defining public methods provides a global access point, and you can also define public properties to provide global access points
</summary>
<returns></returns>
public static Singleton getinstance ()
{

Multi-threaded running at the same time here, will be in the same condition to execute the code in the condition
if (example== null)//Limit One
{

After multiple threads are running here, only one thread can be locked by lock and the other thread will be suspended
Lock (Locker)//Limit Two
{
Again, if an instance of the class is created, instantiated if it does not exist, and vice versa, the instance of the class is output directly
if (example== null) limit three
{
example= new Singleton ();
}
}
}
return example;
}
}

If there is more than one thread running to limit one at the same time, it will pass the restriction one, so that there will be multiple threads running to limit two at the same time, these threads run to the limit of two, will be a small "sort", "sort" the first thread through the restriction of two, the limit of two will be self-closing until The first thread runs out of the limit of two limits, limiting two to continue allowing other "sort" threads to pass. This way, to limit three, there is only one "sort" of the first thread, the sort of the first thread to pass the limit three, instantiate the class, run out of limit three, limit two. Here, just waiting for the other threads will gradually pass the limit of two, but they are already late through the restriction of two, because this class has been instantiated, so they can not limit three, and the direct output is "sort" the first thread instantiation of the class instance,

Single case Mode--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.