Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:
JavaWorld Daily Brew

The Code is nicely placed, i really appreciate the way u thought but i have a doubt,by using reflection does it become a perfo

Back to:
www.javaworld.com/javaworld/javatips/jw-javatip98.html
.

The Code is nicely placed, i really appreciate
the way u thought but i have a doubt,by using reflection does it become a performance killer?

Your rating: None Average: 1 (1 vote)

Why bother with visitString, visitFloat etc. when you can just u

Why bother with differently named methods like visitString, visitFloat etc. when you can just use method overloading e.g. visit(Float) and visit(String) and let the JVM select the right one ?

Recursion is wrong

Also, the recursion in :

public void accept(Visitor visitor) {
visitor.visitTreeNode(this);
visitor.visitTreeNode(leftsubtree);
visitor.visitTreeNode(rightsubtree);
}
is wrong. If visitor.visitTreeNode does no navigation of the structure then this should be more like:
public void accept(Visitor visitor) {
visitor.visitTreeNode(this);
leftsubtree.accept(visitor);
rightsubtree.accept(visitor);
}
If it does do navigation it's still wrong, the left and right subtreest will be navigated twice.

I expect it will hit performance pretty hard...

I expect it will hit performance pretty hard. Look at the performance numbers these folks get with a similar pattern... http://www.cs.ucla.edu/~palsberg/paper/compsac98.pdf

RE: Why bother with visitString, visitFloat etc. when you can ju

You can use visit(Float) and visit(String) but anyway you have to cast the parameter on the caller.

Hul Hul Ra knows this

I'll inform it to Hul Hul Ra.

An additional conclusion...

...is that with its single inheritance java simply couldn't exist without reflection.

hate this pattern

I hate this pattern more than liking it

What's the gain in using dynamic dispatch over single dispatch

What's the difference in the two different uses?
Printer printer = ...
Report report = ...
printer.print(report); //1
report.accept(printer); //2

Type safety with reflection

Nice article. You should mention that with the relection approach you give up compile-time type safety... this can be costly in terms of maintenance and testing overheads.

Commons-visitor

A commons library implementing these ideas has been open sourced at
http://commons-visitor.sourceforge.net

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <p> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <br /> <br> <strike>
  • Lines and paragraphs break automatically.
  • Use <!--pagebreak--> to create page breaks.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
Just checking to see if you're an actual person rather than a spammer. Sorry for the inconvenience.