1.1 考虑用静态工厂方法代替构造器
示例:
public static Boolean valueOf()boolean b { return b ? Boolean.TRUE : Boolean.FALSE; }
静态工厂方法的优势:
1、相对于构造器,静态工厂方法有名称。
例如:构造器BigInteger(int,int,Random)返回的BigInteger可能为素数,如果用名为BigInteger.probablePrime的静态工厂方法来表示,显然更为清楚。(1.4的发行版本增加了这个方法)
2.静态工厂方法不必在每次调用它们的时候都创建一个新对象
静态工厂方法可以返回预先构建好的实例,将对象重复利用。
3、静态工厂方法可以返回原返回类型的任何子类型的对象
例如:Collections类可以返回不可修改的集合,同步集合等。
4、静态工厂方法在创建参数化类型实例的时候,可以使得代码变得更加简洁
例如:Map<String, List<Stirng>> m = new HashMap<String, List<Stirng>>();
如果HashMap提供了这个静态方法:
public static <K, V> HashMap<K, V> newInstance() {
return new HashMap<K, V>();
}
则可以这样声明:
Map<String, List<Stirng>> m = HashMap.newInstance();
静态工厂方法的惯用名称:
valueOf
of
getInstance
newInstance
getType
newType
1.2 遇到多个构造器参数时考虑使用构建器
静态工厂方法和构造器的共用局限性:它们都不能很好地扩展到大量的可选参数。
构建器实例:
public class NutritionFacts { private final int servingSize; private final int servings; private final int calories; private final int fat; private final int sodium; private final int carbohydrate; public static class Builder { private final int servingSize; private final int servings; private int calories = 0; private int fat = 0; private int sodium = 0; private int carbohydrate = 0; public Builder(int servingSize, int servings) { this.servingSize = servingSize; this.servings = servings; } public Builder calories(int val) { calories = val; return this; } public Builder fat(int val) { fat = val; return this; } public Builder sodium(int val) { sodium = val; return this; } public Builder carbohydrate(int val) { carbohydrate = val; return this; } public NutritionFacts build() { return new NutritionFacts(this); } } private NutritionFacts(Builder builder) { servingSize = builder.servingSize; servings = builder.servings; calories = builder.calories; fat = builder.fat; sodium = builder.sodium; carbohydrate = builder.carbohydrate; } public static void main(String[] args) { NutritionFacts n = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build(); } }
1.3 通过私有构造器强化不可实例化的能力
实例:
public class UtilityClass { private UtilityClass() { throw new AssertionError(); } ... }
由于构造器是私有的,所以不可以在该类的外部访问它。AssertionError可以避免不小心在类的内部调用构造器。这种做法还会导致该类不能被子类化。
1.4 避免创建不必要的对象
实例:
下面这个类建立了一个模型:其中有个人,并且有一个isBabyBoomer方法,用来检验这个人是不是生于1946至1964年的人。
class Person { private final Date birthDate; public Person(Date birthDate) { this.birthDate = birthDate; } public boolean isBabyBoomer() { Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0); Date boomStrat = gmtCal.getTime(); gmtCal.set(1956, Calendar.JANUARY, 1, 0, 0, 0); Date boomEnd = gmtCal.getTime(); return birthDate.compareTo(boomStrat) >= 0 && birthDate.compareTo(boomEnd) < 0; } }
isBabyBoomer每次被调用的时候,都会创建一个Calendar,一个TimeZone,两个Date,这种情况完全是可以避免的。
改进后的类:
public class Person { private final Date birthDate; public Person(Date birthDate) { this.birthDate = birthDate; } private static final Date BOOM_START; private static final Date BOOM_END; static { Calendar gmtCal = Calendar.getInstance(TimeZone.getTimeZone("GMT")); gmtCal.set(1946, Calendar.JANUARY, 1, 0, 0, 0); BOOM_START = gmtCal.getTime(); gmtCal.set(1956, Calendar.JANUARY, 1, 0, 0, 0); BOOM_END = gmtCal.getTime(); } public boolean isBabyBoomer() { return birthDate.compareTo(BOOM_START) >= 0 && birthDate.compareTo(BOOM_END) < 0; } }
改进后的Person类只在初始化的时候创建Calendar,TimeZone和Date。