Singleton mode (singletion): guarantees that a class has only one instance and provides a global access point to access the instance.
The primary function of the singleton mode is to guarantee a unique instance, which can strictly control how the client accesses the instance and when to access it. Can be simply understood as controlled access to a unique instance.
Singleton |
-instance:singleton |
-singleton () +getinstance () |
The Singleton class, as a class for which we want to do a singleton operation, defines a static getinstance action function in the class that allows the client to access its unique instance, primarily to create its own unique instance.
public class Singleton { private static Singleton Instance; private Singleton () {} public static Singleton getinstance () { if (Instance = = null = new Si Ngleton (); return Instance; }}
Client Calls
Singleton Singleton = Singleton.getinstance ();
The above is an example of a simple singleton pattern that can be done well in a single-threaded program. And when our program is multi-threaded, problems can occur.
In multithreaded programs, multiple instances can occur when multiple threads access getinstance () at the same time. It is therefore necessary to further refine our singleton classes in multithreaded programs:
Public classSingleton {Private StaticSingleton Instance; Private Static ReadOnly ObjectSyncRoot =New Object(); PrivateSingleton () {} Public StaticSingleton getinstance () {if(Instance = =NULL)----------First, determine whether there is an instance, if there is a direct return, to avoid useless lock effect performance. { Lock(syncRoot) {if(Instance = =NULL)----------double Lock {Instance=NewSingleton (); } } } returnInstance; } }
In addition, C # and the common language runtime also provide a static initialization method that does not require us to explicitly write thread-safe code, which can solve the problem of unsafe in multithreaded programs.
Public Sealed class Singleton---- enclosing class, cannot be integrated { privatestaticreadonlynew Singleton ();-----ReadOnly indicates that the instance can only be allocated in initialization. Private Singleton () {} Public Static Singleton getinstance () { return instance; } }
This static initialization method instantiates a class only when the class is loaded, so it is called a a hungry man singleton pattern. Both of the previous formulations were instantiated as a lazy singleton pattern when the class was first referenced.
A hungry man singleton mode initializes the class when the program loads the class, and we may not use it immediately, so it takes up the resources of the system in advance. However, the lazy singleton mode needs to consider the problem of multi-thread insecurity, and the instantiation of class
The process of double locking to ensure the security of the instantiation. There are two ways to benefit, depending on the specific scenario needs of the application that we are developing to choose the best singleton model.
Design mode: Singleton mode (singletion)