What is the Visitor design pattern?

Answer

The Visitor pattern lets you add new operations to a class hierarchy without modifying those classes, by separating the algorithm from the object structure. You define a Visitor interface with a visit() method for each element type. Each element implements an accept(Visitor v) method that calls v.visit(this) — this "double dispatch" technique ensures the correct visitor method is called based on both the element type and the visitor type. Use it when you have a stable set of element types but need to frequently add new operations. XML/AST processors and compiler optimization passes are classic examples.