[jdom-interest] Need of GOF Visitor Pattern implemantation in JDOM?

Tobias Rademacher toby-wan-kenobi at gmx.de
Fri May 11 14:06:53 PDT 2001


Hi Alex,

> But, now that you mention it, I still haven't figured out what the benefit
> of a
> visitor pattern is over what to me is the more straightforward way:
> 
> void traverse(Document d)
> {
> 	traverse(d.getMixedContent());
> }
> 
> void traverse(List content)
> {
> 	Iterator iter = content.iterator();
> 	while(iter.hasNext())
> 	{
> 		Object node = iter.next();
> 		doSomethingWith(node);
> 		if (node instanceof Element)
> 			traverse(((Element)node).getMixedContent());
> 	}
> }

I would do it like this.

Imagine org.jdom.Document

     public aceept(Visitor aVisitor)  {

        // ensurse call of adapter
        aVisitor.vist(this); 

       // visit attributs
       Iterator attributeIterator = this.getAttributes.iterator();
       while (attributeIterator.hasNext()) {

             ((Attribute)attributeIterator.next()).accept(aVisitor);

       }

       // vist elements
       // same as above applied to elements of document

     }

/* applied  */


Element target;
this.pattern = aPattern;

Visitor aVistor = new JDOMVistor() {
 
  public void visit(Element anElement) {
       if (anElement.getName().equals(pattern)
              target.element = anElement;
  }

};


> 
> To me, this "visitor" pattern is too roundabout, it has too many
> interacting
> parts.

Sounds my sample no better for you? The client programmer code is quite
simple and short.  OK XPath is much better. But If you can also retrial an
instance, its name, this childs or whatever you want. It depends an the client
programmer.

Regards

Toby



-- 
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net




More information about the jdom-interest mailing list