EJ.02複数のコンストラクタパラメータに遭遇した場合はコンストラクタを使用することを考慮する


  • オーバーラップコンストラクタ(telescoping constructor)モード-コードが複雑で、読みにくい
  • サンプルコード:
    // Telescoping constructor pattern - does not scale well!
    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 class Client {
    	public static void main(String[] args) {
    		NutritionFacts cocaCola = new NutritionFacts(240, 8, 100, 0, 35, 27);
    	}
    }
    

     
     
  • JavaBeanモード-スレッドが安全ではなく、クラスが可変
  • サンプルコード:
    // JavaBean Pattern - allows inconsistency, mandates mutability
    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 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 class Client {
    	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);
    	}
    }

     
  • ビルダーモード--多パラメータに適応し、安全で、書きやすく、メンテナンス性が高く、オーバーヘッドが大きい
  • サンプルコード:
    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;
    		// 
    		if(this.fat>this.sodium){
    		    throw new IllegalArgumentException("The fat must be little than sodium!!");
    		}
    	}
    }
    

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