By Joshua Bloch
…
Advantages of static factory methods over constructors:
Boolean.valueOf(boolean)) java.util.ServiceLoader)Limitations/Shortcomings of static factory methods:
Date.from(instant)Set<Rank> faceCards = EnumSet.of(JACK, QUEEN, KING); from and ofgetInstance but is in a different classcreateInstance but is in a different classgetType and newType, e.g. Collections.list()Problem: many fields where some of them are not always necessary.
Solution: Builder pattern
static member of the class it creates)NutritionFacts cocaCola = new NutritionFacts.Builder(240, 8).calories(100).sodium(35).carbohydrate(27).build();
Disadvantages of Builder:
…
…