Visitor Pattern


The Visitor pattern provides a maintainable, easy way to represent an operation to be performed on the elements of an object structure. The Visitor pattern lets you define a new operation without changing the classes of the elements on which it operates.

UML representation

Benefits

The following lists the benefits of using the Visitor pattern:
  • Makes adding new operations easy
  • Gathers related operations and separates unrelated ones

When to Use

You should use the Visitor pattern when:
  • An object structure contains many classes of objects with differing interfaces, and you want to perform operations on these objects that depend on their concrete classes.
  • Classes defining the object structure rarely change, but you often want to define new operations over the structure.
Subpages (1): Visitor Pattern Example
Comments