[jdom-interest] (no subject)

Jason Hunter jhunter at acm.org
Fri May 30 18:14:16 PDT 2003


> What really is the point of Parent and Child interfaces and what is a
> use case of using them? 

It makes generic traversal easier.

> How have JDOM API benefited from having them vs
> not having them at all like in B9? Can you show examples of use cases
> that are now easier to do than before?
> 
> As far as I can tell, it only complicated some regular use cases
> without simplifying any or providing any more genarility. Jason even
> mentioned puting back getParentElement to solve some of these issues.

This cracks me up.  For two years people have wanted some generalization 
from interfaces, and as soon as they're added some other people want 
them out.  Some things just don't always have a "right" solution and you 
go one way or the other.  Brad's Parent/Child interfaces were the first 
time I saw something I liked more than not having interfaces.

> My understanding was that there was an argument for generic "Node"
> interface that's mentioned in TODO. 

There's been an argument for a generic Node since the beginning.  Of 
course, everyone's idea of what was a Node was different!!  XML is NOT a 
generic tree no matter how nice that would be.  It turns out most Java 
models (JDOM, dom4j, XOM) have decided to use the notion of parent and 
child interfaces although the names aren't always parent/child.

> Somehow that morphed into these
> Parent/Child interfaces, which IMO don't add any value to API as they
> stand right now. Having a single Node interface would for example
> allow some apis that work on any JDOM object be more type safe by not
> having to pass Object. 

They won't be any more type safe!  You could pass "MyBogusThing 
implements Node" and screw up the tree.  We have to check at runtime 
what you add regardless.  You don't get any better type safety, and it's 
not like people have real trouble passing in the wrong objects.  There's 
no methods for Node that are common among Parent and Child, so you might 
as well use Object as the generic holder.

> Right, but not as its returned by any of these functions. So I
> wind up using these other methods that return sepcific thigns or
> casting. What use are these interfaces if I always cast away?

Except for getParent() there's no place where you have to do a new cast 
that I know of.

>>null, so all your doing is trading
>>
>>   Child child = element.getChild("foo");
>>   if (child instanceof Element) {
>>       Element foo = (Element) child;
>>       foo.getChild("whatever");
>>   }

That's not legal code.  You call getChildElement("foo") and it returns 
an Element.

> I gained absolutely NOTHING in the above example from having a Child
> interface.

Nope, but you didn't lose anything either.  You just renamed getChild() 
to getChildElement() which is more accurate anyway since getChildren() 
really shouldn't return only elements since elements aren't the only 
"children".

-jh-





More information about the jdom-interest mailing list