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

P

PaulSchrum

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

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?

Thanks.

- Paul
Schrum
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

PaulSchrum said:
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
relationships.

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?

Neither - you should try model that behavior via
polymorphism.

Arne
 
P

PaulSchrum

Arne Vajhøj wrote:
the return value of a getType() method, or some other approach?
Neither - you should try model that behavior via
polymorphism.

Arne

Arne,

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
 
L

Lew

PaulSchrum said:
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
relationships.

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?

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
term.commonMethod();

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
 
T

Thomas Fritsch

PaulSchrum said:
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();
}

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

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,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top