对Java Builder类进行子类化 [英] Subclassing a Java Builder class
问题描述
给予 Dobbs博士文章,特别是Builder模式,我们如何处理子类化Builder的情况?以我们要子类添加GMO标签的示例的缩小版本,一个天真的实现将是:
Give this Dr Dobbs article, and the Builder Pattern in particular, how do we handle the case of subclassing a Builder? Taking a cut-down version of the example where we want to subclass to add GMO labelling, a naive implementation would be:
public class NutritionFacts {
private final int calories;
public static class Builder {
private int calories = 0;
public Builder() {}
public Builder calories(int val) { calories = val; return this; }
public NutritionFacts build() { return new NutritionFacts(this); }
}
protected NutritionFacts(Builder builder) {
calories = builder.calories;
}
}
子类:
public class GMOFacts extends NutritionFacts {
private final boolean hasGMO;
public static class Builder extends NutritionFacts.Builder {
private boolean hasGMO = false;
public Builder() {}
public Builder GMO(boolean val) { hasGMO = val; return this; }
public GMOFacts build() { return new GMOFacts(this); }
}
protected GMOFacts(Builder builder) {
super(builder);
hasGMO = builder.hasGMO;
}
}
现在,我们可以这样编写代码: p>
Now, we can write code like this:
GMOFacts.Builder b = new GMOFacts.Builder();
b.GMO(true).calories(100);
但是,如果我们收到订单错误,它们都会失败:
But, if we get the order wrong, it all fails:
GMOFacts.Builder b = new GMOFacts.Builder();
b.calories(100).GMO(true);
问题当然是 NutritionFacts.Builder
返回一个 NutritionFacts.Builder
,而不是一个 GMOFacts.Builder
,那么我们如何解决这个问题呢?一个更好的模式使用?
The problem is of course that NutritionFacts.Builder
returns a NutritionFacts.Builder
, not a GMOFacts.Builder
, so how do we solve this problem, or is there a better Pattern to use?
注意:这个答案类似的问题提供了我上面的课程;我的问题是关于确保构建器调用的顺序正确的问题。
Note: this answer to a similar question offers up the classes I have above; my question is regarding the problem of ensuring the builder calls are in the correct order.
推荐答案
可以使用泛型来解决它。我认为这被称为好奇重复的通用模式
You can solve it using generics. I think this is called the "Curiously recurring generic patterns"
使基类构建器方法的返回类型成为一个通用参数。
Make the return type of the base class builder methods a generic argument.
public class NutritionFacts {
private final int calories;
public static class Builder<T extends Builder> {
private int calories = 0;
public Builder() {}
public T calories(int val) {
calories = val;
return (T) this;
}
public NutritionFacts build() { return new NutritionFacts(this); }
}
protected NutritionFacts(Builder builder) {
calories = builder.calories;
}
}
现在使用派生类构建器实例化基础构建器通用参数。
Now instantiate the base builder with the derived class builder as the generic argument.
public class GMOFacts extends NutritionFacts {
private final boolean hasGMO;
public static class Builder extends NutritionFacts.Builder<Builder> {
private boolean hasGMO = false;
public Builder() {}
public Builder GMO(boolean val) {
hasGMO = val;
return this;
}
public GMOFacts build() { return new GMOFacts(this); }
}
protected GMOFacts(Builder builder) {
super(builder);
hasGMO = builder.hasGMO;
}
}
这篇关于对Java Builder类进行子类化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!