《Effective java》--创建和销毁对象 2.遇到多个构参数是要考虑用构建器


eg 

普通方法

poublic NutritionFacts{

  private final int servingSize;

  private final int servings;

  private final int calories;

  private final int fat;

  private final int sodium;

  private final int carbohdrate;

  public NutritionFacts(int servingSize,int serving){

     this(servingSize,servings,0);

  }

  public NutritionFacts(int servingSize,int serving,int calories){

    this(servingSize,serving,calories,0);

  }

  public NutritionFacts(int servingSize,int serving,int calories,int fat){

    this(servingSize,serving,calories,fat,0);

  }

  public NutritionFacts(int servingSize,int serving,int calories,int fat,int sodium){

    this(servingSize,serving,calories,fat,sodium,0);

  }

  public NutritionFacts(int servingSize,int serving,int calories,int fat,int sodium,int carbohydrate){

    this.servingSize = servingSize;

    this.serving = serving;

    this.calories= calories;

    this.fat= fat;

    this.sodium = sodium;

    this.carbohydrate= carbohydrate;

  }

}

此方法缺点,调用需要许多你不想设置的参数,但又不得不传。如果参数过多会导致错误。

javaBeans模式

public class NutritionFacts{

  private int servingSize;

  private int servings;

  private int calories;

  private int fat;

  private int sodium;

  private int carbohdrate;

  public NutritionFacts(){}

  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 setCarbohdrate(int val){carbohdrate = val;}

}

NutritionFacts cocaCola = new NutritionFacts ();

cocaCola .setServingSize(10);

cocaCola .setServings(20);

cocaCola .setCalories(10);

cocaCola .setFat(10);

cocaCola .setSodium(10);

cocaCola .setCarbohdrate(10);

此方法缺点:需要付出额外的努力来确保线程安全

Builder模式

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 carbohdrate;

   public static class Builder{

        //Required parameters

       private final int servingSize;

       private final int serving;



      //Optional parameters - initialized to default values

      private int calories = 0;

      private int fat = 0;

      private int sodium = 0;

      private int carbohdrate = 0;

      pubblic Builder(int servingSize,int servings){

           this.servingSize = servingSize;

           this.servings = servings;

      }

      public Builder calorise(int val){

          calories = val;

          return this;

      }

      public Builder fat(int val){

          fat = val;

          return this;

      }

      public Builder carbohdrate(int val){

           carbohdrate = val;

            return this;

      }

      public Builder sodium(int val){

           sodium = val;

            return this;

      }

      public Nutrition build(){

          return new NutrionFacts(this);

      }

   }

   private NutritionFacts(Builder builder){

       servingSize = builder.servingSize;

       servings = builder.servings;

       calories = builder.calories;

       fat = builder.fat;

       sodium = builder.sodium; 

       carbohydrate = builder.carbohydrate;

   }

}

Nutrition cocalCola = new Nutrition.Builder(240,80).calories(100).sodium(35).carbohydrate(27).builder();   

劣势:为了创建对象,必须先创建他的构建器,虽然创建构建器开销的开销在实践中可能不那么明显,但是在某些十分重视性能的情况下,可能就成问题了,Build模式还比重叠构造更加冗长,因此只有在很多参数时才使用

你可能感兴趣的:(《Effective java》--创建和销毁对象 2.遇到多个构参数是要考虑用构建器)