Design patterns·Who calls whom, and when
you added a new operation over the whole tree by writing one class, and did not touch any of the node types.
Visitor pattern
Also calledvisitor, double dispatch pattern
Moving an operation out of a structure's element classes into a separate object, which each element calls back with its own type. It trades one kind of extensibility for another: adding an operation becomes easy and adding a node type becomes a change to every visitor — the expression problem, stated concretely. It is genuinely used in compilers, linters and anything that walks an AST, and almost nowhere else.