标签:cto 可读性 cts 无法 .com etc stack https nfa
// Telescoping constructor pattern - does not scale well! - Pages 11-12
/**
* 营养成分表
*/
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);
}
}
// JavaBeans Pattern - allows inconsistency, mandates mutability - Pages 12-13
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) {
// 调用一个无参构造器来创建对象,然后调用setter()方法来设置
NutritionFacts cocaCola = new NutritionFacts();
cocaCola.setServingSize(240);
cocaCola.setServings(8);
cocaCola.setCalories(100);
cocaCola.setSodium(35);
cocaCola.setCarbohydrate(27);
}
}
// Builder Pattern - Pages 14-15
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;
/**
* 注意是static修饰, 作为NutritionFacts的静态成员类
* 详细的讨论请参考:
* https://stackoverflow.com/questions/5007355/builder-pattern-in-effective-java
*/
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) {
System.out.println("NutritionFacts.Builder init this[" + this + "]");
this.servingSize = servingSize;
this.servings = servings;
}
// builder的setter()方法返回builder本身,以便可以把调用链接起来
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;
}
/**
* build()方法可以检验这些约束条件
* 如果违反了任何约束条件build方法就应该抛出IllegalSelectorException
*/
public NutritionFacts build() {
return new NutritionFacts(this);
}
}
/**
* 注意这是private修饰的
*/
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.Builder(int, int)创建一个builder对象
// 然后客户端在builder对象调用类似于setter的方法,来设置每个相关的可选参数
// 最后客户端调用无参的build()方法来生成不可变的对象NutritionFacts
NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
NutritionFacts cocaCola2 = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).fat(7).build();
}
}
标签:cto 可读性 cts 无法 .com etc stack https nfa
原文地址:http://www.cnblogs.com/fireway/p/7109349.html