Which is better (in Java), depending on instanceof a lot, or having my own getType function

Discussion in 'Java' started by PaulSchrum, Jan 23, 2007.

  1. PaulSchrum

    PaulSchrum Guest

    This question may already be in a FAQ. If it is, please accept my
    apology for not noticing it, and please direct me to the location.

    I am designing a class library for human language grammar

    I have an abstract class, Phrase.

    I have several concrete classes subclassing that: NounPhrase,
    PrepositionalPhrase, etc.

    While I am in the design phase, I would like to know if there is an
    agreed-upon recommended answer to my question:

    If I think I might be doing a lot of checking of the types of various
    instances of Phrase, is it better for me to use "instanceof" all over
    the place, or is it better to have an enum of my own types and switch
    on the return value of a getType() method, or some other approach?


    - Paul
    PaulSchrum, Jan 23, 2007
    1. Advertisements

  2. Neither - you should try model that behavior via

    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Jan 23, 2007
    1. Advertisements

  3. PaulSchrum

    PaulSchrum Guest

    Arne Vajhøj wrote:
    the return value of a getType() method, or some other approach?

    Thanks for your response.

    In the example I am about to give, the NounPhrase, etc. is in the
    Model. Say I want to have balloon help pop up over the phrase in the
    View. Would the proper implementation of polymorphism simply call a
    function like:

    abstract class Phrase
    abstract LMstring getSimpleDescription();


    - Paul
    PaulSchrum, Jan 23, 2007
  4. PaulSchrum

    Lew Guest

    Use neither "instanceof" nor any kind of switching on or enumeration of types.
    Instead, use polymorphism - for each term in your grammar, have classes that
    inherit a common interface (such as "Term"), and do the right thing in the
    context of their own type. Then your control loop does things along the lines of

    Term term = term.getTerm( token ); // run-time type appropriate to token

    Each term's commonMethod() will do the particular action appropriate to its
    own type, which it knows without any of that "instanceof" or laborious
    switching fooferol.

    The general principle is that no parent type should know any of the identities
    of its subclasses, and that no client of that supertype should know of those
    subclasses either. Subtypes, of course, have full knowledge of their supertypes.

    Spend time studying polymorphism.

    - Lew
    Lew, Jan 23, 2007
  5. Yes!
    By doing
    Phrase phrase = new WhateverPhrase(...);
    LMString s = phrase.getSimpleDescription();
    the particular method of your actual non-abstract subclass gets called.
    As Lew already pointed out, you therefore don't need 'instanceof' or a
    getType() method.
    [BTW: I remember the light-bulb-moment, when I grasped this years ago.]
    Thomas Fritsch, Jan 23, 2007
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.