Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Replacement for using instanceOf operator

Posted on 2006-06-28
5
667 Views
Last Modified: 2006-11-18
I am using the instanceof operator in the following manner:

      if (data instanceof ScoringActionData){
            ScoringActionData scoringActionData = (ScoringActionData)data;
            execute(scoringActionData);      
      } else if (data instanceof AggregationActionData){
            AggregationActionData aggregationActionData = (AggregationActionData)data;
            execute(aggregationActionData);
      }

Both the ScoringActionData and the AggregationActionData objects extend ActionDataImpl which in turn implements ActionData.

I would like to find a way to get away from using the instanceof operator, but haven't been able to find a suitable replacement.  I have looked in several places, but haven't been able to find anything. Does anyone have any ideas?  A code example would be appreciated.  Thanks.
0
Comment
Question by:jbcarrico
  • 2
  • 2
5 Comments
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17003747
get the class name

for e.g,

String classname = data.getClass() . getName () ;

it will give the fully qualified name of the class ( including package )

if your data is of type Hashtable,
you will get java.util.HashTable in your classname.
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17003757
if you want only the class nam,

String className = c.getName() .substring( c.getName().lastIndexOf(".") + 1 ,c.getName() .length() ) ;

get the className and check for the condition using className.equals()
0
 

Author Comment

by:jbcarrico
ID: 17003858
Is there any way I can cause the code to branch to the appropriate  'execute()' method by using the superclass, ActionData or something like that?  What I'm trying to do is get away from any type of branching statement.  I want to implement something that will branch to the appropriate 'execute()' without casting the 'ActionData' object directly to a ScoringActionData or AggregationActionData object.  Can that be done?  I hope this is clear enough.  
0
 
LVL 16

Accepted Solution

by:
imladris earned 250 total points
ID: 17003892
I don't know the overall structure and requirements of what is going on.

If it's possible, the nicest way to deal with this would be to have the execute method be part of the ActionData or ActionDataImpl class. Any derived class could (or would have to if you made it abstract) override the execute method to provide functionality appropriate for its data.

Then the branches are simply replaced with:

data.execute();

0
 

Author Comment

by:jbcarrico
ID: 17003939
Thanks to both of you, mukunda_expert and imladris, for the great answers.  I did get the answer I was looking for, and I appreciate all of the effort you put into answering my question.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question