Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Visitor pattern
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Details == The visitor pattern requires a [[programming language]] that supports [[single dispatch]], as common object-oriented languages (such as [[C++]], [[Java (programming language)|Java]], [[Smalltalk]], [[Objective-C]], [[Swift (programming language)|Swift]], [[JavaScript]], [[Python (programming language)|Python]] and [[C Sharp (programming language)|C#]]) do. Under this condition, consider two objects, each of some class type; one is termed the ''element'', and the other is ''visitor''. === Objects === ==== Visitor ==== The ''visitor'' declares a <code>visit</code> method, which takes the element as an argument, for each class of element. ''Concrete visitors'' are derived from the visitor class and implement these <code>visit</code> methods, each of which implements part of the algorithm operating on the object structure. The state of the algorithm is maintained locally by the concrete visitor class. ==== Element ==== The ''element'' declares an <code>accept</code> method to accept a visitor, taking the visitor as an argument. ''Concrete elements'', derived from the element class, implement the <code>accept</code> method. In its simplest form, this is no more than a call to the visitor's <code>visit</code> method. [[Composite pattern|Composite]] elements, which maintain a list of child objects, typically iterate over these, calling each child's <code>accept</code> method. ==== Client ==== The ''client'' creates the object structure, directly or indirectly, and instantiates the concrete visitors. When an operation is to be performed which is implemented using the Visitor pattern, it calls the <code>accept</code> method of the top-level element(s). === Methods === ==== Accept ==== When the <code>accept</code> method is called in the program, its implementation is chosen based on both the dynamic type of the element and the static type of the visitor. When the associated <code>visit</code> method is called, its implementation is chosen based on both the dynamic type of the visitor and the static type of the element, as known from within the implementation of the <code>accept</code> method, which is the same as the dynamic type of the element. (As a bonus, if the visitor can't handle an argument of the given element's type, then the compiler will catch the error.) ==== Visit ==== Thus, the implementation of the <code>visit</code> method is chosen based on both the dynamic type of the element and the dynamic type of the visitor. This effectively implements [[double dispatch]]. For languages whose object systems support multiple dispatch, not only single dispatch, such as [[Common Lisp]] or [[C Sharp (programming language)|C#]] via the [[Dynamic Language Runtime]] (DLR), implementation of the visitor pattern is greatly simplified (a.k.a. Dynamic Visitor) by allowing use of simple function overloading to cover all the cases being visited. A dynamic visitor, provided it operates on public data only, conforms to the [[open/closed principle]] (since it does not modify extant structures) and to the [[single responsibility principle]] (since it implements the Visitor pattern in a separate component). In this way, one algorithm can be written to traverse a graph of elements, and many different kinds of operations can be performed during that traversal by supplying different kinds of visitors to interact with the elements based on the dynamic types of both the elements and the visitors.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)