Solved

Replacement for using instanceOf operator

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
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.

743 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

12 Experts available now in Live!

Get 1:1 Help Now