- Summary
- This pattern allows different operations to be performed on the elements in a hierarchical object structure (composite object structure).
- Visitor lets you define a new operation without changing the classes of the elements on which it operates.
- Examples
- Tree and Graph traversal algorithms.
- Overview Tutorial
- http://sourcemaking.com/design_patterns
- http://www.oodesign.com/visitor-pattern.html
- When to Use
- Similar operations have to be performed on objects of different types grouped in a structure (a collection or a more complex structure).
- There are many distinct and unrelated operations needed to be performed. Visitor pattern allows us to create a separate visitor concrete class for each type of operation and to separate this operation implementation from the objects structure.
- The object structure is not likely to be changed but is very probable to have new operations which have to be added.
- Since the pattern separates the visitor (representing operations, algorithms, behaviors) from the object structure it's very easy to add new visitors as long as the structure remains unchanged.
|
|