Java 子类化Java Builder类

给这篇Dobbs博士文章,尤其是Builder模式,我们如何处理将Builder子类化的情况?以该示例的简化版本为例,在该示例中我们想使用子类来添加GMO标签,一个简单的实现将是:

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;

}

}

现在,我们可以编写如下代码:

GMOFacts.Builder b = new GMOFacts.Builder();

b.GMO(true).calories(100);

但是,如果我们弄错了订单,那么一切都会失败:

GMOFacts.Builder b = new GMOFacts.Builder();

b.calories(100).GMO(true);

问题当然是NutritionFacts.Builder返回a NutritionFacts.Builder,而不是a GMOFacts.Builder,那么我们该如何解决此问题,还是有更好的模式可供使用?

注意:这个类似问题的答案提供了我上面的课程;我的问题是关于确保构建器调用正确顺序的问题。

回答:

你可以使用泛型来解决它。我认为这称为“好奇地重复通用模式”

将基类构建器方法的返回类型设为通用参数。

public class NutritionFacts {

private final int calories;

public static class Builder<T extends Builder<T>> {

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;

}

}

现在使用派生的类构建器作为通用参数实例化基本构建器。

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 子类化Java Builder类 的全部内容, 来源链接: utcz.com/qa/424339.html

回到顶部