[jdom-interest] How to ease traversal of JDOM tree

Frank Sauer Frank.Sauer at trcinc.com
Tue Nov 20 09:36:12 PST 2001


I have to disagree. The Visitor pattern provides 
for that problem in having a separate method for
each concrete type and use double dispatching to
call the right one. See Visitor in GOF book.
As an alternative, you may want to check out aspect
oriented programming and in aprticular aspectJ at
http://www.aspectj.org . That can be used to implement 
this kind of stuff much more elegantly without polluting the 
JDOM code base directly.

Frank Sauer
The Technical Resource Connection, Inc.
a wholly owned subsidiary of Perot Systems
Tampa, FL
http://www.trcinc.com
-----------------------------------------------
Java: The best argument for Smalltalk since C++


-----Original Message-----
From: Laurent Bihanic [mailto:laurent.bihanic at atosorigin.com]
Sent: Tuesday, November 20, 2001 11:43 AM
To: Bradley S. Huffman
Cc: jdom-interest at jdom.org
Subject: Re: [jdom-interest] How to ease traversal of JDOM tree


Bradley S. Huffman wrote:

>>>Why not a Visitor class (see the TODO list)?
>>>
>>Yes but visited objects are supposed to accept the Visitor. Gamma & al. 
>>actually propose to define an interface (thay call "Node" !) that defines
the
>>accept() method.
> 
> Which is a modeling construct to convey that each object in the sturcture
> needs a method Accept( Vistor).  That doesn't necessarily translate to
> actual code.  As along as each class implements a Accept method and the
> Visitor method has a corresponding method for each class, we're okay.
> 

No, if you do not have an interface, you'll have to do something like:

public void visit(Object o)
{
    if (o instanceof Element)   { ((Element)o).visit(this); }
    if (o instanceof Attribute) { ((Attribute)o).visit(this); }
    if (o instanceof Document)  { ((Document)o).visit(this); }
    if (o instanceof Comment)   { ((Comment)o).visit(this); }
    ...
}

Whereas, with an interface:

public void visit(Node o) { o.visit(this); }


Laurent

_______________________________________________
To control your jdom-interest membership:
http://lists.denveronline.net/mailman/options/jdom-interest/youraddr@yourhos
t.com



More information about the jdom-interest mailing list