Encapsulating Node Visitation Code
By collecting all of the tree walking methods from Pattern 12, Embedded Heterogeneous Tree Walker into a single class definition, we arrive at Pattern 13, External Tree Visitor. Our primary goal is to separate our tree-walking code from our tree node definitions. The key advantage of a visitor is that it’s external to the objects it walks.
There are a number of ways to achieve this separation, depending on the programming language we use. Languages such as Ruby and Python make short work of the visitor pattern because they can add methods at run-time. Static languages like Java and C++ require some gymnastics.
The first, and most common solution for Java, is to leave a general visit method in each heterogeneous ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access