Robert

28%
Flag icon
Such tagged classes have numerous shortcomings. They are cluttered with boilerplate, including enum declarations, tag fields, and switch statements. Readability is further harmed because multiple implementations are jumbled together in a single class. Memory footprint is increased because instances are burdened with irrelevant fields belonging to other flavors. Fields can’t be made final unless constructors initialize irrelevant fields, resulting in more boilerplate. Constructors must set the tag field and initialize the right data fields with no help from the compiler: if you initialize the ...more
Robert
If you find yourself doing if-else/switch on the same possible set of values more than twice, that's a likely flag that you've got a "tag" that is better off being class hierarchy.
Effective Java
Rate this book
Clear rating
Open Preview