Robert

5%
Flag icon
Luckily, there is a third alternative that combines the safety of the telescoping constructor pattern with the readability of the JavaBeans pattern. It is a form of the Builder pattern
Robert
A fourth option that would be nice to have in some situations—this nutrition example is a good one—is named parameters with defaults.
Julie
· Flag
Julie
One thing that's nice about the Builder pattern is that it supports setting defaults for optional parameters, and you can make the required parameters explicit via the instantiation of the builder
Robert
· Flag
Robert
Named parameters can support this, too (and do in Ruby they do). If there's a default defined, it's optional; and if there isn't, it's required.

Builders are likely better if there's complexity (like v…
Effective Java
Rate this book
Clear rating
Open Preview