Builder应该计算参数值

时间:2015-12-08 14:14:18

标签: java design-patterns builder

假设我有以下课程和建设者:

public class A(){
    private B b;
    private C c;

    private A(Builder builder) {
       this.b = builder.b;
       this.c = bulder.c;
    }

    public static class Builder() {
       private B b;
       private C c;

       public Builder setB(B b){
           this.b = b;
           return this;
       }

       public Builder setC(C c){
           this.C = c;
           return this;
       }

       public A build(){
          return new A(this);
       }
    }
}

现在让我们说b和c需要以某种方式计算(例如,基于D的列表)。可能问题是:

根据某些计算将填充参数b和c放在何处,也就是说,我应该在A,构建器或其他地方进行计算吗?

1 个答案:

答案 0 :(得分:0)

我会根据列表D是否可以在A知道它后被选​​中来选择该选项。如果可以,那么属性需要能够重新计算,所以应该自己进行计算。如果D是固定列表,一旦提供给A,则属性可以“缓存”并预先计算