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!
==Overview== The Visitor<ref name="GoF">{{Cite book |last=Gamma |first=Erich |author-link=Erich Gamma |url=https://archive.org/details/designpatternsel00gamm/page/331 |title=Design Patterns: Elements of Reusable Object-Oriented Software |last2=Helm |first2=Richard |author-link2=Richard Helm |last3=Johnson |first3=Ralph |author-link3=Ralph Johnson (computer scientist) |last4=Vlissides |first4=John |author-link4=John Vlissides |publisher=Addison Wesley |year=1994 |isbn=0-201-63361-2 |pages=[https://archive.org/details/designpatternsel00gamm/page/331 331ff] |url-access=registration}}</ref> design pattern is one of the twenty-three well-known ''[[Design Patterns|Gang of Four design patterns]]'' that describe how to solve recurring design problems to design flexible and reusable object-oriented software, that is, objects that are easier to implement, change, test, and reuse. === Problems, the Visitor design pattern can solve === * It should be possible to define a new operation for (some) classes of an object structure without changing the classes. When new operations are needed frequently and the object structure consists of many unrelated classes, it's inflexible to add new subclasses each time a new operation is required because "[..] distributing all these operations across the various node classes leads to a system that's hard to understand, maintain, and change."<ref name="GoF" /> === Solution, the Visitor design pattern describes === * Define a separate (visitor) object that implements an operation to be performed on elements of an object structure. * Clients traverse the object structure and call a ''dispatching operation accept (visitor)'' on an element β that "dispatches" (delegates) the request to the "accepted visitor object". The visitor object then performs the operation on the element ("visits the element"). This makes it possible to create new operations independently from the classes of an object structure by adding new visitor objects. See also the UML class and sequence diagram below.
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)