posts - 10,comments - 4,trackbacks - 0

Consider providing static factory methods instead of
constructors 考虑以静态工厂方法取代构造函数

The normal way for a class to allow a client to obtain an instance is to provide a public
constructor.

A class can provide a public static factory method, which is simply
a static method that returns an instance of the class

such as
public static Boolean valueOf(boolean b) {
return (b ? Boolean.TRUE : Boolean.FALSE);
}

advantages
1:One advantage of static factory methods is that, unlike constructors, they have names.
一个类静态工厂方法是有名字的.If the parameters to a constructor do not, in and of themselves, describe the object being returned, a static factory with a well-chosen name can make a class easier to use and the resulting client code easier to read. 如BigInteger(int, int,Random)只知道new了一个BigInteger对象,而BigInteger.probablePrime()能说明返回的可能是一个素数对象.另外,如果一个构造函数仅仅由于参数顺序不同而意思也不同.静态工厂方法就更加有用了.它更能说明函数的意思.因为它有一个名字.
the reason are : If the parameters to a constructor do not, describe the object being
returned, a static factory with a well-chosen name can make a class easier to use and the
resulting client code easier to read.

2:A second advantage of static factory methods is that, unlike constructors, they are not
required to create a new object each time they're invoked.每次请求时,不需要重新创建一个对象----单例的意思.这可以在任何时刻控制这个对象,同时在比较的时候也不需要用equals而用= =就可以解决了.

3:A third advantage of static factory methods is that, unlike constructors, they can return
an object of any subtype of their return type.可以返回一个原返回类型的子类型对象.
One application of this flexibility is that an API can return objects without making their
classes public.Hiding implementation classes in this fashion can lead to a very compact API.这种灵活性的一个应用是,一个A P I可以返回一个对象,同时又不使该对象的类成为公有的。以这种方式把具体的实现类隐藏起来,可以得到一个非常简洁的A P I。这项技术非常适合
于基于接口的框架结构,因为在这样的框架结构中,接口成为静态工厂方法的自然返回类型
sample:
// Provider framework sketch
public abstract class Foo {
// Maps String key to corresponding Class object
private static Map implementations = null;
// Initializes implementations map the first time it's called
private static synchronized void initMapIfNecessary() {
if (implementations == null) {
implementations = new HashMap();
// Load implementation class names and keys from
// Properties file, translate names into Class
// objects using Class.forName and store mappings.
...
}
}
public static Foo getInstance(String key) {
initMapIfNecessary();
Class c = (Class) implementations.get(key);
if (c == null)
return new DefaultFoo();
try {
return (Foo) c.newInstance();
} catch (Exception e) {
return new DefaultFoo();
}
}
}

公有的静态工厂方法所返回的对象的类不仅可以是非公有的,而且该类可以随着每次调用
而发生变化,这取决于静态工厂方法的参数值。只要是已声明的返回类型的子类型,都是允
许的。而且,为了增强软件的可维护性,返回对象的类也可以随着不同的发行版本而不同。
disadvantages:

1:The main disadvantage of static factory methods is that classes without public or protected constructors cannot be subclassed.
类如果不含公有的或者受保护的构造函数,就不能被子类化。对于公有的静态工厂所返回的非公有类,也同样如此

2:A second disadvantage of static factory methods is that they are not readily distinguishable from other static methods.


In summary, static factory methods and public constructors both have their uses, and it pays to understand their relative merits. Avoid the reflex to provide constructors without first considering static factories because static factories are often more appropriate. If you've weighed the two options and nothing pushes you strongly in either direction, it's probably best to provide a constructor simply because it's the norm.




posted on 2006-03-30 17:08 dodoma 阅读(208) 评论(0)  编辑  收藏 所属分类: java基础

只有注册用户登录后才能发表评论。


网站导航: