A detailed example of a single model

Source: Internet
Author: User

Recently learning to design patterns, learning to create patterns, encountered a singleton mode (or called a single-piece mode), now tidy up the notes.

Defined in "Design patterns:elements of resuable object-oriented Software": Ensure a class only have one Instance,and provide a G Lobal point of access to. Its main feature is not to generate a new instance based on the client program call, but to control the number of instances of a type-the only one. (Design mode-engineering implementation and expansion based on C #, Xiang Wang). In other words, the singleton pattern is guaranteed to have only one instance of the specified class at any given time throughout the life of the application, and provides a global access point for the client to get the instance.

First, the Classic mode:

public class Singleton
{
private static Singleton instance;

Private Singleton ()
{

}

public static Singleton getinstance ()
{
if (instance==null)
{
Instance=new Singleton ();
}
return instance;
}
}

The parsing is as follows:

1) First, the Singleton constructor must be private to ensure that the client program does not produce an instance through the new () operation to achieve the purpose of the singleton;

2) because the lifetime of a static variable is the same as the life cycle of the entire application, you can define a private static global variable instance to hold the unique instance of the class;

3) You must provide a global function access to the instance, and in that function provides the ability to control the number of instances, that is, through the IF statement to determine whether the instance has been instantiated, if not, you can create an instance with new (); otherwise, return an instance directly to the customer.

In this classic mode, the thread is not considered for concurrent acquisition of the instance problem, that is, there may be two threads to obtain the instance instance at the same time, and when it is null, there will be two threads created instance, violating the singleton rule. Therefore, you need to modify the above code.

Two, multi-threaded single case mode

1. Lazy mode

public class Singleton
{
private static Singleton instance;
Private static Object _lock=new object ();

Private Singleton ()
{

}

public static Singleton getinstance ()
{
if (instance==null)
{
Lock (_lock)
{
if (instance==null)
{
Instance=new Singleton ();
}
}
}
return instance;
}
}

The above code uses the double lock method to solve the single-mode implementation of multi-threading. First look at the inner if statement block, when using this block, the first lock operation to ensure that only one thread can access the statement block, and thus ensure that only one instance is created. Look at the outer if statement block, which allows each thread to acquire an instance without having to lock each time, because only if the instance is empty (that is, an instance needs to be created), the lock creation is required, and if an instance already exists, the instance is returned directly, saving performance overhead.

2. A Hungry man mode

This model is characterized by its own active instance.

public sealed class Singleton
{
private static readonly Singleton instance=new Singleton ();

Private Singleton ()
{
}

public static Singleton getinstance ()
{
return instance;
}
}
The ReadOnly key used above can be used with static to specify that the constant is of category level, its initialization is implemented by the static constructor, and can be compiled at run time. In this mode, there is no need to solve the thread safety problem yourself, the CLR will fix it for us. As a result of this class being loaded, the class is automatically instantiated without having to instantiate a unique singleton object after the first call to GetInstance (). Category: Design Patterns

A detailed example of a single model

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.