W3C home > Mailing lists > Public > www-dom@w3.org > January to March 2001

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

From: Peter Meyer <petermeyer99@hotmail.com>
Date: Wed, 21 Mar 2001 01:26:55 -0000
To: james.melton@cylogix.com, www-dom@w3.org
Message-ID: <F206nGiAw8HQEUEiffC0000065f@hotmail.com>
see below...

>It seems that the Visitor pattern makes it "easy" to vary operations
>performed on a set of types at the expense of making it "difficult" to
>add new types to the set. If you want to extend the Node interface to
>many subtypes, isn't Visitor the opposite of what you need?

If your large set of classes changes all the time, you are right - visitors 
then can create a lot of work in just keeping up with the changes.

>Another possibility is that you intend to create a large (but stable)
>set of Node subtypes and then flexibly write operations against the
>extended hierarchy.

This would likely be the case in my use case

>This could be a good way to use the Visitor pattern,
>but the Visitor interface includes methods like:
>VisitConcreteElementA(ConcreteElementA);  which requires the Visitor
>interface to know all ConcreteSubtypes on which it can act when it is
>written. If that interface is included in the DOM, doesn't it preclude
>client code adding new Node subtypes that can be "Visited"?

Not quite... First of all, you do not need to call the methods in the 
visitor interface visitConcreteElementA(), visitConcreteElementB(), etc. You 
can just call them visit() and rely on the language function overloading 
mechanism to call the right one. Second, your base interface only specifies 
a visit method for the common base class of your node hierarchy:
interface IVisitor {
    void visit(Node n);
and have all implementing visitors add the additional methods they need. In 
fact, you can even do it more general (the beauty of visitors is that they 
don't even require the hierarchies they are applied to to inherit from a 
common base class) and add a visit function that takes a parameter of type 
Object, where you might put up an error handler that tells you while you are 
debugging that you probably forgot to add an appropriate handler for the 
subclass of Object you are dealing with :-)

Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.
Received on Tuesday, 20 March 2001 20:27:31 UTC

This archive was generated by hypermail 2.3.1 : Tuesday, 20 October 2015 10:46:08 UTC