| |
我们要得到一个对象,马上想到构造方法,其实,静态工厂方法也可以替我们做到。例如,在jdk1.4里面,boolean用了一个静态方法来得到一个boolean对象: public static boolean valueof(boolean b) question:这个方法实际上并不返回一个object? 对的, sun的jdk 帮助里面讲: if a new boolean instance is not required, this method should generally be used in preference to the constructor boolean(boolean). 但是,不是说静态工厂方法所有时候都不返回一个object,只是不是所有时候都返回一个object。 使用静态工厂方法和构造方法比较的优缺点: 优点: 1. 静态工厂方法都是有自己的名字,容易理解. 2. 静态工厂方法并不要求每次都生成一个对象.比如上面提到的boolean的public static boolean valueof(boolean b)就不会返回一个object. sun的jdk帮助文档里面也提到,在不需要得到一个对象的情况下,使用valueof方法可以得到性能的提升. 而且这样,可以控制类在生的期间该类的句柄数,这个作用在单态(item 21)和类型安全(item 21)都是有用的. 3. 静态工厂方法可以返回该类的任何子类,这给了程序员更大的选择空间. 比如collections,用了很多静态工厂方法来得到immuable的set,map之类的. 而且静态工厂方法可以强迫user通过接口来引用被返回的对象,而不是每次都使用构造函数,这是一个好习惯. 静态工厂方法返回的类,在编写含有静态工厂方法这个类的时候,可以并不存在,这就提供了一种灵活的service provider framework.比如java的密码系统扩展(jce),provider为user提供多个api实现,framework提供一种机制来register这些实现,用户只需直接使用api,而不用考虑自己在使用哪个实现。 下面是这种机制的程序框架: [code]import java.util.*; // 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(); } } public static void main(string[] args) { system.out.println(getinstance("nonexistentfoo")); } } class defaultfoo extends foo { }[/code] 静态工厂方法的缺点: 1. 仅仅有静态工厂方法而没有public的构造方法的类不能被继承…这也不是没有好处--可能能迫使程序员使用复合来代替继承. 2. 有时候可能会导致与其他的static方法混淆.这个缺点可以通过使用标准的命名方法来区别于其他的static方法. 现在有两种方法已经开始形成标准: valueof(type k)-返回和k有相同值的句柄. getinstance-返回一个复合参数所描述的句柄.但是不一定和参数具有相同的值..这个方法在provider framework里面是很有用的. 所以,我们不要每次都毫不犹豫的使用构造方法,有时候静态工厂方法可能是更加合适的.但是当你无法取舍的时候,还是使用构造方法吧,因为它是比较普通和标准的.
|
|