Consider a builder when faced with many constructor parameters
The static factories and constructors are ways of instantiating objects but both of them share a limitation. They scale poorly with the increase of (optional) params.
The traditional ways of dealing with this: telescoping constructor
Telescoping constructor pattern – does not scale well!
The telescoping constructor pattern scales poorly. It is hard to write with many parameters and even harder to read.
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 serving size, 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; } }
JavaBeans 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 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; } }
A better approach: Builder pattern
// 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 { // 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; } }
Facts about builder pattern
- Typically a static member class
- The client code is easy to write and, more importantly, easy to read
- Easy to incorporate validity checks
- Check on object fields after copying params from the builder
- The failing check will throw an IllegalArgumentException with exception details
- Well suited to class hierarchies
- Generic builder with recursive type parameters can construct any subclass
- Abstract self() simulates self-type which, combined with covariant return typing, obviates the need for casting
- Flexible
Disadvantages builder pattern
- Cost of creating a builder
- More verbose than a telescoping constructor
Conclusion
Always start with a builder in the first place especially if we have more than a handful of params. A builder is much safer than JavaBeans. In the case of a builder pattern, the client code is much easier to read and write than telescoping constructors.