Visitor pattern

related topics
{math, number, function}
{car, race, vehicle}
{water, park, boat}
{school, student, university}
{system, computer, user}
{@card@, make, design}

In object-oriented programming and software engineering, the visitor design pattern is a way of separating an algorithm from an object structure it operates on. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. It is one way to easily follow the open/closed principle.

In essence, the visitor allows one to add new virtual functions to a family of classes without modifying the classes themselves; instead, one creates a visitor class that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.

While powerful, the visitor pattern is more limited than conventional virtual functions. It is not possible to create visitors for objects without adding a small callback method inside each class. In naive implementations, the callback method in each of the classes is not inheritable.

Contents

Details

A user object receives a pointer to another object which implements an algorithm. The first is designated 'element class' and the latter 'the visitor class'. The idea is to use a structure of element classes, each of which has an accept() method taking a visitor object for an argument. visitor is an interface having a visit() method for each element class. The accept() method of an element class calls back the visit() method for its class. Separate concrete visitor classes can then be written to perform some particular operations, by implementing these operations in their respective visit() methods.

One of these visit() methods of a concrete visitor can be thought of as a method not of a single class, but rather a method of a pair of classes: the concrete visitor and the particular element class. Thus the visitor pattern simulates double dispatch in a conventional single-dispatch object-oriented language such as Java, Smalltalk, and C++. For an explanation of how double dispatch differs from function overloading, see Double dispatch is more than function overloading in the double dispatch article. In the Java language, two techniques have been documented that use reflection to simplify the mechanics of double dispatch simulation in the visitor pattern: getting rid of accept() methods (the Walkabout variation), and getting rid of extra visit() methods.

Full article ▸

related documents
Octonion
Embedding
Analytic continuation
Total order
Uniform convergence
Orthogonality
Burnside's problem
Partial derivative
Tangent space
Inequality
Analytic function
Symmetric matrix
Transcendental number
Sufficiency (statistics)
Root-finding algorithm
Tree automaton
Polymorphism in object-oriented programming
Shell sort
Planar graph
Binary heap
MathML
Brouwer fixed point theorem
Array
Additive category
Naive Bayes classifier
Hausdorff space
Binary tree
Spectral theorem
Euler–Maclaurin formula
Bubble sort