Consider a builder when faced with many constructor parameters

Static factories and constructors share a limitation: they do not scale well to large
numbers of optional parameters. Traditionally, programmers have used the telescoping constructor pattern or JavaBeans pattern

Telescoping constructor pattern

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

The telescoping constructor pattern is hard to write client code when there are many parameters, and harder still to read it.

JavaBeans pattern

public class NutritionFacts {

   private int servingSize = -1;
   private int servings = -1;
   private int calories = 0;
   private int fat = 0;
   private int sodium = 0;
   private int carbohydrate = 0;
   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 setCarbohydrate(int val) {
     carbohydrate = val;
   }
}

JavaBean may be in an inconsistent state partway through its construction
Consider builder pattern as a alternative

// Builder Pattern
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 {
       private final int servingSize;
       private final int servings;
       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;
  }
}

Builder pattern is much easier to read and understand what is going on. The pattern gives you the impression that you can use named optional parameters.

How to use

</pre>
<pre>NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).
calories(100).sodium(35).carbohydrate(27).build();

Happy creating objects !

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s