Module 23 Β· Design Patterns (All 23 GoF) β Lesson 23 of 23 Β· ~6 min
Visitor
Visitor: add operations over a fixed structure via double dispatch
Problem: you have a fixed set of node types and keep needing new operations over them β and you don't want to edit every node class each time.
PawWalk scenario: booking line items. A Total visitor sums them; a Receipt visitor formats them. Add a new visitor without touching the items.
Be honest: Visitor is clunky in dynamic languages. In real Ruby you'd often replace it with a
caseon type or pattern matching. It shines only when the node set is truly fixed and operations grow often.