By piccoloprincipe
via css.dzone.com
Published: Oct 24 2011 / 09:16
In the scenario of today, an if chooses to execute different behavior depending on the type of an object. We should define "type" very looosely; for example, it may be the class of the object or one of the interfaces it implements (instanceof); the value of one of the object's fields (usually enumerative).
Each of these discriminants has the power to be used in dozens of identical conditionals throughout the codebase. We can replace easily this kind of conditionals with polymorphism: we move the relevant logic in the object whose type is queried.
Add your comment