Visitor pattern 00:00:00 Make a prediction You will predict the cost of adding an operation versus adding an element type, and demonstrate how a concrete node reaches the right handler. An expression tree already has number and addition nodes. You add a pretty-printer, then add a brand-new multiplication node. Which change forces edits to existing visitor implementations? The multiplication node adds a case every complete visitor must handle. A new pretty-printer can be another visitor over the existing cases. This asymmetry tells you when the pattern fits. 00:00:35 The street and its callers Several professionals visit the same street. An electrician checks wiring at each kind of building; a tax assessor examines different facts in those very buildings. Adding a new profession is possible without rebuilding the street. A new building type can require every profession to learn a new inspection. Buildings are element types and visiting professions are operations. The node dispatches the visiting operation to its matching case. Visitor is a behavioral design pattern. It represents an operation on the elements of an object structure as an object. The structure can be a list, a tree or anything that can be walked. Operations that do not belong to the data, such as exports, checks and reports, would otherwise fill the data classes and pull in their libraries. 00:01:25 Add a second expression operation An expression tree has number and addition nodes. An evaluator and a pretty-printer both need to process every node but perform different work. Each node accepts a visitor and calls the matching visit operation with itself. The evaluator's number handler returns the numeric value; the printer's returns text. Add a printer without editing the arithmetic classes. Now imagine adding a multiplication node: every visitor must learn that case. This is the pattern's central tradeoff. Visit the same two-leaf expression twice. One operation returns five; the other emits the text two plus three. The tree stays unchanged. If adding the formatter requires embedding output-specific conditionals in every expression operation, the visit has not isolated the new behaviour. 00:02:18 Choose visitor only for the right change direction A drawing application adds export formats every month but rarely changes its shape types. A visitor per format can group export logic together. If new shape types arrive weekly instead, a common draw operation on each shape may be cheaper to maintain. Add a multiply node and list every visitor that now needs a case. That extra work is the price of making new operations easy. When the team introduces new node types every week and rarely introduces operations, another extension strategy may fit its change pattern better. If the model frequently gains new node types and has few operations, a visitor may make the common change expensive. Keep a method on each element, or use language-supported exhaustive pattern matching, when that matches the expected change direction better. Python’s syntax-tree library dispatches an operation based on the kind of node being visited. A transformer can return a replacement node. The Clang compiler also exposes a visitor for traversing syntax trees. Both illustrate how a new analysis can examine existing node types without embedding each analysis into the nodes. 00:03:28 Try an interview follow-up How do you rewrite a tree with a visitor? I would use immutable nodes and have each visit return the replacement subtree. If nothing below changed, the visitor can return the original node. That makes progress visible across repeated passes. I would set a pass limit and check that rewrites intended to preserve meaning produce the same evaluated value. A drawing tool has stable shape types but gains new export formats regularly. Trace adding one exporter using a visitor. Next trace adding a new shape. Every visitor may now need a case for it. Show both edits to explain when the pattern fits the expected direction of growth. 00:04:10 What to take with you Draw the grid of types and operations. If operations grow, make each one a visitor. accept names the element's type, the visitor answers: two dispatches, no type check. The price is fixed: each new element type touches every visitor, and elements must be readable. Decide for unknown elements whether to fail, ignore or pass on.