Submitted by Anonymous on Wed, 02/25/2009 - 10:50.
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 ?
Submitted by Anonymous on Thu, 02/26/2009 - 05:12.
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.
Submitted by Anonymous on Wed, 11/04/2009 - 20:48.
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.
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
Post new comment