[jdom-interest] API Inertia
bob mcwhirter
bob at werken.com
Tue May 1 09:20:13 PDT 2001
> Elliotte Rusty Harold wrote:
> >
> > I think it would help because the Node interface would have
> > hasChildren() and getChildren() methods. These would be applicable to
> > all nodes.
>
> All nodes except: Attribute, DocType, Entity, CDATA.
>
> Or are you saying that hasChildren() will just return false for nodes
> which could *never* have children. That's not very appealing, to have a
> getChildren() method on an object which can't have children.
Just for reference, this is a well-known problem, as we're basically
playing with the Composite Pattern (see the GoF book).
No matter how you slice it, you either
a) cast things, and use instanceof to determine
what the thing really is
b) have extraneous methods on the super interface
which have no meaning to leaf nodes.
> > This lets you
> > walk the tree without caring excessively about the type of each node
> > in the tree or without a lot of casting.
>
> No casting so long as you don't really want to *do* anything with a
> node. :-)
If we really want to walk the tree in a convenient fashion, without
wacky casting, I might suggest implementing the Visitor pattern
(once again, see the GoF book, or the many varieties in the PLoP
books).
-bob
More information about the jdom-interest
mailing list