Re: Visitor pattern vs if-ladder
On Thu, 23 Apr 2009, Giovanni Azua wrote:
"Tom Anderson" <twic@urchin.earth.li> wrote
You can't. You have to write the accept method (with exactly the same text
each time) in each subclass. This is one of the annoying things about the
Visitor pattern.
Not only annoying but also error-prone
How so?
and pollutes the Element Model hierarchy with unrelated concerns.
Not so. Exactly one short method has to be added, and that method is
fundamentally about how the hierarchy interacts, in a generic way, with
other classes, which is very much a concern of the hierarchy itself.
But it does not have to be that way :] e.g. there is no need for accept
methods in the implementation below. The elements are visit(ed) directly
without accept roundtrips in order to achieve the desired
double-dispatch:
http://perfectjpattern.sourceforge.net/xref/org/perfectjpattern/core/behavioral/visitor/Example.html
http://perfectjpattern.sourceforge.net/xref/org/perfectjpattern/core/behavioral/visitor/PrintVisitor.html
You left out the class which actually does the work:
http://perfectjpattern.sourceforge.net/xref/org/perfectjpattern/core/behavioral/visitor/AbstractVisitor.html
And which is based on a pretty shocking reflective strategy which gives
absolutely no type safety, and a lot of overhead (doubly so, because it's
a bad idea, badly implemented). How on earth could you describe Visitor as
'error-prone' and then offer this as an alternative?
tom
--
Only the bagel has the correct aspect ratio.