## Visitor pattern

### Introduction

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

### Participants:

#### Visitor

- declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the element directly through its particular interface.
- this participant can not be read-only.

#### Visitor Adapter

- provides trivial implementation for each Visitor's operation.
- this participant can not be read-only.
- this participant can not be an interface.

You can enable/disable generation of the Visitor Adapter using the "Create visitor adapter" checkbox.

#### Concrete Visitor

- implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class of object in the structure. Concrete Visitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
- this participant can not be read-only.
- this participant can not be an interface.

#### Element

- defines an accept operation that takes a visitor as an argument.

#### Concrete Element

- implements an accept operation that takes a visitor as an argument.
- this participant can not be read-only.
- this participant can not be an interface.

The "Copy documentation" option controls whether to copy JavaDoc comments from methods in interfaces participating in the pattern to the stubs of these methods made by the pattern in classes implementing such interfaces.

The "Create pattern links" option controls whether to generate additional links which can be used by this pattern later to determine classes and interfaces participating in the pattern. That means that if you selected this option and used the pattern to create a set of classes and interfaces, the pattern invoked for some participant later (via the popup menu item "Choose Pattern...") will automatically find (if it is possible) all other participants and fill in participant fields with their names.

Also, if you applied the pattern with this option on and invoked the pattern via the popup menu item "Choose Pattern..." for some participant later, the pattern will have additional field called "Use selected class as", containing possible roles for the selected element.

This option is very useful when you are planning to change something in the classes/interfaces participating in the pattern. For example, if this option is on and after creation of the classes and interfaces you added several methods to certain interface-participant (and this change must be reflected somehow in other participants), all you have to do is to select this changed interface, invoke the "Choose Pattern..." dialog for this element and select the original pattern. After that the pattern will determine other participants and you will only have to press "Finish". The pattern will modify all other classes and interfaces accordingly to your changes.

### Applicability

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.
- many distinct and unrelated operations need to be performed on objects in an object structure, and you want to avoid "polluting" their classes with these operations. Visitor lets you keep related operations together by defining them in one class. When the object structure is shared by many applications, use Visitor to put operations in just those applications that need them.
- the classes defining the object structure rarely change, but you often want to define new operations over the structure. Changing the object structure classes requires redefining the interface to all visitors, which is potentially costly. If the object structure classes change often, then it's probably better to define the operations in those classes.