definition
Singleton mode is a kind of common software design pattern. In its core structure, there is only one special class that is called a singleton. The singleton mode ensures that there is only one instance of a class in the system.
Singleton mode is one of the simplest forms in design mode. The purpose of this pattern is to make an object of a Class A unique instance of the system. To achieve this, you can start by instantiating it from the client. Therefore, you need a mechanism that only allows you to generate a unique instance of an object class, "block" All access to the object that you want to generate. Use the factory method to restrict the instantiation process. This method should be a static method (class method), because it makes no sense to let an instance of the class generate another unique instance.
It is also divided into three different forms
A Hungry man type
//only the inner class can be static. Public classsingin{//defines an instance of itself within itself, for internal invocation only Private Static FinalSingin instance =NewSingin (); PrivateSingin () {}//This provides a static method for external access to this class, which can be accessed directly Public StaticSingin getinstance () {returninstance; }}
Lazy type
/*** * A common form*/ Private StaticSingin instance =NULL; Public Static synchronizedSingin getinstance () {//This method is better than the above, and does not have to be generated every time, just the first time//generate an instance when used, improve the efficiency! if(Instance = =NULL) Instance=NewSingin (); returninstance; }
Double lock type
//the synchronization content below the if inside, improve the efficiency of execution, do not have to synchronize every time to get the object, only the first synchronization, the creation of the future is not necessary. Private Static volatileSingin instance=NULL; PrivateSingin () {} Public StaticSingin getinstance () {if(instance==NULL){ synchronized(Singin.class){ if(instance==NULL) {instance=NewSingin (); } } } returninstance; }
Using the method, the getinstance () method is called directly to get the object.
Advantages
Instance control: Singln prevents other objects from instantiating copies of their own SINGLN objects, ensuring that all objects have access to unique instances
Flexibility: Because classes control the instantiation process, classes can be more flexible in modifying the instantiation process
Disadvantages
Overhead: Although the number is small, there is still some overhead required to check for an instance of the class each time the object requests a reference, which can be resolved by using static initialization.
Possible development confusion: When using SINGLN objects, especially those defined in a class library, developers must remember that they cannot instantiate objects using the New keyword. Because library source code may not be accessible, application developers may unexpectedly find themselves unable to instantiate this class directly.
Lifetime of object: Singln cannot resolve an issue that deletes a single object.
Single-instance mode for Android design mode