第一种处理方式:
/**
*
* 这个类有很多个参数
*
* 其中的servingSize和servings是必须的,其他的是可选的
*
* 我们可以采用重叠构造器(telescoping constructor)
*
* 在这个模式之下,我们最少只需要提供参数个数最少的那个构造器即可,其他的参数默认为0
*
* 缺点:如果每个参数都要传入,实例化的时候很南阅读。
*/
public class NurtritionFacts1 {
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 NurtritionFacts1(int servingSize, int servings) {
this(servingSize, servingSize, 0);
}
public NurtritionFacts1(int servingSize, int servings, int calories) {
this(servingSize, servings, calories, 0);
}
public NurtritionFacts1(int servingSize, int servings, int calories, int fat) {
this(servingSize, servings, calories, fat, 0);
}
public NurtritionFacts1(int servingSize, int servings, int calories,
int fat, int sodium) {
this(servingSize, servings, calories, fat, sodium, 0);
}
public NurtritionFacts1(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 int getServingSize() {
return servingSize;
}
public int getServings() {
return servings;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
public int getSodium() {
return sodium;
}
public int getCarbohydrate() {
return carbohydrate;
}
}
第二种方式:
/**
- 对于重叠构造器的难以阅读,我们采用JavaBean模式代替,增加它的可阅读性。
- 对每一个成员变量都提供一个setter方法
- 也就是把本来该在类实例生成的时候就设置的参数延迟到实例创建成功以后再设置
- 这样使得代码很好阅读,但是带来一个问题就是可能出现不同步的问题
*/
public class NurtritionFacts2 {
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 NurtritionFacts2() {
}
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;
}
public int getServingSize() {
return servingSize;
}
public int getServings() {
return servings;
}
public int getCalories() {
return calories;
}
public int getFat() {
return fat;
}
public int getSodium() {
return sodium;
}
public int getCarbohydrate() {
return carbohydrate;
}
}
第三种方式:
/**
- 这种方式既有重叠构造器的安全性也有JavaBean模式的可读性
- 这就是Builder模式。
- 把成员变量用final修饰,就表示一旦初始化,后面就能修改,以保证安全性。
*/
public class NurtritionFacts3 {
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 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 val) {
calories = val;
return this;
}
public Builder fat(int val) {
fat = val;
return this;
}
public Builder sodium(int val) {
sodium = val;
return this;
}
public Builder carbohydrate(int val) {
carbohydrate = val;
return this;
}
public NurtritionFacts3 build() {
return new NurtritionFacts3(this);
}
}
private NurtritionFacts3(Builder builder) {
servingSize = builder.servingSize;
servings = builder.servings;
calories = builder.calories;
fat = builder.fat;
sodium = builder.sodium;
carbohydrate = builder.carbohydrate;
}
}
测试类代码:
/**
*
-
测试类
*/
public class Main {public static void main(String[] args) {
NurtritionFacts1 nurtritionFacts1 = new NurtritionFacts1(55, 85); NurtritionFacts2 nurtritionFacts2 = new NurtritionFacts2(); nurtritionFacts2.setServingSize(96); nurtritionFacts2.setServings(8555); NurtritionFacts3 nurtritionFacts3 = new NurtritionFacts3.Builder(555, 7777).calories(89).fat(888).sodium(456464).build();
}
}