第二条:遇到多个构造器参数时要考虑用构造器
静态工厂与构造器有个共同的局限性:它们都不能很好地扩展到大量可选参数;书中举了一个食品营养成分表例子,每分的含量,每罐的含量,每份的卡路里这些是必须的,一些总脂肪量,饱和脂肪等可以为零的我们称为可选域,对于这样的类,我们应该怎么编写呢?
第一种方法:重叠构造器
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 NutritionFacts(int servingSize, int servings) {
this(servingSize, servings, 0);
}
public NutritionFacts(int servingSize, int servings, int calories) {
this(servingSize, servings, calories, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
this.servingSize = servingSize;
this.servings = servings;
this.calories = calories;
this.fat = fat;
this.sodium = sodium;
this.carbohydrate = carbohydrate;
}
}
但是这样写有个弊端,就是如果中间出现了我们不想写的值,但是顺序后面有要写的值,就显得很累赘:
//中间出现了0
NutritionFacts cola = new NutritionFacts(210,8,100,0,35,27);
如果参数过长,我们还得一个个去对,所对应的是哪个参数,可读性很差。
第二种方法:JavaBeans模式
这种模式应该是大家比较常用的
// JavaBeans Pattern - allows inconsistency, mandates mutability
public class NutritionFacts{
private int seringSize = -1; // Required; no default value
private int servings = -1; //
private int calories = 0;
private int fat = 0;
private int sodium = 0;
private int carbohydrate = 0;
public NutritionFacts(){}
// Setters
public void setServingSize(int val) { servingSize = val;}
public void setServings(int val) {servings = val;}
public void setCalories(int val) {calories = val;}
public void setFat(int val) { cal = val;}
public void setSodium(int val) { sodium = val;}
public void setCarbohydrate(int val) {carbohydrate = val;}
}
我们先在类中给各个属性赋予初始值,然后再调用一个无参构造器,再通过调用一个个setter方法给必要的参数赋值,因为方法名字都是setXxx,我们可以清晰知道我们赋值的是哪一个参数;
NutritionFacts cola = new NutritionFacts();
cola.setServingSize(100);
cola.setServings(20);
cola.setCalories(2);
cola.setFat(0);
cola.setSodium(22);
cola.setCarbohydrate(22);
但是这种方法也有它严重弊端,因为它把构造过程分到了几个调用之中,可能会出现不一致的状态:在多线程环境中,我们可能刚刚执行完NutritionFacts cola = new NutritionFacts();这一行,还没来得及进行调用set赋值,就被其他线程调用了,但是对象却又是生成了的,这就会造成错误;因为是基于JavaBeans模式,所以阻止了把这个做成不可变类的可能(因为有set方法且变量无final修饰),如何确保这个类线程安全成了很大的问题。
第三种方法:Builder模式
"既能保证像重叠构造器的安全性,又能保证像JavaBeans模式那么好的可读性"要怎么做呢,不可变类+可读 = Builder模式:
// Builder Pattern
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 {
// Required paramters;
private final int servingSize;
private final servings;
// Optional parameters - initialized to default vlaues
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 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 carbohydrate(int val) {
carbohydrate = val;
return this;
}
public Builder sodium(int val) {
sodium = 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;
}
}
NutritionFacts是不可变的,所以默认参数值都单独放在一个地方。builder的setter返回对象本身,以便于把调用链接起来,客户端代码:
NutritionFacts cola = new NutritionFacts(210,8).calories(100).sodium(23).carbohydrate(22).build();