Overview:
Singleton: ensures that a class has only one instance and provides a global access point to it. Generally, we can make a global variable to make an object accessible, but it cannot prevent you from instantiating multiple objects. The best way is to let the class itself save its unique instance. This class can ensure that no other instance can be created, and it can provide a method to access and change the instance.
Applicable scenarios:
1. When the class can only have one instance and the customer can access it from a well-known access point.
2. When this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code.
Class diagram:
Sample Code:
Class Singleton
{
Private Static Singleton instance;
// The constructor privatize it so that the outside world cannot be instantiated through new.
Private Singleton ()
{}
Public static Singleton getinstance ()
{
If (null = instance)
{
Instance = new Singleton ();
}
Return instance;
}
}
Frontend call Test
Static void main (string [] ARGs)
{
Singleton S1 = singleton. getinstance ();
Singleton S2 = singleton. getinstance ();
If (S1 = S2)
{
Console. Write ("same instance ");
}
Console. Read ();
}
Multi-threaded singleton: if multiple threads call the getinstance () method at the same time, multiple instances may be created. You can use the lock statement to ensure that when a thread is located in the critical section of the Code, the other thread does not enter the critical section. If other threads attempt to enter the locked code, the code will be blocked until the object resource is released.
Class singletonthreads
{
Private Static singletonthreads instance;
// Create a process helper object for the static system when the program runs
Private Static readonly object syncroot = new object ();
Private singletonthreads (){}
/// <Summary>
/// Double lock judgment
/// </Summary>
/// <Returns> </returns>
Public static singletonthreads getinstance ()
{
// If you do not determine whether the instance is instantiated, you must execute the shackles each time. After the judgment, the shackles are only executed when the instance is not instantiated, improving the performance.
If (instance = NULL)
{
// Only one thread can enter the part of the program that is locked at the same time.
Lock (syncroot)
{
If (instance = NULL)
{
Instance = new singletonthreads ();
}
}
}
Return instance;
}
}
This static Initialization Method of C # solves the thread security problem under multiple threads, so that it does not need to be solved through the double yoke mechanism.
Thread security is a problem, but the class is instantiated upon loading, occupying system resources in advance, through the yoke mechanism, you do not need to advance
Use System Resources and select the singleton mode based on actual conditions.
/// <Summary>
/// Sealed the class to block derivation and prevent adding instances through Derivation
/// </Summary>
Public sealed class singletonsealed
{
// Readonly indicates static initialization. When this class is called for the first time, CLR processes variable initialization.
Private Static readonly singletonsealed instance = new singletonsealed ();
Private singletonsealed (){}
Public static singletonsealed getinstance ()
{
Return instance;
}
}
Summary:
The Singleton mode is widely used, for example, database instantiation and file read/write.