Design Patterns Java™ Workbook

(Michael S) #1
Chapter 29. Visitor

CHALLENGE 29.5


List two alternatives to building VISITORinto the Oozinoz machine and process
hierarchies.

Summary......................................................................................................................................................


The VISITOR pattern lets you define a new operation for a hierarchy without changing the
hierarchy classes. The mechanics for VISITOR include defining an interface for visitors and
adding accept() methods in the hierarchy that a visitor will call. The accept() methods
dispatch their calls back to the visitor in a double-dispatching scheme. This scheme arranges
for the execution of a visit() method that applies to the specific type of object from the
hierarchy.


A visitor developer must be aware of some, if not all, of the subtleties in the design of the
visited hierarchy. In particular, visitors need to beware of cycles that may occur in the visited
object model. This type of difficulty leads some developers to eschew VISITOR, regularly
applying alternatives instead. Using VISITOR should probably be a team decision that depends
on your methodology and the specifics of your application.

Free download pdf