liveBook · Manning

Visitor design pattern

As explained in Software Design for Python Programmers

The Visitor pattern is a behavioral design pattern that separates an operation from the object structure on which it operates, allowing operations to be added without changing the structure's classes.

The Visitor pattern separates algorithms from a heterogeneous object structure. It suits a tree such as intramural sports data, where multiple passes produce different reports without embedding report logic in the nodes. section 11.0 section 11.2

A typical implementation divides responsibilities:

  • Make each element implement Node.accept(visitor). Intramural and Sport accept a visitor and forward it to their children; Game and Hall dispatch directly. section 11.2.3
  • Define a Visitor interface with a visit method for every element type, such as visit_intramural(), visit_sport(), visit_game(), and visit_hall(). section 11.2.3 section 11.2.4
  • Put each operation in a concrete visitor. ScoresReportVisitor, ActivitiesReportVisitor, and WinningsReportVisitor each implement one reporting algorithm. section 11.2.3
  • Run a pass by sending a visitor to the root: intramural_node.accept(visitor). Polymorphism selects the visit method matching each node type. section 11.2.3 section 11.2.4

Refactoring intramural reports

Before Visitor, Intramural, Sport, Game, and Hall contain methods for scores, activities, and winnings reports. Adding another report requires changing those data classes and extending their call chains. After refactoring, the classes maintain the tree and expose properties, while separate visitors generate each report. The same tree can therefore produce scores, activity counts, and winnings through three independent passes. section 11.2.2 section 11.2.3 section 11.4

Figure 11.11 The generic model of the Visitor Design Pattern. Compare with figure 11.10. Each Visitor class encapsulates an algorithm. To execute an algorithm during a pass of the Element objects, each Element object accepts a Visitor object. The accept() function of each concrete Element object calls the visit method that corresponds to that Element object. The visit methods together execute the Visitor class’s algorithm as they visit the Element objects one after another.
A diagram of a computer

Description automatically generated

Use the generic model to trace how concrete elements accept one visitor and dispatch to matching visit methods during a pass. section 11.2.4

How Software Design for Python Programmers covers it

How deep it goes
Explains interfaces, accept traversal, runtime dispatch, concrete visitors, and a before-and-after Python refactoring.
Examples
Uses store products and intramural baseball, football, and volleyball games to demonstrate tree traversal and reporting visitors.
What you can do after
Build it from scratch by defining Node and Visitor interfaces, implementing accept dispatch, and adding independent algorithms as concrete visitors.
How it connects
Contrasts with the Iterator Design Pattern and distinguishes operation separation from the State Design Pattern’s state-based behavior.

Also explained in

Compare how 4 books explain visitor design pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Design for Python Programmers. The section links point to the parts of the book it draws on.