Visitor design pattern
E679870
The Visitor design pattern is a behavioral software design pattern that lets you separate algorithms from the object structures they operate on, enabling new operations to be added without modifying the existing classes.
Observed surface forms (1)
| Surface form | Occurrences |
|---|---|
| Visitor pattern | 2 |
Statements (53)
| Predicate | Object |
|---|---|
| instanceOf |
behavioral design pattern
ⓘ
software design pattern ⓘ |
| applicableWhen |
classes defining the object structure rarely change
ⓘ
many unrelated operations need to be performed on objects in a structure ⓘ object structure is relatively stable ⓘ |
| belongsTo | Gang of Four design patterns NERFINISHED ⓘ |
| benefit |
can simplify maintenance of complex operations
ⓘ
facilitates adding cross-cutting operations ⓘ improves separation of concerns between data structures and operations ⓘ keeps related operations together in visitor classes ⓘ supports accumulation of state during traversal ⓘ |
| commonUseCase |
code analysis tools
ⓘ
compilers and abstract syntax tree processing ⓘ document object model traversal ⓘ operations on hierarchical data structures ⓘ |
| coreMechanism | double dispatch ⓘ |
| definesCollaborationBetween |
Element
ⓘ
ObjectStructure ⓘ Visitor NERFINISHED ⓘ |
| describedIn | Design Patterns: Elements of Reusable Object-Oriented Software NERFINISHED ⓘ |
| drawback |
adding a new element class requires changes to all existing visitors
ⓘ
can break encapsulation by exposing element internals to visitors ⓘ increases coupling between visitors and element classes ⓘ |
| enables | adding new operations by creating new visitor classes ⓘ |
| hasAlias | Visitor ⓘ |
| hasCategory | behavioral pattern ⓘ |
| introducedBy |
Erich Gamma
NERFINISHED
ⓘ
John Vlissides NERFINISHED ⓘ Ralph Johnson NERFINISHED ⓘ Richard Helm NERFINISHED ⓘ |
| keyMethod |
accept()
ⓘ
visit() ⓘ |
| oftenUsedWith |
composite pattern
ⓘ
interpreter pattern NERFINISHED ⓘ iterator pattern ⓘ |
| primaryIntent |
enable adding new operations without modifying existing classes
ⓘ
separate algorithms from object structures ⓘ |
| relatedConcept |
double dispatch
ⓘ
open/closed principle NERFINISHED ⓘ separation of concerns ⓘ |
| requires | modifying visitor interface when adding new element types ⓘ |
| supports | open/closed principle ⓘ |
| supportsLanguageFeature | emulation of multiple dispatch in single-dispatch languages ⓘ |
| tradeOff | easy to add new operations but hard to add new element classes ⓘ |
| typicalLanguageContext | object-oriented programming ⓘ |
| typicalStructureElement |
ConcreteElement class
ⓘ
ConcreteVisitor class NERFINISHED ⓘ Element interface ⓘ ObjectStructure class ⓘ Visitor interface ⓘ |
| usedFor |
defining new operations on object structures
ⓘ
performing operations on elements of an object structure ⓘ traversing complex object structures ⓘ |
Referenced by (4)
Full triples — surface form annotated when it differs from this entity's canonical label.
this entity surface form:
Visitor pattern
this entity surface form:
Visitor pattern