Item2 Use builder when faced with multiple constructor params

Builder pattern is a good choice when designing classes whose constructors or static factories would have more than a handful of parameters, especially if many of the parameters are optional or of identical type. 《effective java》

Telescope constructor pattern -- Too much constructors
JavaBean Pattern (use setter to initialize optional fields) -- allow inconsistency, mandates mutability

Builder Pattern Example

public class NutritionFacts {
  private final int fact1;
  private final int fact2;
  private final int fact3;  // optional value
  
  public abstract class Builder {
    private final int fact1;
    private final int fact2;
    
    public Builder(int fact1, int fact2) {
      fact1 = fact1;
      fact2 = fact2;
    }
    public Builder fact3(int fact3) {
      fact3 = fact3;
    }
    public NutritionFacts build() {
      return new NutritionFacts(this);
    }
  }
  
  public NutritionFacts(Builder builder) {
    this.fact1 = builder.fact1;
    this.fact2 = builder.fact2;
    this.fact3 = builder.fact3;
  }
}

NutritionFacts facts = new NutritionFacts.Builder(1, 2).fact3(3). build();

Class Hierarchies Builder Pattern

public abstract class Pizza {
  public enum Topping {HAM, MUSHROOM, ONION}
  final Set toppings

  abstract static class Builder> {
    EnumSet toppings = EnumSet.noneOf(Topping.class);
    public T addTopping(Topping topping) {
      toppings.add(Objects.requreNonNull(topping));
      return self();
    }
    
    abstract Pizza build();
    protected abstract T self();
  }
  Pizza(Builder builder) {
    toppings = builder.toppings.clone();
  }
}


public NyPizza extends Pizza {
  public enum Size {SMALL, MEDIUM, LARGE}
  private final Size size;  // required field
  
  public static class Builder extends Pizza.Builder {
    private int final size;
    public Builder(Size size) {
      this.size = Objects.requireNonNull(size);
    }
    @override public NyPizza build() {
        return new NyPizza(this);
    }
    @override protected Builder self() {
        return this;
    }
  }
  NyPizza(Builder builder) {
    super(builder);
    size = builder.size;
  }
}

public class Calzone {
  private boolean isSauceIn;
  public static class Builder extends Pizza.Builder {
    private boolean isSauceIn = false;
    public Builder sauceInside(){
      isSauseIn = true;
      return this;
    }
    @override public Calzone build() {
      return new Calzone(this);
    }
    @override protected Builder self() { return this;}
    
  }
   private Calzone(Builder builder) {
      super(builder);
      sauceInside = builder.sauceInside;
    }
}

NyPizza nyPizza = new NyPizza.Builder(Size.SMALL).addTopping(SAUSAGE).build();

你可能感兴趣的:(Item2 Use builder when faced with multiple constructor params)