Build an Expression Evaluator

Visitor pattern

Challenge 3 of Actions as ObjectsAdvanced~55 minFree

Build an arithmetic expression tree and learn the Visitor pattern by adding operations to it. You will give nodes an accept method for double dispatch, write an evaluator, add a new node type, then add printing and node-counting visitors — each a new operation with zero changes to the node classes.

5 steps

  1. 1Nodes That Accept a Visitor
  2. 2The Eval Visitor
  3. 3A New Node Type
  4. 4A Second Visitor, No Node Changes
  5. 5Counting Nodes
Start step 1