Solved

Replacement for using instanceOf operator

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Printing to a specific printer tray (HP 806dn printer) 3 83
maven example error 3 53
eclipse shortcuts 9 45
how to install java on RHEL image on EC2 4 7
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now