[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Replacement for using instanceOf operator

Posted on 2006-06-28
5
Medium Priority
?
673 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 1000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month17 days, 15 hours left to enroll

831 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