Recently I was studying about parsers including the design patterns used to built one. The example I have chosen is javax.xml.parsers and org.w3c.dom packages.
Looks like factory and builder patterns used to design the parser structure in these packages. DocumentBuilderFactory is to return an immediate factory to build DocumentBuilder. DocumentBuilder, then, uses its parse() method to parse an xml file; but the return type is Document in this case: Document doc = builder.parse(in);
But, what I didn’t get here is Document is an interface which contains plenty of methods to manipulate XML attributes. It also extends Node interface. We can still call its operations: doc.hasAttributes() or doc.getChildNodes() etc.
I spent an hour on this, but still couldn’t get the logic behind this architecture:
1) Where are those Document’s methods implemented?
2) Why is it better to return interface type object (Document) to represent the DOM object after parsing?
Document,Node,Elementand all of the other types are interfaces. There are a couple of libraries around that provide an implementation for those interfaces – one prominent example is Apache Xerces. From their front page:If you exactly need to know which implementation of DOM is actually used, start a debugger or dump the class name of your
Documentobject to the console/log.