An accept method lets a tree node receive a visitor, dispatch the visitor operation matching the node type, and continue traversal through child nodes. section 11.2.3
A node-specific accept method typically:
- Calls the visitor method for its own type, passing itself so the visitor can use the node's public methods. section 11.2.3
- Delegates to child nodes, keeping collection structure and traversal out of visitor classes. section 11.2.3
- Uses polymorphism to bind node and visitor types at run time. section 11.2.3
Intramural node
An Intramural node calls visitor.visit_intramural(self), then asks each Sport and Hall child to accept the same visitor. Sport similarly calls visit_sport(self) and visits its Game children. section 11.2.3
Listing 11.22 (Program 11.4 Results-VisitorDP) intramural.py
from node import Node
class Intramural(Node):
...
def accept(self, visitor):
visitor.visit_intramural(self) #1
for sport in self._sports:
sport.accept(visitor) #2
for hall in self._halls:
hall.accept(visitor) #3
Shows an accept implementation that dispatches to the node-specific visitor method and recursively visits child collections. section 11.2.3
