Behavioral

Visitor

Description

Lets you separate algorithms from the objects on which they operate.

Where to use

  • Compilers (AST traversal).
  • \n
  • Exporting data.

Real World Example

You have a graph of shapes. You write an 'XMLExportVisitor' that walks the graph and generates XML, without modifying the shape classes.

Code Example

class Visitor:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Visitor')