when to use instanceof?

D

Digital Puer

I've read that using instanceof to downcast is considered a
sign of poor OO design. What are some reasonable scenarios
where you would definitely need to use instanceof?

I can think of a few:

1. In old Java 1.0 GUI code, you see a lot of instanceof in
the action() method.

2. If you're using a ClassLoader and load a class, you need
use instanceof to see what kind of class you got.

3. If you're putting a mix of types into say, a vector, and
when you pull them out, you need to figure out which is which.
 
R

Roedy Green

3. If you're putting a mix of types into say, a vector, and
when you pull them out, you need to figure out which is which.

this one you get around by basing the objects you put in around some
common base class or make them implement a common interface. Then you
can often just use a method, without bothering to figure out what sort
of animal it was. That is the essence of oo. You don't CARE what kind
of animal it is, just so long as it understands the command "sit".
 
V

VisionSet

Roedy Green said:
this one you get around by basing the objects you put in around some
common base class or make them implement a common interface. Then you
can often just use a method, without bothering to figure out what sort
of animal it was. That is the essence of oo. You don't CARE what kind
of animal it is, just so long as it understands the command "sit".

Though not always.
I've just written some code where I have a Collection of subclasses, sure
they are sorted by their superclass attributes, but when I render them, they
are rendered in very different ways, here I use instanceof to render them
appropriately. Sure I could get round it by makeing them implement
renderable, but I don't want them to have any knowledge of the way they will
be used, they are just value objects. I suppose another alternative is to
wrap them in there own kind of renderer class, but this seems a bit bloaty
and over complex. The way I have it, the code that renders them is at the
logical place, where the rendering is done. Does this sound reasonable?
 
R

Roedy Green

The way I have it, the code that renders them is at the
logical place, where the rendering is done. Does this sound reasonable?

It is the traditional way of doing it. The problem is, when you come
to add a new thing to be rendered, you have to disturb existing code,
often making changes in it all over the place.

oo think says it is best to try to encapsulate that logic and put it
with each class, ( not necessarily the data class, but as associated
class) Then you can create new classes by inheritance and override
just the needed behaviour without doing any great analysis off the
whole rendering logic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top