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

Java Builder Design Pattern Redundant Field Declaration in Class and its Builder

classic Builder Pattern 要求在 class-to-be-built 中声明字段,并且在构建器 class 中声明完全相同的字段。当有很多字段并且在重构期间字段类型不保持同步时,这可能会导致问题。这是我的意思的一个例子(我从 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 也有 6 个字段。如果有 20 或 100 个字段怎么办?在 NutritionFactsBuilder 中复制它们将是真正的痛苦。有没有更好的方法来避免所有重复和潜在的类型同步错误?

您可以使用 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;
    }
}  

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