Like most other self-respecting developers I had also read the GoF book,
including the section on the visitor pattern. However, when a colleague came
over to me with a question, I could not initially justify the complexity of
the example code I saw in the book. What follows is a discussion of why the
visitor pattern is the way it is.
Brief Review of the Pattern
The definitive description of the pattern is in the GoF book Design Patterns,
Chapter 5 (pp 331-344)(see References section). The Wikipedia has a concise
and good description, which formed the basis for my brief review here. The
visitor pattern is classified as a Behavioral pattern, so the thing to notice
is the way in which the classes and objects interact and distribute
responsibility. A typical application of this pattern occurs in the following
scenario: we have a number of elements in an object structure ... (more)