30 lines
1.2 KiB
Markdown
30 lines
1.2 KiB
Markdown
# About this kata
|
|
|
|
This kata refers to the book Effective Java by Joshua Bloch.
|
|
It addresses
|
|
|
|
Item 2: Consider a builder when faced with many constructor parameters.
|
|
|
|
# Problem description
|
|
telescoping constructor pattern ???
|
|
-> In Java, there is no support for default values for constructor parameters. As a workaround, a technique called "Telescoping constructor" is often used. A class has multiple constructors, where each constructor calls a more specific constructor in the hierarchy, which has more parameters than itself, providing default values for the extra parameters. The next constructor does the same until there is no left.
|
|
|
|
public Person(String firstName, String lastName) {
|
|
this(firstName, lastName, "No description available");
|
|
}
|
|
|
|
public Person(String firstName, String lastName, String description) {
|
|
this(firstName, lastName, description, -1);
|
|
}
|
|
|
|
public Person(String firstName, String lastName, String description, int age) {
|
|
this.firstName = firstName;
|
|
this.lastName = lastName;
|
|
this.description = description;
|
|
this.age = age;
|
|
}
|
|
|
|
JavaBeans pattern precludes the possibility of making a class immuteable (->Item 17!)
|
|
|
|
# Clues
|