PerfectJPattern - Visitor Design Pattern (original) (raw)
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. TheAbstractVisitor<E>.visit(<E>)method or its static variationreusableVisithandle 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 extendingAbstractVisitor<E>(or otherwise reusing the staticreusableVisitimplementation) and implementing the relevantvisitXXX(...)methods for each Element subtype of interest. Concrete Visitors' visit method names may be chosen freely, though a good convention would be to usevisitXXX(...)but it is not compulsory.
UML Class Design

Example
- Example.java: Example startup main
- ICarPart.java: Visitable Element tree root
- Engine.java: Concrete Element type
- Body.java: Concrete Element type
- Wheel.java: Concrete Element type
- Car.java: Concrete Element type
- PrintVisitor.java: Concrete Visitor type
- DoVisitor.java: Concrete Visitor type