In Java, the singleton design pattern is a very common design pattern, the concept of a single-instance design pattern, do not do too much introduction, the following will be introduced in one of four single-case design patterns:
1. The first type of single-case design mode
1.1 Code implementation
Package com.singleton.one;/** * First Singleton design mode * @author Administrator * */public class Singletonone {//instantiate private static Sing Letonone instance = new Singletonone ()//private construction Method Private Singletonone () {System.out.println ("---singleton one---");} Get singleton instance public static Singletonone getinstance () {return instance;} static method public static void Hello () {System.out.println ("---singleton one Hello---");}}
1.2 Related tests
Package Com.singleton.test;import static Org.junit.assert.asserttrue;import Org.junit.test;import Com.singleton.one.singletonone;public class Testsingletonone {/** * test is a singleton */@Testpublic void Testsingletonone () { Singletonone one1 = Singletonone.getinstance (); Singletonone One2 = Singletonone.getinstance (); Asserttrue (one1 = = One2);} /** * Test the first single-instance load mode * Run Result: *---singleton one---*---singleton one hello---* 1, first executed the private construction method; * 2, then execute the static method * Conclusion: * When the simple call static method, it is not necessary to call the private constructor, i.e. * cannot implement lazy loading */@Testpublic void Testsingletononeload () {Singletonone.hello ()}}
2. The second type of single case
2.1 Code implementation
package com.singleton.one;/** * the second single-case design mode * @ author administrator * */public class singletontwo {private static singletontwo instance ;p Rivate singletontwo () {System.out.println ("-- singleton two --");} /** * lazy loading processing, but thread insecure * @return *///public static SingleTonTwo GetInstance () {//if (instance == null) {//instance = new singletontwo ();//}//// return instance;//}/** * The above comment getinstance is non-thread-safe, the method does a synchronous processing; * but because of threading, often the efficiency is not high */ Public static synchronized singletontwo getinstance () {if (instance == null) {Instance = new singletontwo ();} Return instance;} Public static void hello () {System.out.println ("-- single ton two hello --");}}
2.2 Related Tests
Package Com.singleton.test;import static Org.junit.assert.asserttrue;import Org.junit.test;import com.singleton.one.singletontwo;/** * Test four types of singleton-the second one (lazy loading) * @author Administrator * */public class Testsingletontwo {/* * * Test is a single case */@Testpublic void Testsingletontwo () {Singletontwo one1 = singletontwo.getinstance (); Singletontwo One2 = Singletontwo.getinstance (); Asserttrue (one1 = = One2);} /** * Test the second single test * run Result: *--double ton Two Hello--* Conclusion: * Private construction method is not called when running a static method (i.e. lazy loading) */@Testpublic void Testsingletontwo Load () {Singletontwo.hello ();}}
3, internal class implementation of a single case
3.1 Code implementation
Package com.singleton.one;/** * Inner class implementation Singleton */public class Singletonthree {private Singletonthree () {System.out.println ("-- SingleTon three--");} /** * Inner class instantiation * @author Administrator * */private static class Singletonthreehandler{public static Singletonthree instance = new Singletonthree ();} /** * Through the inner class, get instance * @return */public static Singletonthree getinstance () {return singletonthreehandler.instance;} public static void Hello () {System.out.println ("--singletonthree Hello--");}}
3.2 Related tests
Package Com.singleton.test;import static Org.junit.assert.asserttrue;import Java.lang.reflect.constructor;import Java.lang.reflect.invocationtargetexception;import Org.junit.test;import com.singleton.one.singletonthree;/** * Test four types of singleton-a third singleton (internal static Class) * @author Administrator * */public class Testsingletonthree {/** * test is a singleton */@Testpublic void Te Stsingletonthree () {Singletonthree one1 = singletonthree.getinstance (); Singletonthree One2 = Singletonthree.getinstance (); Asserttrue (one1 = = One2);} /** * The third type of single test * operation Result: *--Singletonthree Hello--* Conclusion: * There is no trigger construction method, only the method internal logic is executed, and lazy loading is realized. */@Testpublic void Testsingletonthreeload () {Singletonthree.hello ();}}
4, enumeration implementation of a single case
4.1 Code implementation
Package com.singleton.one;/** * Enumeration type Implementation Singleton * @author Administrator * */public enum Singletonenum {/** * Singleton example */instance;priv Ate Singletonenum () {System.out.println ("--enum Singleton--");} public static void Hello () {System.out.println ("--eum Singleton Hello--");}}
4.2 Related tests
Package Com.singleton.test;import Org.junit.test;import Static Org.junit.assert.*;import com.singleton.one.singletonenum;/** * Test Enumeration Type Singleton * @author Administrator * */public class Testsingletonenum {/** * test is a singleton * /@Testpublic void Testsingletonenum () {Singletonenum enum1 = singletonenum.instance; Singletonenum enum2 = singletonenum.instance;asserttrue (enum1 = = enum2);} /** * Test for lazy loading * Results: *--enum Singleton---*--eum Singleton Hello--* This enumeration type, cannot lazy loading * * @Testpublic void TESTSINGLETONENUM1 () {Singletonenum.hello ();}}
5, four kinds of single-case efficiency test
package com.singleton.test;import org.junit.after;import org.junit.before;import Org.junit.test;import com.singleton.one.singletonenum;import com.singleton.one.singletonone;import com.singleton.one.SingleTonThree;import com.singleton.one.SingleTonTwo;/** * test the efficiency of four individual cases * operation results: * ---singleton one--- *3 *-- singleton two -- *57 *-- singleton three -- * 4 *-- enum singleton -- *3 * Results Analysis: * The second time consuming the most, due to its synchronous processing, resulting in more overhead * @author administrator * */public class testsingleton {long start;long end;@ Beforepublic void setup () {Start = system.currenttimemillis ();} @Testpublic void testsingletonone () {for (int i = 0; i < 1000000; i++) {Singletonone instance = singletonone.getinstanCE ();}} @Testpublic void testsingletontwo () {for (int i = 0; i < 1000000; i++) {singletontwo instance = singletontwo.getinstance ();}} @Testpublic void testsingletonthree () {for (int i = 0; i < 1000000; i++) {singletonthree instance = singletonthree.getinstance ();}} @Testpublic void testsingletonenum () {for (int i = 0; i < 1000000; i++) {singletonenum enum1 = singletonenum.instance;}} @Afterpublic void teardown () {end = system.currenttimemillis (); System.out.println (End - start);}}
6. Summary
The first single case: no lazy loading effect;
The second single example: Lazy loading, if divided into thread-safe and non-thread-safe two, when using thread-safe synchronization, will affect efficiency.
The third kind of singleton: lazy loading, and suitable for multi-threading, high efficiency, but can be instantiated through Java reflection of multiple instances, so in general, the way to achieve better;
The fourth type of Singleton: must be above jdk5.0, not implemented lazy loading, multithreaded friendly, and can not be instantiated through Java reflection of multiple instances.
In general, it is recommended to use a third and a fourth approach.
This article is from the "Java Program Ghost" blog, please be sure to keep this source http://793404905.blog.51cto.com/6179428/1541268