- Visitor pattern
[
LePUS3 ( [http://lepus.org.uk/ref/legend/legend.xml legend] ) ]In
object-oriented programming andsoftware engineering , the visitor design pattern is a way of separating analgorithm from an object structure upon which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying those structures. Thus, using the visitor pattern helps conformance with theopen/closed principle .In essence, the visitor allows one to add new
virtual function s 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 throughdouble dispatch .While powerful, the visitor pattern does have limitations as compared with conventional
virtual functions . It is not possible to create visitors for objects without adding a small callback method inside each class and the callback method in each of the classes is not inheritable to the level of the new subclass.Elaborated
The idea is to use a structure of element classes, each of which has an
accept()
method that takes avisitor
object as an argument.Visitor
is an interface that has avisit()
method for each element class. Theaccept()
method of an element class calls back thevisit()
method for its class. Separate concretevisitor
classes can then be written that perform some particular operations, by implementing these operations in their respectivevisit()
methods.One of these
visit()
methods of a concretevisitor
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 simulatesdouble dispatch in a conventional single-dispatchobject-oriented language such as Java,Smalltalk , andC++ . For an explanation of how double dispatch differs fromfunction overloading , see Double dispatch is more than function overloading in the double dispatch article. In the Java language, two techniques have been documented which use reflection to simplify the mechanics of double dispatch simulation in the visitor pattern: [http://www.cs.ucla.edu/~palsberg/paper/compsac98.pdf getting rid of accept() methods] (the Walkabout variation), and [http://www.javaworld.com/javaworld/javatips/jw-javatip98.html getting rid of extra visit() methods] .The visitor pattern also specifies how iteration occurs over the object structure. In the simplest version, where each algorithm needs to iterate in the same way, the
accept()
method of a container element, in addition to calling back thevisit()
method of thevisitor
, also passes thevisitor
object to theaccept()
method of all its constituent child elements.Because the Visitor object has one principal function (manifested in a plurality of specialized methods) and that function is called
visit()
, the Visitor can be readily identified as a potentialfunction object orfunctor . Likewise, theaccept()
function can be identified as a function applicator, a mapper, which knows how to traverse a particular type of object and apply a function to its elements. Lisp's object system with its multiple dispatch does not replace the Visitor pattern, but merely provides a more concise implementation of it in which the pattern all but disappears.Example in Java
The following example is an example in the Java programming language:
Example in C++
The following example is an example in the C++ programming language:
tate
Aside from potentially improving
separation of concerns , the visitor pattern has an additional advantage over simply calling a polymorphic method: a visitor object can have state. This is extremely useful in many cases where the action performed on the object depends on previous such actions.An example of this is a pretty-printer in a
programming language implementation (such as acompiler or interpreter). Such a pretty-printer object (implemented as a visitor, in this example), will visit nodes in a data structure that represents a parsed and processed program. The pretty-printer will then generate a textual representation of the program tree. In order to make the representation human readable, the pretty-printer should properly indent program statements and expressions. The "current indentation level" can then be tracked by the visitor as its state, correctly applying encapsulation, whereas in a simple polymorphic method invocation, the indentation level would have to be exposed as a parameter and the caller would rely on the method implementation to use and propagate this parameter correctly.ee also
*Double and
multiple dispatch
*Composite pattern
*Hierarchical visitor pattern
*Strategy pattern
*Function object External links
* [http://objectmentor.com/resources/articles/visitor.pdf The Visitor Family of Design Patterns] by Robert C. Martin - a rough chapter from "
The Principles, Patterns, and Practices of Agile Software Development ",Robert C. Martin , Prentice Hall
* [http://www.lepus.org.uk/ref/companion/Visitor.xml Visitor pattern in UML and in LePUS3] (a Design Description Language)
*Article " [http://se.ethz.ch/~meyer/publications/computer/visitor.pdf Componentization: the Visitor Example] byBertrand Meyer and Karine Arnout, "Computer" (IEEE), vol. 39, no. 7, July 2006, pages 23-30.
*Article " [http://www.onjava.com/pub/a/onjava/2005/06/01/searchvisitor.html Domain Searching Using Visitors] " byPaul Mukherjee
*Article " [http://codeproject.com/cpp/ConditionInterpreter.asp Parsing Conditions using Interpreter and Visitor Pattern] "
*Article [http://www.cs.bham.ac.uk/~hxt/research/mfps-visitors.pdf A Type-theoretic Reconstruction of the Visitor Pattern]
*Article " [http://citeseer.ist.psu.edu/palsberg97essence.html The Essence of the Visitor Pattern] " byJens Palsberg andC. Barry Jay . 1997 IEEE-CS COMPSAC paper showing that accept() methods are unnecessary when reflection is available; introduces term 'Walkabout' for the technique.
*Article " [http://www.polyglotinc.com/articles.html#reflectVisitor Eliminate accept() methods from your Visitor pattern] " by Bruce Wallace
*Article " [http://www.artima.com/cppsource/cooperative_visitor.html Cooperative Visitor: A Template Technique for Visitor Creation] " byAnand Shankar Krishnamoorthi
* [http://goblin.colourcountry.net/apt1002/Visitor%20patterns Visitor Patterns] as a universal model of terminating computation.
* [http://www.oodesign.com/oo_design_patterns/behavioral_patterns/visitor_pattern.html Visitor Pattern] using reflection(java).
* [http://perfectjpattern.sourceforge.net/dp-visitor.html PerfectJPattern Open Source Project] , Provides a context-free and type-safe implementation of the Visitor Pattern in Java based on Delegates.
Wikimedia Foundation. 2010.