Developer

Solve basic design issues using Marketplace

The Marketplace design pattern solves some basic design issues for usability in XML documents. Here's how.

This article originally appeared as an XML e-newsletter.

By Brian Schaffner

When designing XML solutions, the most obvious answer isn't always the most correct answer. One instance where this is true is the case of the Marketplace design pattern. This pattern solves some basic design issues for usability in XML documents.

The basic problem

The Marketplace pattern helps solve a basic problem with XML documents. Imagine you have a document with a hierarchy of elements, such as a family tree. In Listing A, we see a family tree that contains two Parent elements and one Child element.

The problem is that, using this hierarchy, it's difficult to place the Child element in the structure using a simple parent-child hierarchy. That's because a human child (unlike an XML child) is required to have two parents rather than just one.

Listing A: marketplace_problem.xml
<?xml version="1.0" ?>
<Family>
  <Parent>
    <Name>Dad</Name>
  </Parent>
  <Parent>
    <Name>Mom</Name>
  </Parent>
  <Child>
    <Name>John</Name>
  </Child>
</Family>

This type of problem exists any time you have a set or series of elements that can be classified in a hierarchy in multiple ways.

The Marketplace pattern

The essence of this design pattern is that it allows you to regroup and reorganize your data hierarchy to solve this problem. In the above example, you may be tempted to place the Child element under one of the Parent elements; however, you'll quickly realize the difficulty in processing and understanding this type of data. Placing the Child under the Dad element removes the relationship to the Mom element and vice-versa.

Instead, we'll implement the Marketplace pattern. This solution uses the XML feature called attributes to remove the inadequate hierarchy structure, yet it still retains the important relationship data. Rather than place the Child element under either the Mom or Dad element, you leave it where it is and add additional attributes to indicate the structure.

The solution

In our example, we want the data to communicate that there is a family that consists of a mom, a dad, and a child named John. We also want to show explicitly that John is the child of Mom and Dad—a relationship that is only implied in Listing A.

In order to explicitly show this relationship, we won't make the Child element a child of either Parent element. Instead, we'll leave it where it is, but add some attributes to describe the structure. This approach is shown in Listing B.

Listing B: marketplace_solution.xml
<?xml version="1.0" ?>
<Family>
  <Parent>
    <Name>Dad</Name>
  </Parent>
  <Parent>
    <Name>Mom</Name>
  </Parent>
  <Child mom="Mom" dad="Dad">
    <Name>John</Name>
  </Child>
</Family>

In this example, you can clearly see that John has a mom called Mom and a dad called Dad.

There are multiple approaches to creating an explicit solution. Instead of identifying the structure by name, you could use identifier tags. Also, instead of creating explicitly structured attributes, you could create a more generic attribute and use a list. You can see a combination of these approaches in Listing C.

Listing C: marketplace_solution2.xml
<?xml version="1.0" ?>
<Family>
  <Parent id="1">
    <Name>Dad</Name>
  </Parent>
  <Parent id="2">
    <Name>Mom</Name>
  </Parent>
  <Child parents="1,2">
    <Name>John</Name>
  </Child>
</Family>

This approach allows you to create more sophisticated data structures. For instance, in today's world, a child may have two fathers or two mothers rather than one of each; the solution in Listing C allows you to easily model such a family.

Summary

When designing XML documents, you often encounter data structures that don't fit easily into an XML model. The Marketplace pattern is used to solve some of these seemingly simple XML data structure problems.

By using the Marketplace pattern, you can create more complex XML documents that use multiple hierarchies.

Brian Schaffner is an associate director for Fujitsu Consulting. He provides architecture, design, and development support for Fujitsu's Technology Consulting practice.

Editor's Picks