Jump to content

Visitor pattern

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Vfnn (talk | contribs) at 08:59, 24 January 2022 (Fixed serial violations of MOS:BOLD, MOS:SECTIONSTYLE, MOS:CONTRACTIONS). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In object-oriented programming and software engineering, the visitor pattern is a way of separating an algorithm from an object structure on which it operates. A practical result of this separation is the ability to add new operations to existing object structures without modifying the structures. It is one way to follow the open/closed principle.

The visitor pattern allows new virtual functions to be added to a family of classes, without modifying the classes. Instead, a visitor class is created that implements all of the appropriate specializations of the virtual function. The visitor takes the instance reference as input, and implements the goal through double dispatch.

Overview

The visitor design pattern is one of the 23 described in Design Patterns (1994) that describe how to solve recurring design problems to design flexible and reusable object-oriented software.

Target problems

It may be desirable to define a new operation for some classes of an object structure without changing the classes. When new operations are needed frequently and the object structure consists of many unrelated classes, it is inflexible to add new subclasses each time a new operation is required.[1]

Solution

The visitor pattern involves the definition of a separate visitor object that implements an operation to be performed on elements of an object structure. Clients traverse the object structure and call a dispatching operation accept (visitor) on an element — that "dispatches" (delegates) the request to the "accepted visitor object". The visitor object then performs the operation on the element ("visits the element"). This makes it possible to create new operations independently from the classes of an object structure by adding new visitor objects.

Definition

The Gang of Four defines the Visitor as:

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

The nature of the Visitor makes it an ideal pattern to plug into public APIs, thus allowing its clients to perform operations on a class using a "visiting" class without having to modify the source.[2]

Uses

Moving operations into visitor classes is beneficial when

  • many unrelated operations on an object structure are required,
  • the classes that make up the object structure are known and not expected to change,
  • new operations need to be added frequently,
  • an algorithm involves several classes of the object structure, but it is desired to manage it in one single location,
  • an algorithm needs to work across several independent class hierarchies.

A drawback to this pattern, however, is that it makes extensions to the class hierarchy more difficult, as new classes typically require a new visit method to be added to each visitor.

  • Iterator pattern – defines a traversal principle like the visitor pattern, without making a type differentiation within the traversed objects
  • Church encoding – a related concept from functional programming, in which tagged union/sum types may be modeled using the behaviors of "visitors" on such types, and which enables the visitor pattern to emulate variants and patterns.

See also

References

  1. ^ Cite error: The named reference GoF was invoked but never defined (see the help page).
  2. ^ Visitor pattern real-world example