Context:

A recursive object structure (e.g. file directory tree) has to be traversed from the outside (e.g. view traversing its document), applying routine functionality (e.g. formatting for display).

Challenges:

  • To traverse the structure node by node, ignoring node type (possibly applying operations that affect the integrity of the structure).
  • To copy or move nodes, ignoring Node type (counting on automatic validation) .

Skill:

Participants:

Component:

The capability to traverse a subtree. Optionally, the ability to match node types.

Particular Leaf:

Silently ignores the traversal protocol. (But does other useful jobs).

Composite:

Manages a heterogeneous collection of Components. Implements the default traversal protocol.

Particular Composite:

Implements particular collection management protocol. Optionally, re-implements the traversal protocol.

Signature:

Both particular Leaves and default Composite are Concrete Components. Composite contains (or references) Components. The Particular Composite may choose to limit the scope of the inherited association (see rectangular inheritance of association).  

Used patterns and idioms:

Recursive Object-Structure idiom, Rectangular Inheritance of Association idiom, unified interface.

Used by:

Document/View architectures (document side), generic presentation and report systems, arithmetic expressions, parse trees, rule bases.

Known issues:

See Rectangular Inheritance of Association idiom.

Scope:

General.