45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:静态工厂方法介绍

静态工厂方法介绍

2016-08-30 18:23:06 来源:www.45fan.com 【

静态工厂方法介绍

它只是一个简单的静态方法,返回类的一个实例.

1. 与构造函数不同,静态工厂方法具有名字.选用适当名字的静态工厂方法可以使一个类更易于使用,并且相应的客户代码更易于阅读.

2. 与构造函数不同,不要求非得创建一个新的对象.这使得一些非可变类可以使用一个预先构造好的实例,或者把已经构造好的实例缓冲起来.同时,静态工厂方法可以为重复的调用返回同一个对象,这也可以被用来控制"在某一时刻哪些实例应该存在." 好处1:使得一个类可以保证是一个singleton. 好处2:使非可变类可以保证"不会有两个相等的实例存在",即当且仅当a==b时才有a.equals(b)为true.这可以提高性能.

3. 与构造函数不同,它们可以返回一个原返回类型的子类型的对象.特别是,能够返回实现类对于调用者隐藏的对象.这项技术非常适合于基于接口的框架结构,因为这样的框架结构中,接口成为静态工厂方法的自然返回类型.调用者只能通过接口来引用被返回的对象,而不能通过实现类来引用被返回的对象.

静态工厂方法的命名包括getInstance和valueOf,但这并不是强制要求的,只要具有明确的意义即可.

示例:

