Singleton is a creation pattern that refers to a class that uses singleton mode, after which the class is created, only one instance can be generated for external access, and a global access point is provided.
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.
One: a hungry man mode
The advantages are: it is relatively simple to write, and there is no multithreading synchronization problem, which avoids the performance problem caused by synchronized;
The disadvantage is that when the class singletontest is loaded, the static instance is initialized, the statically variable is created and the memory space is allocated, and since then, This static instance object always occupies this memory (even if you haven't used it yet), and when the class is unloaded, the static variable is destroyed and the memory is freed, so it consumes memory under certain conditions .
PackageCn.design.singleton;/*** A hungry man mode * Advantages are: It is relatively simple to write, and there is no multithreading synchronization problem, to avoid the performance problems caused by synchronized; * The disadvantage is that when the class singletontest is loaded, the static instance is initialized, Static variables are created and allocated memory space * Since then, this static instance object has occupied this memory (even if you have not yet used this instance), and when the class is unloaded, the static variable is destroyed and the occupied memory is freed, so it consumes memory under certain conditions. * @authorLing ye June **/ Public classSingletontest {//privatize the construction method so that external access is not available//instantiated objects can only be obtained from the defined getinstance method//prevent external instantiation of objects through new singletontest () Privatesingletontest () {}/*** Instance external can not be accessed directly, loaded with the class load * Static variables in memory only one copy (memory saving), the JVM only statically allocated memory once, in the process of loading the class to complete the memory allocation of static variables * Final member Variable table Can only be assigned once, and the value will no longer change after the value is assigned. */ Private Static FinalSingletontest instance=Newsingletontest (); //static methods, which do not change depending on the object//returns the instance object defined above Public Staticsingletontest getinstance () {returninstance; } Public Static voidMain (string[] args) {//can be accessed under the current class, but other classes cannot access private variablesSingletontest st=Newsingletontest (); //in other classes, this getsSingletontest st1=singletontest.getinstance (); }}
Two: Full-han mode
The advantage is: it is relatively simple to write, when the class singletontest is loaded, static variable instance is not created and allocates memory space, when the getinstance method is first called, the instance variable is initialized, and the memory is allocated , so the memory is saved under certain conditions;
The disadvantage is that multiple singletontest instances are likely to occur in a concurrent environment .
PackageCn.design.singleton; Public classSingletonTest1 {//privatization of construction methods to prevent external acquisition of objects through New SingleTest1 () PrivateSingletonTest1 () {}//Full-han mode is full, don't worry. When initializing an object, get a unique instance//without the final keyword, if you add it, it will be assigned immediately.//and the full-Han mode requires dynamic invocation when creating a unique instance Private StaticSingletonTest1 instance; //defines a static method to wait for a call when the object is initialized (there is no guarantee that there is only one object in multi-threading) Public StaticSingletonTest1 getinstance () {if(instance==NULL) {instance=NewSingletonTest1 (); } returninstance; } Public Static voidMain (string[] args) {SingletonTest1 St=singletontest1.getinstance (); System.out.println (ST); }}
Three: The optimization of the mode of filling and Han
The advantage is that multiple singletontest instances occur when using the Synchronized keyword to avoid multi-threaded access.
The disadvantage is that when synchronous methods are frequently called, the efficiency is slightly lower.
PackageCn.design.singleton;/*** Optimize the full-han mode * Advantages: Locking prevents multiple instances of multi-threaded access problems * Disadvantage: The synchronous method frequently calls, the efficiency is slightly lower. * @authorLing ye June **/ Public classSingletonTest2 {PrivateSingletonTest2 () {}Private StaticSingletonTest2 instance; //defines a static method that is initialized when invoked//Add a lock synchronized to prevent multiple instances of multi-threaded concurrent calls from occurring Public synchronized StaticSingletonTest2 getinstance () {if(instance==NULL) {instance=NewSingletonTest2 (); } returninstance; }}
Four: Optimal scheme (regardless of reflection)
Method Four is the best implementation of singleton pattern. Memory footprint, high efficiency, thread safety, multi-threaded operation atomicity.
PackageCn.design.singleton; Public classSingletonTest3 {PrivateSingletonTest3 () {}//using volatile guarantees the visibility of the instance variable during multi-threaded access,//avoids instance initialization when other variable attributes are not yet assigned, are called by another thread//The use of volatile guarantees the visibility of the instance variable during multi-threaded access, avoiding the call of another thread when the other variable property is not assigned to instance initialization Private Static volatileSINGLETONTEST3 instance; Public StaticSingletonTest3 getinstance () {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(SingletonTest3.class) {System.out.println (SingletonTest3.class); Instance=NewSingletonTest3 (); } } returninstance; } Public Static voidMain (string[] args) {SINGLETONTEST3 St=singletontest3.getinstance (); }}
Java design pattern-Singleton mode