According to Effective Java 2ed Item 2
telescoping constructor pattern, in which you provide a constructor with only the required parameters, another with a single optional parameter, a third with two optional parameters, and so on, culminating in a constructor with all the optional parameters.
An example of constructors of the class applying this pattern is borrowed from When would you use the Builder Pattern?
code set 1a
Pizza(int size) { ... }
Pizza(int size, boolean cheese) { ... }
Pizza(int size, boolean cheese, boolean pepperoni) { ... }
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon) { ... }
size is a required parameter. cheese, pepperoni, and bacon are optional parameters. Supposing that I would like to provide the constructor like below.
code set 1b
Pizza(int size) { ... }
Pizza(int size, boolean cheese) { ... }
Pizza(int size, boolean pepperoni) { ... }
Pizza(int size, boolean cheese, boolean pepperoni, boolean bacon, int price, int) { ... }
Another example is that
code set 2
public AttributeKey(String key, Class<T> clazz)
public AttributeKey(String key, Class<T> clazz)
public AttributeKey(String key, Class<T> clazz, @Nullable T defaultValue, boolean isNullValueAllowed)
public AttributeKey(String key, Class<T> clazz, @Nullable T defaultValue, boolean isNullValueAllowed, @Nullable ResourceBundleUtil labels)
The two latest example I gave didn't follow the characteristic of telescoping constructor as code set 1a did
- Are code set 1b and 2 included in telescoping constructor? if not, what is it called?
- Comparing to using Builder Pattern, which one (between Builder and pattern of code set 1a,2) provides more benefits