静态工厂方法介绍静态工厂方法介绍publicclassComplexNumber{

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
/**

静态工厂方法介绍*Staticfactorymethodreturnsanobjectofthisclass.

静态工厂方法介绍
*/

静态工厂方法介绍静态工厂方法介绍publicstaticComplexNumbervalueOf(floataReal,floataImaginary){

静态工厂方法介绍
returnnewComplexNumber(aReal,aImaginary);

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍
//privateconstructorpreventssubclassing

静态工厂方法介绍静态工厂方法介绍privateComplexNumber(floataReal,floataImaginary){

静态工厂方法介绍fReal
=aReal;

静态工厂方法介绍fImaginary
=aImaginary;

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍
privatefloatfReal;

静态工厂方法介绍
privatefloatfImaginary;

静态工厂方法介绍}

JDK2中的示例:

LogManager.getLogManager

静态工厂方法介绍静态工厂方法介绍publicstaticLogManagergetLogManager(){

静态工厂方法介绍静态工厂方法介绍
if(manager!=null){

静态工厂方法介绍manager.readPrimordialConfiguration();

静态工厂方法介绍}

静态工厂方法介绍returnmanager;

静态工厂方法介绍}

Pattern.compile

静态工厂方法介绍静态工厂方法介绍publicstaticPatterncompile(Stringregex){

静态工厂方法介绍
returnnewPattern(regex,0);

静态工厂方法介绍}

Collections.unmodifiableCollection, Collections.synchronizeCollection等等

静态工厂方法介绍静态工厂方法介绍publicstatic<T>Collection<T>unmodifiableCollection(Collection<?extendsT>c){

静态工厂方法介绍
returnnewUnmodifiableCollection<T>(c);

静态工厂方法介绍}

静态工厂方法介绍静态工厂方法介绍publicstatic<T>Collection<T>synchronizedCollection(Collection<T>c){

静态工厂方法介绍
returnnewSynchronizedCollection<T>(c);

静态工厂方法介绍}

Calendar.getInstance

静态工厂方法介绍静态工厂方法介绍publicstaticCalendargetInstance(){

静态工厂方法介绍Calendarcal
=createCalendar(TimeZone.getDefaultRef(),Locale.getDefault());

静态工厂方法介绍cal.sharedZone
=true;

静态工厂方法介绍
returncal;

静态工厂方法介绍}

与reflection一起作用:

对象实例可以由user input,config file,或者任意来源决定.

完全支持在runtime时决定需要的对象实例.

静态工厂方法介绍静态工厂方法介绍publicclassCreator{

静态工厂方法介绍

静态工厂方法介绍
//#1:basicstaticfactorymethod

静态工厂方法介绍静态工厂方法介绍protectedstaticHellocreateHelloer(){

静态工厂方法介绍
returnnewHelloWorld();

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍
//#2:parametedstaticfactorymethod

静态工厂方法介绍protectedfinalstaticintWORLD=1;

静态工厂方法介绍

静态工厂方法介绍
protectedfinalstaticintCAMEL=2;

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
protectedstaticHellocreateHelloer(intid){

静态工厂方法介绍静态工厂方法介绍
if(id==1){

静态工厂方法介绍
returnnewHelloWorld();

静态工厂方法介绍}

静态工厂方法介绍静态工厂方法介绍if(id==2){

静态工厂方法介绍
returnnewHelloCamel();

静态工厂方法介绍}

静态工厂方法介绍returnnull;

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍
//#3:usereflection

静态工厂方法介绍静态工厂方法介绍protectedstaticHellocreateHelloer(Stringname){

静态工厂方法介绍静态工厂方法介绍
try{

静态工厂方法介绍Classc
=Class.forName(name);

静态工厂方法介绍
return(Hello)c.newInstance();

静态工厂方法介绍静态工厂方法介绍}
catch(ClassNotFoundExceptione){

静态工厂方法介绍e.printStackTrace();

静态工厂方法介绍静态工厂方法介绍}
catch(InstantiationExceptione){

静态工厂方法介绍e.printStackTrace();

静态工厂方法介绍静态工厂方法介绍}
catch(IllegalAccessExceptione){

静态工厂方法介绍e.printStackTrace();

静态工厂方法介绍}

静态工厂方法介绍returnnull;

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
publicstaticvoidmain(String[]args){

静态工厂方法介绍静态工厂方法介绍
/*

静态工厂方法介绍*nofactorymethod,usualcodes

静态工厂方法介绍
*/

静态工厂方法介绍HelloWorldhw=newHelloWorld();

静态工厂方法介绍hw.sayHello();

静态工厂方法介绍HelloCamelhc
=newHelloCamel();

静态工厂方法介绍hc.sayHello();

静态工厂方法介绍System.out.println(
"===========================");

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
/*

静态工厂方法介绍*basicfactorymethod

静态工厂方法介绍*

静态工厂方法介绍*fordifferentrequirement,youcannewdifferentsubclassand

静态工厂方法介绍*overridecreateHelloer()togetdedicatedimplementedclass

静态工厂方法介绍
*/

静态工厂方法介绍Helloh=createHelloer();

静态工厂方法介绍h.sayHello();

静态工厂方法介绍System.out.println(
"===========================");

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
/*

静态工厂方法介绍*parametedfactorymethod

静态工厂方法介绍
*/

静态工厂方法介绍h=createHelloer(WORLD);

静态工厂方法介绍h.sayHello();

静态工厂方法介绍h
=createHelloer(CAMEL);

静态工厂方法介绍h.sayHello();

静态工厂方法介绍静态工厂方法介绍
/*

静态工厂方法介绍*parametedfactorymethodbyreflection

静态工厂方法介绍
*/

静态工厂方法介绍System.out.println("===========================");

静态工厂方法介绍h
=createHelloer("HelloWorld");

静态工厂方法介绍h.sayHello();

静态工厂方法介绍h
=createHelloer("HelloCamel");

静态工厂方法介绍h.sayHello();

静态工厂方法介绍}

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
interfaceHello{

静态工厂方法介绍
voidsayHello();

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
classHelloWorldimplementsHello{

静态工厂方法介绍静态工厂方法介绍
publicvoidsayHello(){

静态工厂方法介绍System.out.println(
"Hello,World!");

静态工厂方法介绍}

静态工厂方法介绍}

静态工厂方法介绍

静态工厂方法介绍静态工厂方法介绍
classHelloCamelimplementsHello{

静态工厂方法介绍静态工厂方法介绍
publicvoidsayHello(){

静态工厂方法介绍System.out.println(
"Hello,Camel");

静态工厂方法介绍}

静态工厂方法介绍}

----------------------------------------------

作者: nirvana_li

日期:06-09-05
 

本文地址:http://www.45fan.com/dnjc/69975.html
Tags: 方法 静态 工厂
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部