Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Visitor pattern
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Visitor pattern
(section)
Add topic