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();
	}
}

 

 

你可能感兴趣的:(builder,构建器)