[jdom-interest] Interfaces vs. Concrete Base Classes

Anthony Eden aeden at signaturedomains.com
Mon Dec 11 08:05:33 PST 2000


I have been reading the debate regarding interfaces with great interest and
have decided to say something at this point (I am sure I will regret this
later).

For the record: I am a fan of interfaces and the flexibility they provide.
I like being able to plug in alternate implementations.  When appropriate I
use interfaces.

In this case however, I believe that interfaces may not be appropriate.
Interfaces would be fine when reading in XML documents.  It is very easy to
imagine a builder returning interfaces mapped to some concrete
implementation.  Unfortunately it becomes much more difficult when creating
XML documents.

Right now with JDOM a common application would be:

Element rootElement = new Element("root");
rootElement.addContent(new Element("foo"));
rootElement.addContent(new Element("bar").addContent(new Element("baz")));
..etc..
Document document = new Document(rootElement);

Now, if Element were an interface then creating elements would require
either a factory, which has been strongly opposed by many here
(arghh...we're creating DOM all over again) or you will have to replace
every instance of Element with a concrete class (i.e. Element element = new
JDOMElement()).  If you are creating documents with lots of elements this
creates a large number of code changes, which is not desirable.

I think concrete element classes are a by-product of the JDOM philosophy:
that creating new XML document within Java should be easy.  Therefore, I
believe that sticking with concrete base classes which can be extended is
the right thing to do at this point.

Sincerely,
Anthony Eden




More information about the jdom-interest mailing list