Design Patterns Java™ Workbook

(Michael S) #1

Chapter 29. Visitor........................................................................................................................................


Chapter 29. Visitor


The ordinary way to extend a class hierarchy's behavior is to add methods that provide the
behavior you need. It may happen, though, that the behavior you need is not consistent with
the thrust of the existing object model. In other cases, the developer of a hierarchy may have
little information about the behaviors that later developers will need.


If the developers who control the hierarchy code can't or won't change quickly enough to meet
your needs, it may be impossible to extend the hierarchy's behavior without modifying the
hierarchy's classes. But VISITOR lets a hierarchy developer build in support for the prospect
that another developer may want to extend the behavior of the hierarchy. The intent of
VISITOR is to let you define a new operation for a hierarchy without changing the hierarchy
classes.


Supporting Visitor.......................................................................................................................................


Chapter 5, Composite, introduced the MachineComponent hierarchy and suggested several
possible behaviors for this composite structure. Suppose that the developers at Oozinoz have
built some of these behaviors into the hierarchy and have also applied VISITOR to allow for
later extensions. Figure 29.1 shows the current state of the hierarchy.


Figure 29.1. The MachineComponent hierarchy designers planned for extension by
building in support for visitors.
Free download pdf