next up previous
Next: CLASS DIAGRAMS Up: Preventive Program Maintenance in Previous: TRAVERSALS

VISITORS

The purpose of a visitor is to augment the behavior of traversals. Several visitor objects may be attached to the same traversal. When the traversal reaches a certain object, the visitors will do their work. Visitor objects are like ordinary objects and contain visitor methods which express what needs to be done when certain objects are reached.

Visitors are often composed of subvisitors. For example, if we want to compute the average salary paid by a company, we would define a SummingVisitor which adds together all salaries and a CountingVisitor which counts the number of Salary-objects encountered during the traversal. An AverageVisitor would contain both the SummingVisitor and the CountingVisitor to compute the average. All three visitors are attached to the same traversal to accomplish their task simultaneously.

Since visitor objects are just ordinary objects, they gain all the usual benefits of object-oriented programming: encapsulation, inheritance, and polymorphism. This leads to greater re-use of the behavior embodied by each visitor object. Also, they themselves may be the subject of traversals, so that one may build up large collections of cooperating visitors and use AP to succinctly manage the data shared between them.



Karl Lieberherr
Fri Nov 15 14:30:41 EST 1996