Visitor

From CDOT Wiki
Revision as of 21:58, 8 April 2007 by Snnaraid (talk | contribs)
Jump to: navigation, search

Visitor Pattern (Object Behavioral)

Represents an operation to be performed on the elements of an object Structure. Visitor lets you define a new operation withou changing the classes of the elements on which it operates.

Prupose

The purpose of the Visitor Pattern is to encapsulate an operation that you want to perform on the elements of a data structure. In this way, you can change the operation being performed on a structure without the need of changing the classes of the elements that you are operating on. Using a Visitor pattern allows you to decouple the classes for the data structure and the algorithms used upon them.

Each node in the data structure "accepts" a Visitor, which sends a message to the Visitor which includes the node's class. The visitor will then execute its algorithm for that element. This process is known as "Double Dispatching." The node makes a call to the Visitor, passing itself in, and the Visitor executes its algorithm on the node. In Double Dispatching, the call made depends upon the type of the Visitor and of the Host (data structure node), not just of one component.

UML Diagram

Example.jpg