Note: Scenes and examples are from GitHub's design pattern. Portal:Https://github.com/iluwatar/java-design-patterns/tree/master/singleton
Intention:
Singleton mode even to ensure that a class has only one instance and provides a global access point.
Scene:
There is only one ivory tower in the world that allows wizards to learn magic, and all wizards come to this ivory tower to practise. Then the ivory tower can be understood here as a single case. Simply to create an object of a class, this ivory tower can be understood as a unique object.
Realize:
To better understand the singleton pattern, it's best to first look at the keyword static in java. Portal: http://www.cnblogs.com/ahangBlogs/p/7719330.html
Talk is cheap,show me the code ......................................................................................................................... ................................................................................................................ (Split line)
The implementation of several singleton patterns is listed as follows:
One:
Package Singleton; Public Final class ivorytower { private ivorytower () {} privatestatic Final ivorytower instance=new ivorytower (); Public Static ivorytower getinstance () { return Instance; }}
Two: threadsafelazyloaded
PackageSingleton; Public Final classThreadsafelazyloadedivorytower {Private StaticThreadsafelazyloadedivorytower Instance; PrivateThreadsafelazyloadedivorytower () {if(instance!=NULL){ Throw NewIllegalStateException ("already initialized."); } } Public Staticthreadsafelazyloadedivorytower getinstance () {if(instance==NULL) {Instance=NewThreadsafelazyloadedivorytower (); } returnInstance; }}
Three: Thread-safe double lock check
PackageSingleton; Public classthreadsafedoublechecklocking {Private Staticthreadsafedoublechecklocking instance; Privatethreadsafedoublechecklocking () {if(instance!=NULL){ Throw NewIllegalStateException ("Already instance!"); } } Public Staticthreadsafedoublechecklocking getinstance () {//Use local variables to improve performance by 25%. Derived from Effectice Java Th2. In simple terms, local variables are saved on the stack ....Threadsafedoublechecklocking result=instance; //check whether the power of the singleton mode is initialized, and if it is initialized, return to the instance directly, without initialization, go down. if(result==NULL{//The instance is not initialized, but we cannot ensure that the other thread initializes the instance during this time period, so to make sure we have to lock an object to repel each other. synchronized(threadsafedoublechecklocking.class{//will be the instance assigned to the local variable again, this time the current thread cannot enter the lock space, if we have initialized our return instance result=instance; if(result==NULL{//Enter the IF, that is, there is no initialization in the other thread. Then we can safely create an instance as our singleton example. Instance=result=Newthreadsafedoublechecklocking (); } } } returnresult; } }
Four:
Package Singleton; Public enum enumivorytower { INSTANCE; @Override public String toString () { return getdeclaringclass (). Getcanonicalname () + "@" + hashcode ();} }
Applicability:
Using singleton mode
- You must have only one instance of the class, and you must be able to access the client from a well-known access point
- When the only instance should be extended by subclasses, the client should be able to use the extended instance without modifying their generation
Disadvantages:
- Violate the single responsibility principle (SRP) by controlling your own creation and life cycle.
- The use of global shared instances is encouraged to prevent objects and resources used by the object from being freed.
- create tightly coupled code. Singleton customers become difficult to test.
Single-instance mode/Java implementation with code/