Goal: Object creation.
Consider Bloch's Item 2:
Consider a Builder When Faced With Many Constructor Parameters.
Implement all three approaches given by Bloch
(telescoping constructors, JavaBeans pattern, and Builder pattern)
for the NutritionFacts
example.
You will end up with 3 variants of the
NutritionFacts
class.
Expand NutritionFacts
to include another attribute (eg "protein").
Modify each of the three classes above to reflect this new attribute.
(The point here is to have you see the maintenance perspective;
you only need to turn in the final versions of the classes.)
Create JUnit tests for the three classes.
For the JavaBeans pattern class,
include a JUnit test which (erroneously)
accesses
the values before initialization is complete.
(You will need to add a get()
method to the interface
to observe this effect.)