: Computer equipment containment hierarchy and example object
A composite is a structural pattern that represents a whole-part hierarchy . For example, the class structure in Figure 8 describes a containment hierarchy for a computer consisting of equipment, which may contain other equipment as parts. An Iterator is used to allow the elements of a composite or aggregate structure to be accessed. Iterators at each level in the composite hierarchy could be written to traverse a computer object, performing some task during the traversal. Existing languages require traversals to be hard-coded, with specific reference relations between classes used for traversal. A modification to the class design would require maintenance of the traversal-oriented code. An adaptive traversal specification may be used to define paths within class structures, without hard-coding details of the actual class structure . An adaptive traversal specification supplies the minimal information required for abstracting a path from a class structure.
Figure 9: Traversal expression
Traversing the parts of a computer requires navigation along paths that lead to pieces of equipment. Thus the target of traversal is the equipment class. Figure 9 shows example code to accomplish this task, using a traversal expression style proposed by Lopes and Lieberherr . The keyword traverse is used along with a path description, for example, indicating paths that lead to equipment objects. The keywords to, via and bypassing can be used to constrain paths through a class structure.
Figure 10: Equivalent C++ code
Executing a traversal expression triggers the navigation of the method's host object. The simple traversal behavior will be composed with task-specific actions to perform as objects are encountered. The basic traversal algorithm consists of the following three steps: (1) an action to perform upon encountering an object, (2) traversal of the object, and (3) an action to perform after object traversal. The variable part of the algorithm is the action surrounding the object traversal. Figure 10 contains example C++ code conceptually equivalent to the traversal expression in Figure 9, assuming the class structure from Figure 8. The code contains template methods, or a skeleton, for defining the traversal algorithm. The variable part of the algorithm is the actual before and after behavior. The traverse method should be executed within some context that defines implementations of the before and after methods. We assume empty default before and after method implementations for each class, and that the methods are class-stored methods rather than instance-stored methods. One or more contexts will be used to override the default empty behavior, providing task-specific implementations.