Re: Type-safe iteration over the DOM in DOM 2 & 3?

>I am sure it has been considered by the W3C
>to add the ability to use a visitor pattern

Yes, we did consider the Visitor pattern. It didn't seem to fit the use
cases we were considering at the time.

The Gang Of Four book says one of the indications of when to use visitor is
that you have many different interfaces intermixed and want to perform
operations that depend on the conrete classes. In our case, we have a
shared interface, Node, from which the others are subclassed, and the
nodetypes are clearly self-identifying, so this advantage is largely
negated.

Outside of coding style, there really isn't a lot of difference between
"traverse, switch, and call appropriate subroutine method" versus "visit,
accept(), and call back to appropriate subroutine method". Performance is
likely to actually be better with the switch, especially if the particular
DOM implementation is using a single class for multiple nodetypes and
switching internally to decide which behavior to apply.

Note that you could easily implement an ObjectStructure mechanism on top of
the current Traversal objects which accepted Visitor objects and dispatched
to them, with results essentially indistinguishable (for a basic DOM) from
those of implementing ConcreteElement and accept() on the nodes.

The only real difference would arise if you wanted to change the
dispatching. In the Visitor mechanism you would override that by
subclassing the nodes and changing where accept() calls back to -- which
actually is a significant DISADVANTAGE in that it risks breaking other
visitors to the same data structure. In the Traverse-and-call-back
alternative, the dispatching is encapsulated in that interface object --
and hence you can subclass that and create an extended version without
adverse affect on those operations which want to use the basic behavior.

If you've got a specific use case that the Traverse/callback approach would
not address, or if you can show that my concerns about fragility of Visitor
as behaviors are extended are unfounded,  I'm certainly willing to
reconsider this. I can see the aesthetic attraction of the Visitor pattern.
But I really think it's the wrong level of abstraction given the DOM's
design and the realities of how the DOM is being used

[DOM WG: Do we need a FAQ on this topic?]

______________________________________
Joe Kesselman  / IBM Research

Received on Tuesday, 20 March 2001 10:25:23 UTC