// Create a new Element
Element element = new Element("guitar");
Things remain equally simple with a custom subclass:
// Create a new Element, typed as an ORAElement
Element oraElement = new ORAElement("guitar");
The element is dropped into the O'Reilly namespace because of
the custom subclass. Additionally, this method is more
self-documenting than using a factory. It's clear at any point
exactly what classes are being used to create objects. Compare that
to this code fragment:
// Create an element: what type is created?
Element someElement = doc.createElement("guitar");
It's not clear if the object created is an
Element instance, an ORAElement
instance, or something else entirely. For these reasons, the custom
class approach serves JDOM well. For object creation, you can simply
instantiate your custom subclass directly. However, the need for
factories arises when you are building a document:
// Build from an input source
SAXBuilder builder = new SAXBuilder( );
Document doc = builder.build(someInputStream);