GoF Visitor Pattern

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Componentized Visitor Pattern

PerfectJPattern's componentized version of the Visitor Pattern differs from the original GoF version in that:

  • Adding new visit operations is still easy but adding new Concrete Elements is also easy because PerfectJPattern's does not require adding a new abstract operation on the IVisitor<E> type for each new Concrete Element type. The AbstractVisitor<E>.visit(<E>) method or its static variation reusableVisit handle all Element subtypes via double-dispatch.
  • Provides non-intrusive implementation that does not impose any requirements on the Elements hierarchy i.e. the GoF implementation requires the Element hierarchy to provide a repetitive and error-prone accept(...) method.
  • Context-free ready to use AbstractVisitor<E> implementation that provides the double-dispatch mechanism implemented on top of PerfectJPattern's Delegates. Adding new Concrete Visitor is as simple as extending AbstractVisitor<E> (or otherwise reusing the static reusableVisit implementation) and implementing the relevant visitXXX(...) methods for each Element subtype of interest. Concrete Visitors' visit method names may be chosen freely, though a good convention would be to use visitXXX(...) but it is not compulsory.

UML Class Design



Example