features
-
A singleton class can have only one instance.
-
The Singleton class must create its own unique instance.
-
The Singleton class must provide this instance to all other objects.
Singleton mode ensures that a class has only one instance, and instantiates itself and provides this instance to the entire system. In a computer system, the driver objects of the thread pool, cache, log Object, dialog box, printer, and video card are often designed as singleton. These apps have more or less the functionality of the resource manager. Each computer can have several printers, but only one printer Spooler to prevent both print jobs from being output to the printer at the same time. Each computer can have several communication ports, and the system should centrally manage these communication ports to prevent a communication port from being called simultaneously by two requests. In short, the selection of Singleton mode is to avoid inconsistent state, to avoid long-running political.
Single Case design mode
Singleton is a pattern of creation in which a class takes Singleton mode, and when the class is created, it is possible to produce only one instance for external access and a global access point.
The core points of knowledge are as follows:
(1) The construction method of a class with a singleton design pattern is privatized (with private modification).
(2) The instantiation object of the class is generated within it and encapsulated as a private static type.
(3) Defines a static method that returns an instance of the class.
Method One:
1 /** 2 * 3 * Single-instance mode implementation: A hungry man , thread-safe but inefficient4 */ 5 Public classSingletontest {6 7 //to define a private construction method8 Privatesingletontest () {9 } Ten One //set the instance object of itself to a property, plus the static and final modifiers A Private StaticFinal Singletontest instance =Newsingletontest (); - - //static methods return instances of this class the Public Staticsingletontest getinstancei () { - returninstance; - } - +}
Advantagesis: It is relatively simple to write, and there is no multithreading synchronization problem, to avoid the performance problems caused by synchronized;
Disadvantagesis: When the class singletontest is loaded, the static instance is initialized, the statically variable is created and the memory space is allocated.
Since then, this static instance object has been occupying this memory (even if you haven't used it yet), and when the class is unloaded, the static variable is destroyed and the occupied memory is freed, so the memory is consumed under certain conditions.
Method Two:
1 /** 2 * Method Two3 * Implementation of Singleton mode: Full-Chinese, non-thread safe4 * 5 */ 6 Public classSingletontest {7 8 //Define private construction methods (prevent instantiation with new Singletontest ())9 Privatesingletontest () {Ten } One A //define a variable of type singletontest (uninitialized, note that the final keyword is not used here) - Private StaticSingletontest instance; - the //defines a static method (Singletontest is initialized when called, but can cause repeated initialization problems when multithreaded access) - Public Staticsingletontest getinstance () { - if(Instance = =NULL) -Instance =Newsingletontest (); + returninstance; - } +}
Advantagesis: It is relatively simple to write, when the class singletontest is loaded, static variable instance is not created and allocated memory space, when the GetInstance method is first called,
Initializes the instance variable and allocates memory so that it saves memory under certain conditions;
DisadvantagesYes: Multiple singletontest instances are likely to occur in a concurrent environment.
method Three: ( simple optimization of Method two )
1 /** 2 * Method Three3 * Single-mode implementation: Full-Chinese, thread-safe simple implementation4 * 5 */ 6 Public classSingletontest {7 8 //Define private construction methods (prevent instantiation with new Singletontest ())9 Privatesingletontest () {Ten } One A //define a variable of type singletontest (uninitialized, note that the final keyword is not used here) - Private StaticSingletontest instance; - the //define a static method (re-initialize the singletontest when called and use synchronized to avoid multi-threaded access, which can cause heavy re-initialization problems) - Public Static synchronizedsingletontest getinstance () { - if(Instance = =NULL) -Instance =Newsingletontest (); + returninstance; - } +}
Advantagesis: Multiple singletontest instances occur when using the Synchronized keyword to avoid multithreaded access.
DisadvantagesIs that when the synchronous method is called frequently, the efficiency is slightly lower.
Method Four: (The best implementation of a singleton pattern.) Memory footprint, high efficiency, thread safety, multi-threaded operation atomicity)
Method Four is the best implementation of singleton pattern. Memory footprint, high efficiency, thread safety, multi-threaded operation atomicity.
1 /** 2 * Method Four3 * Optimal scheme of single case mode4 * thread safety and high efficiency5 * 6 */ 7 Public classSingletontest {8 9 //Define a private construction methodTen Privatesingletontest () { One A } - //defines a static private variable (uninitialized, does not use the final keyword, and volatile guarantees the visibility of the instance variable when multi-threaded access is avoided, and is called by another thread when the other variable property is not assigned when instance initialized) - Private Static volatileSingletontest instance; the - //defines a common static method that returns an instance of the type - Public Staticsingletontest getistance () { - //when an object is instantiated (without using a synchronous block of code, instance is not equal to NULL, the object is returned directly, improving operational efficiency) + if(Instance = =NULL) { - //Synchronizing code blocks (when an object is not initialized, use a synchronous block of code to guarantee that the object will not be created again after the first creation of the multi-threaded access) + synchronized(Singletontest.class) { A //Uninitialized , the initial instance variable at if(Instance = =NULL) { -Instance =Newsingletontest (); - } - } - } - returninstance; in } -}
In fact, the constructor of the private type can be instantiated through the Java reflection mechanism, which essentially invalidates all Java singleton implementations.
Java design Pattern--Singleton mode