Marketplace
Abstract
Instead of organizing objects in a hierarchical fashion, objects are organized in a linear way, with signs on each object to indicate its classifications.
Problem
A natural way to use mark up to classify objects is to use a container for each new type in the classification.
<Employees>
<Managers>
<Person type="salaried">
Riff Raff
</Person>
<Person type="contractor">
Frank Furter
</Person>
</Managers>
<Workers>
<Person type="salaried">
Brad Majors
</Person>
<Person type="contractor">
Janet Weiss
</Person>
</Workers>
</Employees>
This can lead to problems, if processing software needs to find all of the employees, or just the salaried employees, it needs to iterate through two different lists, the managers and the workers.
Context
Any place where data can be organized by more than one type.
Forces
Ease-of-processing can be greatly affected by the way that elements are organized. Ease-of-Authoring can also be a problem, pick one structural organization can lead to awkward groupings of elements, and the choice of structure may seem arbitrary.
Solution
The objects to be modeled can all be grouped at the same level, and the categorization can be done with attributes on the element. This allows multiple characteristics of the objects to be modeled without enforcing a possibly artificial hiearchy on the objects.
Examples
The following model can be contrasted to the sample in the problem section, which uses a tree structure to model employees. Each employee can have two different types, whether they are Salaried or Contractors and whether they are Workers or Managers. In the tree structure, one of the types, the Worker/Manager distinction is given more importance than the Salaried/Contractor. Extra effort would be required to extract just the Salaried employees.
The sample below uses the Marketplace pattern to organize the same data into a flatter structure. In this organization both types are given equal precedence, and both types are equally easy to process.
<Employees>
<Person type="salaried" level="manager">
Riff Raff
</Person>
<Person type="contractor" level="manager">
Frank Furter
</Person>
<Person type="salaried" level="worker">
Brad
</Person>
<Person type="contractor" level="worker">
Janet
</Person>
</Employees>
Discussion
This pattern results in a organization that can make mapping to database tables easier. The elements in the marketplace are equivalent to rows in a table, while the attributes are the columns. A more hierarchical structure can be harder to map to databases.
Related Patterns
The Marketplace is often contained in a
Collection Element.
Known Uses
References
See:The Microsoft whitepaper
Serializing Graphs of Data in XML (http://www.infoloom.com/gcaconfs/WEB/TOC/t0302_.HTM) for a discussion of representing graph structure such as RDBMS tables in XML.