我使用Joshua Bloch介绍的JavaBuilder模式。有时,我会发现某些字段与基元类型相比,用默认值初始化更昂贵。
因此,我的策略是。
-
我延迟了这些字段的默认值初始化操作。
-
在构建期间,如果调用方以前没有设置它们,那么我将只将它们初始化为默认值。
我不确定是不是
好的
这样做?可能会有什么收获吗?比如,线程安全问题?到目前为止,我没有看到任何问题。
package sandbox;
import java.util.Calendar;
/**
*
* @author yccheok
*/
// 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;
private final java.util.Calendar calendar; // !!!
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;
// We suppose to provide a default value for calendar. However, it may
// seem expensive. We will do it later during build.
private java.util.Calendar calendar = null;
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() {
// !!!
if (this.calendar == null) {
this.calendar = Calendar.getInstance();
}
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;
calendar = builder.calendar;
}
}