When you are in a pure OO model, then instanceof definitely the smell of code.
If, however, you are not using a 100% OO model or you need to inject material into it from the outside, then instanceof or equivalents ( isXXX() , getType() , ...) can have their own applications.
A general “rule” would be to avoid it whenever possible, especially when you control the type hierarchy and can use, for example, a subtype polytype. The idea is not to ask the object what type it is and to do something with it, but rather directly or indirectly request the object through a visitor (essentially a double polymorphism) to perform some actions.
eljenso May 01 '10 at 16:56 2010-05-01 16:56
source share