日期:2014-05-20 浏览次数:20741 次
/** * @author HUHG * 2009-07-29 11:37 */ public class Nutrition { private final int servingSize; private final int servings; //option private final int calories; private final int fat; private final int sodium; //... more public static class Builder { private int servingSize; private int servings; //option private int calories= 0; private int fat= 0; private int sodium= 0; //... more public Builder(int servingSize, int servings){ this.servingSize= servingSize; this.servings= servings; } public Builder calories(int value){ calories= value; return this; } public Builder fat(int value){ fat= value; return this; } public Builder sodium(int value){ sodium= value; return this; } //... more public Nutrition build(){ return new Nutrition(this); } } private Nutrition(Builder builder){ servingSize= builder.servingSize; servings= builder.servings; calories= builder.calories; fat= builder.fat; sodium= builder.sodium; //... more } /** * @param args */ public static void main(String[] args){ Nutrition object= new Nutrition.Builder(100, 10).calories(20).fat(5).build(); //... use it } }