--------2015-11-20 14:45:10
Java Design Pattern Single-instance mode (Singleton):
---application: Only one object is needed, such as a dynasty and an emperor.
---effect: Ensure that an instance of an application has only one. Use one together to save resources.
---Type: A hungry man mode, lazy mode.
One. A hungry man mode (class loading will be instantiated, thread-safe)
1 PackageCom.sun.singleton;2 3 //a hungry man mode4 Public classSingleton {5 6 //privatization of constructors does not allow external direct calls to create objects7 PrivateSingleton () {8 9 }Ten //creates a unique instance of the class internally, decorated with private static (instantiated once when the class is loaded, after which the instance is invoked several times) One Private StaticSingleton instance =NewSingleton (); A - //provides an external method for obtaining an instance, with public static decoration - Public StaticSingleton getinstance () { the returninstance; - } - -}
Two. Lazy mode (lazy, so only call method getinstance time based on the results of the decision will be instantiated, and thread is not secure)
PackageCom.sun.singleton;//lazy Mode (thread insecure) Public classSingleton2 {//1.the constructor is privatized and external direct calls are not allowed PrivateSingleton2 () {}//2. Justdeclares a unique instance of a class that is not actually instantiated, using the private static modifier Private StaticSingleton2 instance; //3.provides a way to get an instance, with public static decoration
//(1). Resolve thread insecure-------plus sync (each thread gets an instance each time to determine the lock, less efficient) Public Static synchronizedSingleton2 getinstance () {if(Instance = =NULL){ returnInstance =NewSingleton2 (); } returninstance; } //(2). Resolve Thread insecurity-----Double check Lock (improve efficiency) Public StaticSingleton2 GetInstance2 () {/*if the first thread obtains a singleton instance object, the subsequent thread does not need to enter the synchronization code block when it acquires the instance .*/ if(Instance = =NULL){ synchronized(Singleton2.class) { if(Instance = =NULL) {instance=NewSingleton2 (); } } } returninstance; } }
Output:
PackageCom.sun.singleton; Public classMain { Public Static voidMain (string[] args) {//instantiate two times for comparison testing//a hungry man typeSingleton s1=singleton.getinstance (); Singleton S2=singleton.getinstance (); if(s1==12) {System.out.println ("A hungry man type is the same instance"); }Else{System.out.println ("A hungry man type is not the same instance"); } //Lazy TypeSingleton2 s3 =singleton2.getinstance (); Singleton2 S4=singleton2.getinstance (); if(s3==S4) {System.out.println ("Lazy is the same example"); }Else{System.out.println ("Lazy type is not the same instance"); } }}
Results:
Description Singleton mode is the same object instantiation is a shared memory space.
The difference between the two:
A Hungry man mode: Class loading time is slow (loading time instantiation), but the runtime is faster, thread-safe.
Lazy mode: Class loading is faster, but the runtime is relatively slow (invoking the method when the instantiation), the thread is not secure.
Beginner's study notes: This is the record of their own learning process, to facilitate their own review, good memory than bad writing, a lot of advice.
Art to
------------2015-11-20 16:42:51
A single-instance pattern of Java design patterns