我使用Joshua Bloch介绍的Java builder模式。有时候,我发现某些字段比原始类型与默认值一样初始化更加昂贵。
因此,我的策略是什么。
我不确定这样做是否好?有没有可能发生的捕获?就像,线程安全问题?到目前为止,我没有看到任何问题。
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;
}
}
答案 0 :(得分:4)
如果我真的希望日历为空,该怎么办?那么你的模式将不起作用。否则,对我来说似乎没问题。
答案 1 :(得分:3)
我没有看到它的问题。 Builder模式实际上是一个帮助完成某些任务的API。只要您遵守模式/ API的原则,您就可以在API的掩护下做任何您想做的事情。我认为延迟加载昂贵的资源很好,只需确保记录您认为开发人员应该了解的任何行为。
答案 2 :(得分:2)
小心,因为Calendar是可变的,所以你应该在NutritionFacts构造函数中防御它。