Solved

Replacement for using instanceOf operator

Posted on 2006-06-28
5
666 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 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