代码改变世界

Item 2---遇到构造器具有多个参数时,要考虑用构建器;Builder模式

2015-03-20 20:45  ttylinux  阅读(164)  评论(0编辑  收藏  举报

问题,面对这种一个构造器具备多个参数的问题,现有的做法是使用重叠构造器的方式,该方式存在的问题:

public class NutritionFacts {
    private final int servingSize; // (mL) required
    private final int servings; // (per container) required
    private final int calories; // optional
    private final int fat; // (g) optional
    private final int sodium; // (mg) optional
    private final int carbohydrate; // (g) optional

    public NutritionFacts( int servingSize, int servings) {
        this(servingSize, servings, 0);
    }

    public NutritionFacts( int servingSize, int servings, int calories) {
        this(servingSize, servings, calories, 0);
    }

    public NutritionFacts( int servingSize, int servings, int calories, int fat) {
        this(servingSize, servings, calories, fat, 0);
    }

    public NutritionFacts( int servingSize, int servings, int calories, int fat,
            int sodium) {
        this(servingSize, servings, calories, fat, sodium, 0);
    }

    public 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;
    }

    public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27);
    }
}

重叠构造器存在的问题是,客户端使用NutritionFacts类容易出错,也比较困难,困难在于,客户端要记住构造器的每个参数的含义及顺序。这种方式,可读性也差,原因就是,要读懂一句代码,就要记住该构造器所用所有参数的含义。

-------------------------------------------------------------------------------------------------------------------------------------------------------------

一种替代的解决办法:

public class NutritionFacts {
    // Parameters initialized to default values (if any)
    private int servingSize = -1; // Required; no default value
    private int servings = -1; // "     " "      "
    private int calories = 0;
    private int fat = 0;
    private int sodium = 0;
    private int carbohydrate = 0;

    public NutritionFacts() {
    }

    // Setters
    public void setServingSize(int val) {
        servingSize = val;
    }

    public void setServings(int val) {
        servings = val;
    }

    public void setCalories(int val) {
        calories = val;
    }

    public void setFat(int val) {
        fat = val;
    }

    public void setSodium(int val) {
        sodium = val;
    }

    public void setCarbohydrate(int val) {
        carbohydrate = val;
    }

    public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts();
        cocaCola.setServingSize(240);
        cocaCola.setServings(8);
        cocaCola.setCalories(100);
        cocaCola.setSodium(35);
        cocaCola.setCarbohydrate(27);
    }
}
 
使用JavaBean的方式,使用空参数的构造器,创建对象;然后,把构造器参数的设置,放到setter方法来实现,如上。
存在的问题,构造过程,被分到了几个调用中。上述代码块,不能被分裂开来执行,如果分裂开执行,会导致创建的对象状态不一致。
 
存在的问题就是,会导致创建的对象,其状态不一致。这在多线程程序中会出现。比如,cocaCola.setSodium(35);在执行这句代码之前,对象cocaCola已经被其它线程使用了,这就会导致使用了不状态不正确的cocaCola对象。
简单地说,就是创建对象,并且设置对象状态的代码,并不原子化。
------------------------------------------------------------------------------------------------------------
另一种解决办法:Builder模式
 
    public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8)
                .calories(100).sodium(35). carbohydrate(27).build();
    }

 

对比:

重叠构造器方式
 public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27);
    }

JavaBean方式

  public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts();
        cocaCola.setServingSize(240);
        cocaCola.setServings(8);
        cocaCola.setCalories(100);
        cocaCola.setSodium(35);
        cocaCola.setCarbohydrate(27);
    }
 
Builder模式,客户端在使用的时候,需要记住两个必备参数的含义及顺序;然后,如果需要其它可选构造参数,则通过调用对应的方法来实现。
比如,需要可选参数calories,则调用calories(int val)方法。这个可读性要好,用户通过阅读该方法的名称得知参数的含义,知道此时传递的构造器参数,是用来设定calories的值的。
这就解决了重叠构造器方式存在的问题,可读性差,客户端程序员使用容易出错的问题。
另外,使用Builder模式,创建对象时,代码都是同时执行,而不是分成各个语句,是一条独立的语句,而不是分成多条语句。
 
Builder模式:
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 {
        // Required parameters
        private final int servingSize;
        private final int servings;

        // Optional parameters - initialized to default values
        private int calories = 0;
        private int fat = 0;
        private int carbohydrate = 0;
        private int sodium = 0;

        public Builder(int servingSize, int servings) {
            this.servingSize = servingSize;
            this.servings = servings;
        }

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

    public static void main(String[] args) {
        NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8)
                .calories(100).sodium(35).carbohydrate(27).build();
    }
}