Tuesday, January 27, 2009

A Universal XML Software Pattern, Part II


The Composite pattern in object-oriented design is a natural fit for implementing an XML document into the variables of an application program. According to the “gang of four” canonical definition of design patterns, the Composite pattern is used to “Compose objects into tree structures to represent part-whole hierarchies.” (1) This pattern does for the variables of an application program exactly what XML does for the data in a document – it structures it into a hierarchical tree wherein the children nodes of the tree are components or parts of their parent node.

The above diagram shows two application programs importing the data from an XML document and storing it internally as a Composite pattern tree. The program on the left has a structure of internal variables that is a direct reflection of an XML document. The program on the right is presenting the same XML document in a graphical user interface using a graphical tree (in a manner similar to the way the file system of a computer is displayed graphically to the user in the Windows Explorer).

The fit of the Composite pattern to the elements of an XML document are apparent and intuitive at first glance. Each element of an XML document is a whole that can be decomposed into parts (until we reach the most fundamental elements which we refer to as the “leaves of our tree”). This is exactly how the Composite pattern works. Each node in the Composite tree includes a set of pointers to its children (again excepting the lowest level “leaves” of the Composite tree). In the Composite pattern, each node in the tree can be treated as a whole that is made up of its children parts and this is exactly how the elements of an XML document work. Put simply, there is a one-to-one isomorphic correspondence between the elements of an XML document and the nodes of a tree in the program’s Composite pattern.

Our Universal XML Pattern starts with the storing of the XML elements with a Composite pattern. This is the easy and intuitive part. The greater challenge will be in bridging the gap from an incoming stream of XML data to a fully structured Composite tree within the application program and doing so in a manner that works for all XML documents.

The following posts to this blog will show how a short recursive algorithm can read through the elements of any XML document and generate a tree-like structure internal to an application program. Using simple variations of the Factory Method and Adapter patterns, we can enable any application program to universally convert any XML document into a Composite pattern of program variables.

(1) Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Reading, Massachusetts: Addison-Wesley Publishing Company: 1995) p. 163.

No comments: