Adds new operations and behaviours to existing objects without modifying them.

Able to separate an algorithm from the object on which it operates.

  • "Double Dispatch" technique
    • An object takes in a visitor, and calls the right visitor method with the needed data

ie Car

1
2
3
4
5
6
visitor = Visitor()
Car.accept(visitor)
-> for each element in car:
   -> element.accept(visitor)
      -> visitor.visit(this)
visitor.visit(this)
  • Define a Visitor class which knows of the classes it will visit
    • Has a visit(T) method for each class type
    • In the implementation, do whatever you need
  • Define an interface Visitable which exposes an accept(Visitor) method
    • In the implementation, <Visitor>.visit(this)

A new class would require a new visit method to be added to each visitor...