您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 招聘面试 > 拓胜技术专家教你如何深入理解设计模式(单例模式)
1/25Singleton1.1xmlpropertiesJavaproperties1.2propertiesJavaJava1./**2.*3.*/4.publicclassAppConfig{5./**6.*A7.*/8.privateStringparameterA;9./**10.*B11.*/12.privateStringparameterB;13.14.publicStringgetParameterA(){15.returnparameterA;2/2516.}17.publicStringgetParameterB(){18.returnparameterB;19.}20./**21.*22.*/23.publicAppConfig(){24.//25.readConfig();26.}27./**28.*29.*/30.privatevoidreadConfig(){31.Propertiesp=newProperties();32.InputStreamin=null;33.try{34.in=AppConfig.class.getResourceAsStream(35.AppConfig.properties);36.p.load(in);37.//38.this.parameterA=p.getProperty(paramA);39.this.parameterB=p.getProperty(paramB);40.}catch(IOExceptione){41.System.out.println();42.e.printStackTrace();43.}finally{44.try{45.in.close();46.}catch(IOExceptione){47.e.printStackTrace();48.}49.}50.}51.}AppConfig.propertiesAppConfigJava3/251.paramA=a2.paramB=bJava1.publicclassClient{2.publicstaticvoidmain(String[]args){3.//4.AppConfigconfig=newAppConfig();5.6.StringparamA=config.getParameterA();7.StringparamB=config.getParameterB();8.9.System.out.println(paramA=+paramA+,paramB=+paramB);10.}11.}Java1.paramA=a,paramB=b1.3newAppConfigAppConfigAppConfigAppConfigAppConfigAppConfig4/252.12.21SingletonSingletongetInstance5/252.3JavaJava1./**2.*3.*/4.publicclassSingleton{5./**6.*7.*/8.privatestaticSingletonuniqueInstance=null;9./**10.*11.*/12.privateSingleton(){13.//14.}15./**16.*17.*@returnSingleton18.*/19.publicstaticsynchronizedSingletongetInstance(){20.//21.if(uniqueInstance==null){22.//23.uniqueInstance=newSingleton();24.}25.//26.returnuniqueInstance;27.}28./**29.*30.*/31.publicvoidsingletonOperation(){32.//33.}34./**6/2535.*36.*/37.privateStringsingletonData;38./**39.*40.*@return41.*/42.publicStringgetSingletonData(){43.returnsingletonData;44.}45.}Java1./**2.*3.*/4.publicclassSingleton{5./**6.*7.*/8.privatestaticSingletonuniqueInstance=newSingleton();9./**10.*11.*/12.privateSingleton(){13.//14.}15./**16.*17.*@returnSingleton18.*/19.publicstaticSingletongetInstance(){20.//21.returnuniqueInstance;22.}23.24./**25.*26.*/27.publicvoidsingletonOperation(){28.//7/2529.}30./**31.*32.*/33.privateStringsingletonData;34./**35.*36.*@return37.*/38.publicStringgetSingletonData(){39.returnsingletonData;40.}41.}2.4Java1./**2.*3.*/4.publicclassAppConfig{5./**6.*7.*/8.privatestaticAppConfiginstance=newAppConfig();9./**10.*AppConfig11.*@returnAppConfig12.*/13.publicstaticAppConfiggetInstance(){14.returninstance;15.}16.17./**18.*A19.*/20.privateStringparameterA;21./**8/2522.*B23.*/24.privateStringparameterB;25.publicStringgetParameterA(){26.returnparameterA;27.}28.publicStringgetParameterB(){29.returnparameterB;30.}31./**32.*33.*/34.privateAppConfig(){35.//36.readConfig();37.}38./**39.*40.*/41.privatevoidreadConfig(){42.Propertiesp=newProperties();43.InputStreamin=null;44.try{45.in=AppConfig.class.getResourceAsStream(46.AppConfig.properties);47.p.load(in);48.//49.this.parameterA=p.getProperty(paramA);50.this.parameterB=p.getProperty(paramB);51.}catch(IOExceptione){52.System.out.println();53.e.printStackTrace();54.}finally{55.try{56.in.close();57.}catch(IOExceptione){58.e.printStackTrace();59.}60.}61.}62.}9/25Java1.publicclassClient{2.publicstaticvoidmain(String[]args){3.//4.AppConfigconfig=AppConfig.getInstance();5.6.StringparamA=config.getParameterA();7.StringparamB=config.getParameterB();8.9.System.out.println(paramA=+paramA+,paramB=+paramB);10.}11.}3.1getInstanceJavaClassLoaderClassLoaderClassLoaderClassLoaderClassLoadergetInstance()getInstance10/253.2Java1.privateSingleton(){2.}Java1.publicSingletongetInstance(){2.}staticJava1.publicstaticSingletongetInstance(){2.}11/25Java1.publicstaticSingletongetInstance(){2.returnnewSingleton();3.}newJava1.privateSingletoninstance=null;staticstaticJava1.privatestaticSingletoninstance=null;getInstanceJava1.publicstaticSingletongetInstance(){2.//instance3.if(instance==null){4.//5.//instance6.instance=newSingleton();7.}8.//12/259.returninstance;10.}Java1.publicclassSingleton{2.//43.//5static4.privatestaticSingletoninstance=null;5.//16.privateSingleton(){7.}8.//29.//3static10.publicstaticSingletongetInstance(){11.//612.if(instance==null){13.//6.114.instance=newSingleton();15.}16.//6.217.returninstance;18.}19.}getInstancegetInstancegetInstanceJavastaticstaticstaticJavastaticJava13/251.publicclassSingleton{2.//43.//4.privatestaticSingletoninstance=newSingleton();5.//16.privateSingleton(){7.}8.//29.//3static10.//11.publicstaticSingletongetInstance(){12.//513.returninstance;14.}15.}staticstaticstaticgetInstance2214/2533.3LazyLoad3.415/253.5JavaJavaJavaMapJava1./**2.*Java3.*/4.publicclassJavaCache{5./**6.*MapKeyValue7.*keyString8.*/9.privateMapString,Objectmap=newHashMapString,Object();10./**16/2511.*12.*@paramkeykey13.*@returnkeyValue14.*/15.publicObjectgetValue(Stringkey){16.//17.Objectobj=map.get(key);18.//19.if(obj==null){20.//21.//22.obj=key+,value;23.//24.map.put(key,obj);25.}26.//27.returnobj;28.}29.}Java3.6JavaSingletonJava1./**2.*3.*/4.publicclassSingleton{5./**6.*key7.*/8.privatefinalstaticStringDEFAUL
本文标题:拓胜技术专家教你如何深入理解设计模式(单例模式)
链接地址:https://www.777doc.com/doc-3561282 .html