遇到多个构造器形参时要考虑用构建器
引言
遇到多个构造器时要考虑用构建器(builder)
重叠构造器(telescoping constructor)
// Telescoping constructor pattern - does not scale well! - Pages 11-12
/**
* 营养成分表
*/
public class NutritionFacts {
private final int servingSize; // (mL) required
private final int servings; // (per container) required
private final int calories; // optional
private final int fat; // (g) optional
private final int sodium; // (mg) optional
private final int carbohydrate; // (g) optional
/**
* 重叠构造器模式:提供第一个只有必要参数的构造器
*/
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;
}
public static void main(String[] args) {
NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27);
}
}
缺点:
① 当有许多参数的时候,客户端代码会很难编写,并且仍然较难以阅读。
② 如果读者想知道那些值是什么意思,必须很仔细地数着这些参数来探个究竟。如果客户端不小心颠倒了其中两个参数的顺序,编译器也不会出错,但是在程序运行时会出现错误行为。
JavaBeans模式
// JavaBeans Pattern - allows inconsistency, mandates mutability - Pages 12-13
public class NutritionFacts {
// Parameters initialized to default values (if any)
private int servingSize = -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) {
fat = val;
}
public void setSodium(int val) {
sodium = val;
}
public void setCarbohydrate(int val) {
carbohydrate = val;
}
public static void main(String[] args) {
// 调用一个无参构造器来创建对象,然后调用setter()方法来设置
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
}
}
缺点:
① 无法保证一致性:JavaBean模式自身有着很严重的缺点,因为构造过程被分到了几个调用中,在构造过程中JavaBean可能处在不一致的状态。试图使用处于不一致状态的对象,将会导致失败,这种失败调试起来十分困难
② 阻止了把类做成不可变
Builder模式
// Builder Pattern - Pages 14-15
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;
/**
* 注意是static修饰, 作为NutritionFacts的静态成员类
* 详细的讨论请参考:
* https://stackoverflow.com/questions/5007355/builder-pattern-in-effective-java
*/
public static class Builder {
// Required parameters
private final int servingSize;
private final int servings;
// Optional parameters - initialized to default values
private int calories = 0;
private int fat = 0;
private int carbohydrate = 0;
private int sodium = 0;
public Builder(int servingSize, int servings) {
System.out.println("NutritionFacts.Builder init this[" + this + "]");
this.servingSize = servingSize;
this.servings = servings;
}
// builder的setter()方法返回builder本身,以便可以把调用链接起来
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;
}
/**
* build()方法可以检验这些约束条件
* 如果违反了任何约束条件build方法就应该抛出IllegalSelectorException
*/
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
/**
* 注意这是private修饰的
*/
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.Builder(int, int)创建一个builder对象
// 然后客户端在builder对象调用类似于setter的方法,来设置每个相关的可选参数
// 最后客户端调用无参的build()方法来生成不可变的对象NutritionFacts
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
NutritionFacts cocaCola2 = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).fat(7).build();
}
}
优点:
① Builder方式创建的对象,在调用 build() 方法之前是不会创建NutritionFacts 对象的,所有的属性设置都必须在 build() 方法之前,而且创建了NutritionFacts 对象后就不可以更改其属性了,这就保证了对象状态的唯一性,而且代码的可读性也提高了。
② 如果有些参数是必填的,可以加到 Builder 的构造函数中
缺点:
Builder模式的确也有自身不足,为了创建对象,必须先创建它的构建器,在某些十分注重性能的情况下,可能就成为问题了。
参考
时间: 2024-10-16 05:14:42