首页 > 代码库 > 第二章:创建和销毁对象。ITEM2:遇到多个构造器参数时要考虑用构建器。

第二章:创建和销毁对象。ITEM2:遇到多个构造器参数时要考虑用构建器。

如果一个类中有大量的可选参数,有以下几种方式:

1、重叠构造器:

package com.twoslow.cha2;/** * 重叠构造器可行,但是当由许多参数的时候,客户端代码很难编写。 * @author sai * */public class Item201 {    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 Item201(int servingSize, int servings) {        this(servingSize, servings, 0);    }    public Item201(int servingSize, int servings, int calories) {        this(servingSize, servings, calories, 0);    }    public Item201(int servingSize, int servings, int calories, int fat) {        this(servingSize, servings, calories, fat,0);    }        public Item201(int servingSize, int servings, int calories, int fat,int carbohydrate) {        this(servingSize, servings, calories, fat, carbohydrate,0);    }        public Item201(int servingSize, int servings, int calories, int fat,int carbohydrate,int sodium) {        this.servingSize = servingSize ;         this.servings = servings ;         this.calories = calories ;         this.fat = fat ;         this.carbohydrate = carbohydrate ;         this.sodium  = sodium ;     }}

2、javaBeans模式,提供setter方法:

package com.twoslow.cha2;/** * JavaBeans模式,调用setter方法设置每个必要的参数。 * 构造过程被分到了几个调用中,在构造过程中JavaBean可能处于不一致的状态。 * @author sai * */public class Item202 {    private int servingSize = -1;    private int servings = -1;    private int calories = 0;    private int fat = 0;    private int sodium = 0;    private int carbohydrate = 0;    public Item202() {    }    public void setServingSize(int servingSize) {        this.servingSize = servingSize;    }    public void setServings(int servings) {        this.servings = servings;    }    public void setCalories(int calories) {        this.calories = calories;    }    public void setFat(int fat) {        this.fat = fat;    }    public void setSodium(int sodium) {        this.sodium = sodium;    }    public void setCarbohydrate(int carbohydrate) {        this.carbohydrate = carbohydrate;    }}

3、构建器:

package com.twoslow.cha2;/** * 构建器,不足:1、为了创建对象,必须显创建它的构建器。 * 2、比重叠构造器更加冗长,只在有很多参数的时候才使用,>=4. * @author sai * */public class Item203 {    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 {//可以通过实现泛型接口构建任何类型的Builder        private final int servingSize ;         private final int servings ;                 private int calories = 0;         private int fat  = 0;         private int sodium  = 0;         private int carbohydrate  = 0;                public Builder(int servingSize , int servings) {            this.servingSize = servingSize ;             this.servings = servings ;         }                public Builder calories(int calories) {            this.calories = calories ;             return this ;         }        public Builder fat(int fat) {            this.fat = fat ;             return this ;         }        public Builder sodium(int sodium) {            this.sodium = sodium ;             return this ;         }        public Builder carbohydrate(int carbohydrate) {            this.carbohydrate = carbohydrate ;             return this ;         }                public Item203 build() {            return new Item203(this) ;        }    }            public Item203(Builder builder) {            this.servingSize = builder.servingSize ;             this.servings = builder.servings ;             this.calories = builder.calories ;             this.fat = builder.fat ;             this.sodium = builder.sodium ;             this.carbohydrate = builder.carbohydrate ;         }}

 

第二章:创建和销毁对象。ITEM2:遇到多个构造器参数时要考虑用构建器。