Application scenarios:
In software systems, there are often such special classes that must ensure that they only have one instance in the system to ensure their logic correctness and good efficiency.
This isClass DesignerIs not the user's responsibility.
Ensure that a class has only one instance and provides a global access point for the instance. -- Design Pattern gof
CodeThe implementation is as follows:
// Singleton example of a single thread: </P> <p> public class Singleton <br/>{< br/> Private Static Singleton instance; </P> <p> Public static Singleton instance <br/> {<br/> Get <br/> {<br/> If (instance = NULL) <br/>{< br/> instance = new Singleton (); <br/>}< br/> return instance; <br/>}</P> <p> // display the constructor of the call class and set it to private, the purpose is to block calls to the default public constructor. <br/> // It also prevents external calls from initializing the class instance. <br/> private Singleton () {}< br/>}</P> <p> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> // no matter how many Singleton objects are referenced, all references the same instance <br/> // Of course, you can also define a static method in the class to obtain the instance using the method <br/> Singleton S1 = Singleton. instance; </P> <p> Singleton S2 = Singleton. instance; </P> <p> Singleton S3 = Singleton. instance; </P> <p >}< br/>}
Explanation:
1. The instance constructor in singleton mode can be set to protected to allow subclass derivation.
2. The singletom mode only takes into account the management of object creation,Management of object destruction is not taken into account. In terms of the overhead of platforms and objects that support garbage collection, we generally do not need to perform special management on their destruction.
3. Unable to cope with multi-threaded environments: In a multi-threaded environment, multiple instance objects of the singleton class may still be obtained using the singleton mode.
The above code is only applicable to a single threadProgramWhen running in a multi-threaded environment, multiple objects can still be created. The specific possibility is:
**********************************
If (instance = NULL)
{
Instance = new Singleton ();
**********************************
,
In a multi-threaded environment:
When running to "if" and left curly braces, if there are two threads, one thread has determined that the result of "instance = NULL" is true, prepare to execute the statement "instance = new Singleton ();", but it has not been executed yet. Then, another thread judges the statement "instance = NULL, because the previous thread has not yet constructed a class instance, the result is still true, so it will go through this branch and then build an instance, as a result, the number of instances cannot be reached;
Solution: Use the Mechanism. The Code is as follows:
// Example of multi-threaded singleton: </P> <p> public class Singleton <br/>{< br/> Private Static Singleton instance; <br/> // secondary object <br/> Public static object lockhelper = new object (); </P> <p> Public static Singleton instance <br/> {<br/> Get <br/> {<br/> If (instance = NULL) <br/> {<br/> // lock. The following code runs in a single-threaded environment. <br/> lock (lockhelper) <br/> {<br/> // judge again <br/> If (instance = NULL) <br/> {<br/> instance = new Singleton (); <br/>}</P> <p >}< br/> return instance; <br/>}</P> <p> // explicitly call the constructor of the class and set it to private, the purpose is to block calls to the default public constructor. <br/> // It also prevents external calls from initializing the class instance. <br/> private Singleton () {}< br/>}</P> <p> class Program <br/>{< br/> static void main (string [] ARGs) <br/>{< br/> // no matter how many Singleton objects are referenced, all references the same instance <br/> // Of course, you can also define a static method in the class to obtain the instance using the method <br/> Singleton S1 = Singleton. instance; </P> <p> Singleton S2 = Singleton. instance; </P> <p> Singleton S3 = Singleton. instance; </P> <p >}< br/>}
Another simple method:
// Example of multi-threaded singleton: </P> <p> public class Singleton <br/>{< br/> Public static readonly Singleton instance = new Singleton (); </P> <p> // when the last line of code is executed, it is equivalent to executing the following annotated code segment, because, <br/> // The static constructor defined by line 3 of code must be executed before the code of Line 3 is executed, <br/> // The execution mechanism of the static constructor must be a single-threaded environment <br/> // public static readonly Singleton instance; </P> <p> // static Singleton () <br/> // {<br/> // instance = new Singleton (); <br/> //} </P> <p> private Singleton () {}< br/>}< br/>
Of course, one drawback of the above Code is: lack of mobility, that isThe static constructor cannot pass parameters.That is to say, the constructor cannot contain parameters. At this time, it is necessary to regularly initialize some methods or attributes of parameters;
Singleton mode extension:
1. Extend an instance to N instances, such as the implementation of the Object pool.
2. Transfer the call of the new constructor to other classes. For example, in a collaborative working environment of multiple classes, only one instance of a certain class is required for a local environment.
3. The core of understanding and scaling the singleton model isHow to control the arbitrary call of a new class instance Constructor".