创建对象的三种模式
- 静态工厂和构造器模式:将所有参数传递到构造函数中,这种方式不能很好地扩展到大量的可选参数。
- JavaBean模式:调用一个无参数构造器,然后调用setter方法来设置每个必要的参数以及可选参数。
- Builder模式:让客户端利用所有必要的参数调用构造器(或静态工厂方法),得到一个builder对象,然后客户端在builder对象上调用类似setter方法,来设置每个相关的可选参数。
构造器模式:
提供一个只有必要参数的构造器,第二个构造器有一个可选参数,第三个构造器有两个可选参数.....
// Telescoping constructor pattern - does not scale well!
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;
}
}
这种情况下,当我们调用某个构造器时,有些你本不想设置的参数,但还是得为它们传值。
NutritionFacts cocaCola =new NutritionFacts(240, 8, 100, 0, 35, 27);
如上,我们如果不需要fat参数,但却不得不传递一个值给它,以满足构造器需求。此外,当构造器存在多个参数时,客户端代码会变得很难编写,并且难以阅读。
JavaBean模式:
调用一个无参数构造器,然后调用setter方法来设置每个必要的参数以及可选参数。
// JavaBeans Pattern - allows inconsistency, mandates mutability
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;
}
}
调用方法:
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
使用JavaBean模式,可以解决构造器模式可选参数问题,并且代码的可读性也提高了不少。
但JavaBean本身也存在严重的缺点:
- 因为构造过程中被划分到了几个调用中,在构造过程中JavaBean可能处于不一致的状态。类无法仅仅通过检验构造器参数的有效性来保证一致性。
- JavaBean模式下无法将类设置为不可变(即final类型),那么这样需要我们去确保它的线程安全。
Builder模式:
让客户端利用所有必要的参数调用构造器(或者静态工厂),得到一个builder对象。然后客户端在builder对象上调用类似于setter的方法,来设置每个相关的可选参数。最后,客户端调用无参的build方法来生成不可变的对象。
在NutritionFacts类中构造一个嵌套类Builder,来构建builder对象:
public static class Builder{
//其中servingSize和servings为必要参数,因此设置为final类型,通过构造器传入的参数来初始化
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;
}
//设置可选参数,并返回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方法获取到NutritionFacts对象
public NutritionFacts build(){
return new NutritionFacts(this);
}
}
在NutritionFacts中通过builder对象来初始化参数:
public class NutritionFacts {
//都设置为final类型,不可变对象,以保证线程的安全
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
//传入builder对象,通过builder对象来初始化参数
public NutritionFacts(Builder builder){
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
完整代码:
public class NutritionFacts {
//都设置为final类型,不可变对象,以保证线程的安全
private final int servingSize;
private final int servings;
private final int calories;
private final int fat;
private final int sodium;
private final int carbohydrate;
//传入builder对象,通过builder对象来初始化参数
public NutritionFacts(Builder builder){
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
public static class Builder{
//其中servingSize和servings为必要参数,因此设置为final类型,通过构造器传入的参数来初始化
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;
}
//设置可选参数,并返回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方法获取到NutritionFacts对象
public NutritionFacts build(){
return new NutritionFacts(this);
}
}
}
调用方法:
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
calories(100).sodium(35).carbohydrate(27).build();
- 可见Builder模式不仅能保证构造器模式下的安全性(设置参数为final类型),也能保证像JavaBean模式下良好的可读性。
- 但其实Builder模式也存在一些不足之处,为了创建对象,必须先创建它的构建器(即Builder对象)。虽然构建器的开销在实践中可能不那么明显,但是在某些十分注重性能的情况下,这可能就是个问题了。
网友评论