类中的Java Builder设计模式冗余字段声明及其构建器

时间:2015-02-13 04:53:13

标签: java builder-pattern

经典的Builder Pattern要求在要构建的类中声明字段,并在构建器类中声明完全相同的字段。当存在许多字段时,这会导致问题,并且在重构期间,字段类型不会保持同步。这是我的意思的一个例子(我从Joshua Block的一篇文章中借用了这个代码示例):

// 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;

    public static class Builder {
        private int servingSize   = 0;
        private int servings      = 0;
        private int calories      = 0;
        private int fat           = 0;
        private int carbohydrate  = 0;
        private int sodium        = 0;

        public Builder servingSize(int val)
            { servingSize = val;   return this; }
        public Builder servings(int val)
            { servings = val;      return this; }
        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() {
            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;
    }
}

现在让我们假设servingSize需要从int更改为long并且此更改是在NutritonFacts中完成的,但是,不小心,也在静态Builder中完成。

不可否认,字段数量较少。 NutritionFacts有6个字段,因此Builder也是如此。如果有20或100个字段怎么办?在NutritionFactsBuilder中复制它们将是真正的痛苦。有没有更好的方法可以避免所有重复和类型同步错误的可能性?

2 个答案:

答案 0 :(得分:3)

您可以使用NutritionFacts对象存储构建器的状态:

// 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;

    public static class Builder {
        private NutritionFacts state = new NutritionFacts(0,0,0,0,0,0);

        public Builder servingSize(int val) { 
            state = new NutritionFacts(val, state.servings, state.calories, state.fat, state.sodium, state.carbohydrate);
            return this;
        }
        [...]

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

    private NutritionFacts(Builder builder) {
        servingSize  = builder.state.servingSize;
        servings     = builder.state.servings;
        calories     = builder.state.calories;
        fat          = builder.state.fat;
        sodium       = builder.state.sodium;
        carbohydrate = builder.state.carbohydrate;
    }

    private NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
        this.servingSize  = servingSize;
        this.servings     = servings;
        this.calories     = calories;
        this.fat          = fat;
        this.sodium       = sodium;
        this.carbohydrate = carbohydrate;
    }
}  

由于NutritionFacts是不可变的,因此需要为每次更改构建一个新的状态对象,这可能是也可能不值得。

如果您可以使NutritionFacts的内部状态相互关联,但使用私有设置器会更容易 - 根据定义而不是final关键字使对象不可变:

// Builder Pattern
public class NutritionFacts {
    private int servingSize = 0;
    private int servings = 0;
    private int calories = 0;
    private int fat = 0;
    private int sodium = 0;
    private int carbohydrate = 0;

    public static class Builder {
        private NutritionFacts state = new NutritionFacts();

        public Builder servingSize(int val) { 
            state.servingSize = val;
            return this;
        }
        public Builder servings(int val) { 
            state.servings = val;
            return this;
        }
        [...]

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

    private NutritionFacts(Builder builder) {
        servingSize  = builder.state.servingSize;
        servings     = builder.state.servings;
        calories     = builder.state.calories;
        fat          = builder.state.fat;
        sodium       = builder.state.sodium;
        carbohydrate = builder.state.carbohydrate;
    }
}  

答案 1 :(得分:2)

您可以使用步骤构建器模式增强经典构建器模式,以便构建具有无脑界面,易于使用,不可能出错的对象。有关详细信息,请参阅此post