Skip to content
StackDecode
System Design
15. Software Design PatternsAnimated walkthrough

Visitor Pattern

Add operations without changing the classes.

The idea

Elements accept a visitor, which implements the operation per element type.

Watch it work

The steps light up one at a time. Use the controls to pause or step through.

  1. 1Define visitor methods
  2. 2Elements call accept
  3. 3The visitor handles each type
  4. 4Add new visitors freely
1/4

Step by step

  1. 1Define visitor methods
  2. 2Elements call accept
  3. 3The visitor handles each type
  4. 4Add new visitors freely

Where it is used

Compilers and tree operations.

Watch out for

Adding a new element type changes every visitor.

Related