Visitor pattern

The Visitor (English visitor or visitor pattern ) is a design pattern in the field of software development and belongs to the category of behavior ( behavioral patterns ). The pattern is one of the so-called GoF design patterns. It is used for encapsulation of operations performed on items of an object structure that enable that new operations can be defined without any change of the affected member classes.

With the help of the visitor easy multi methods ( double dispatch ) implemented in an OOP language that natively supports only single dispatch.

Use

The integration of different of unrelated operations in the classes of an object structure is often difficult. With the addition of new operations, all classes must be extended. The visitors outsourced the operations in external visitor classes. For this purpose, however, to visit classes must define an interface for receiving a visitor.

In general, we recommend the use of visitors when

  • Many different, unrelated operations are to be implemented in an object structure,
  • Not change the class of the object structure,
  • Often new operations must be integrated on the object structure or
  • An algorithm about the classes of an object structure works distributed, but should be centrally managed.

UML diagram

Actors

  • Visitor declared for each class of concrete elements a callback function
  • Visit implemented functions
  • Each Visited Function is a fragment of the algorithm which is applied to the entire object structure
  • Local state serves as a context for the algorithm
  • Declares an interface for receiving a visitor's
  • Implements the reception of a visitor
  • Collection or composite object structure (see compound ( design type) )

Benefits

  • New operations can easily be uploaded through the definition of new visitors.
  • Related operations are managed centrally in visitors and separated from visitors foreign operations.
  • Visitors can work on multiple class hierarchies.

Disadvantages

  • The good expandability of the classes of visitors must be paid for with a poor extensibility of classes of concrete elements. Do new concrete elements are added, this means that many visitors visits methods must be implemented.

Examples

Virtual Travel Agency

A tour operator offers its customers a variety of bus trips, vacation homes and rental cars. Each object is assigned a description and a price category for summer and winter. The prices of the categories are stored in a module price. For holiday homes are also pictures, technical data stored in rental cars. Both the classes for bus travel, holiday homes and rental cars, as well as the price module provide an interface for receiving a visitor. The price module is outside the class hierarchy of coach travel, holiday homes and rental cars.

A customer can put together a trip now. He then asks for the total price, so a visitor first visits the objects of interest, queries the respective category. For each category, he maintains a local counter. Most recently, he visited the Price module and calculated on the basis of the stored there prices and its locally collected information the total price.

To the client decides to book the trip, you can create a travel confirmation another visitor. For this purpose he visited again the customer objects of interest and the price module. Its local state of a document, which he designed in accordance with the information of the objects. For any object, he added first lists the description and the price level in the rental car technical data. Visiting the price module he adds, the individual descriptions for the specific prices.

Both visitors engage over class hierarchies, as they work on both the class hierarchy of bookable travel elements as well as on the price module.

Visitors in compilers

In compiler construction is according to the syntactic analysis mostly an abstract syntax tree in front. Such a tree can be well described by classes for the various elements and use of aggregations as an object structure. On this object structure, you can now define a general visitor who traverses the tree. For this purpose the visit function for a class element of the tree are visited the aggregated elements sequentially in implementation. From this general visitors is now possible to derive various visitors that implement different operations on the abstract syntax tree.

In a visitor can realize the semantic analysis. These visits this the elements of the tree and extends the symbol table for information about data types, variables and routines or checks expressions involving the symbol table, whether they are wohltypisiert. According to the characteristics of the source language, the collection of information and the type test shall be distributed to two visitors.

Another visitor may then implement the synthesis of the target code. Also, these visits to the individual elements and collects the target code fragments in its local state. Depending on the class of the visited element can then already be combined to form larger fragments collected.

More visitors can gather debugging information, or perform code optimizations on the source code base. All visitors are able to draw on the functions of the general visitor's visit, when an item without further operations should only be traversed. Also, the target code may initially be generated in a tree structure again in order to realize improvements in various then different users.

Related Design Patterns

  • Command. The command encapsulates as the visitor one or more functions in an object, to deliver it to a caller. In contrast to the visitors the command contains no principle for traversing an object structure.
  • Iterator. The iterator defines a Traversierungsprinzip as well as the visitors, but does not make any type of discrimination in the traversed objects.

Relationship to other design patterns

  • Compound. If the types are defined sufficiently stable in a composite structure, a Visitor to edit the structure offers.

Pictures of Visitor pattern

120779
de