[jdom-interest] Inheritance for the people!

Lasse Lindgård lasse at lintor.dk
Tue Jan 9 09:09:00 PST 2001


I have been using JDom since september now. And I am really happy about it.

However there is one thing that I have been missing for a long time: a
common JDOM element. The JDOMElement should be the abstract superclass for
Element, Attribute and CDATA.
 - An interface might also do the job, if that is prefered.

I often find myself writing methods like:

Some superclass contains a templatemethod like this:

Element toXML() {
	return new Element(getName()).addContent(getXMLvalue());
}
abstract Element getXMLvalue();

My subclass might choose to implement them differently:
1)
Element getXMLvalue() {
	return new Element("value").setText(getValue);
}

2)
Element getXMLvalue() {
	return new Element("value").addContent(new CDATA(getValue));
}

As you can see the new Element("value") is duplicate code in this example.
That annoys me a lot, since it is not only once or twice I have code like
that but loads and loads of times.

I'd rather do something like this:
Some superclass contains a template method like this:
Element toXML() {
	return new Element(getName()).addContent(new
Element("value").addContent(getXMLvalue()));
}
abstract Element getXMLvalue();

My subclass might choose to implement them differently:
1)
JDOMElement getXMLvalue() {
	return getValue;
}

2)
JDOMElement getXMLvalue() {
	return new CDATA(getValue));
}

3)
JDOMElement getXMLvalue() {
	return new Element("value1").addContent(getValue));
}


But alas that is only possible if there existed a common superclass for JDOM
elements.

Regards
Lasse Lindgård
Lintor ApS




More information about the jdom-interest mailing list