From 0fca949131b1e1bcac78f6a2aea8d2b88d8dbaaf Mon Sep 17 00:00:00 2001 From: Christoph Kroczek Date: Sat, 13 Jul 2019 21:31:24 +0200 Subject: [PATCH] item002 description started --- effectivejava/item002/README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/effectivejava/item002/README.md b/effectivejava/item002/README.md index 54e32a0..8b24499 100644 --- a/effectivejava/item002/README.md +++ b/effectivejava/item002/README.md @@ -1,5 +1,29 @@ # 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