XMLPatterns.com

Flyweight

Abstract

If the same information is included at many different points in a document the information can be placed in just one place, and shared from each place in the document that needs to refer to it.

Problem

Placing the same information in many different place can cause several problems:

Context

This is a very general pattern, almost any document type can make use of it. This pattern can be used anywhere where the same information must be repeated more than once in a document. The repeated data can occur in the document declaration, or in the document instance itself.

Forces

This pattern can effect the length of the document, maintainability, and readability of the document.

Solution

Place the shared information in just one place, and make references to it from every place in the document that needs to include this information. There are several ways to do this: See the Examples section for an example of each of the above techniques.

Examples

XML Entities

This example shows how XML Entities can be used to put a piece of information in just one place, and then have it appear in multiple places in the document.
          

<!DOCTYPE Doc[
<!ENTITY TITLE "My Document">
]>
<Document>
<title>&TITLE;</title>
<H1>&TITLE;</H1>
This is my paragraph.
</Document>

          
        

XLink

This document shows the use of XLink attributes to include the contents of a document in two different places. Since XLink is a standard it is possible that existing tools could be used to do the substitution.
          

<Document>
<title>
<include xlink:show="embed"
xlink:href="titledoc.txt"/>
</title>
<h1>
<include xlink:show="embed"
xlink:href="titledoc.txt"/>
</h1>
This is my paragraph.
</Document>

          
        

ID and IDREF

This shows how ID and IDREF attributes can be used as a Flyweight. Note thats this would require some extra work on the part of the processing software to do the substitution.
          

<Document>
<text id="title">My Document</text>
<title IDREF="title"/>
<h1 IDREF="title"/>
</Document>

          
        

Discussion

This pattern can greatly enhance the maintainability of a document, if the same data is repeated several times, and that data changes, every occurrence of that data must be found and updated. This can be a tedious and error prone task. Having the data in only one place allows all occurrences of it to be changed at once.

The Flyweight pattern should not be used if the shared information can vary over time. This would increase the maintenance burden of the document.

Readability of the document can suffer if the Flyweight pattern is used, readers are forced to reference a different section of the document when looking at the contents.

Related Patterns

See Declare Before First Use for suggestions on where to place the shared information.

Known Uses

XHTML uses a a common attributes parameter entity that is a Flyweight.

The XML & SGML Cookbook page 1-126 mentions the Flyweight pattern.