when to use instanceof?

Discussion in 'Java' started by Digital Puer, Sep 4, 2003.

  1. Digital Puer

    Digital Puer Guest

    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.
    Digital Puer, Sep 4, 2003
    #1
    1. Advertising

  2. Digital Puer

    Roedy Green Guest

    On 4 Sep 2003 13:19:41 -0700, (Digital Puer)
    wrote or quoted :

    >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".


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 4, 2003
    #2
    1. Advertising

  3. Digital Puer

    VisionSet Guest

    "Roedy Green" <> wrote in message
    news:...
    > On 4 Sep 2003 13:19:41 -0700, (Digital Puer)
    > wrote or quoted :
    >
    > >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".


    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?

    --
    Mike W
    VisionSet, Sep 5, 2003
    #3
  4. Digital Puer

    Roedy Green Guest

    On Fri, 5 Sep 2003 00:02:14 +0100, "VisionSet" <>
    wrote or quoted :

    >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.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
    Roedy Green, Sep 5, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet
    Replies:
    13
    Views:
    740
    Dale King
    Nov 18, 2003
  2. HalcyonWild
    Replies:
    30
    Views:
    1,246
  3. Replies:
    21
    Views:
    21,318
  4. François Grondin

    Looking for a way to avoid use of instanceOf

    François Grondin, Dec 4, 2008, in forum: Java
    Replies:
    14
    Views:
    679
    François Grondin
    Dec 4, 2008
  5. Marcel Müller
    Replies:
    0
    Views:
    139
    Marcel Müller
    Aug 21, 2013
Loading...

Share This Page