Visitor Pattern

  1. Summary

      1. This pattern allows different operations to be performed on the elements in a hierarchical object structure (composite object structure).

      2. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

  2. Examples

      1. Tree and Graph traversal algorithms.

  1. Overview Tutorial

  1. When to Use

      1. Similar operations have to be performed on objects of different types grouped in a structure (a collection or a more complex structure).

      2. 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.

      3. The object structure is not likely to be changed but is very probable to have new operations which have to be added.

      4. 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.