We have already learned that the Visitor pattern has to know the structure of the composite for someone to write an instance of the Visitor interface. This can create an anomaly called abstraction leak. The GoF pattern catalog has a pattern that will help us to navigate the contents of a tree in a structure-agnostic manner. Yes, you might have guessed it correctly: the Iterator pattern is the candidate! For the Iterator to do its job, the composite has to be flattened into a list sequence or Stream. Let's write some code to flatten the expression tree that we modeled in the previous section. Before we write the logic to flatten a Composite, let's create a data structure to store the contents of an AST as a list. Every node in the list has to store either an operator or value, depending upon whether we need to store operators or operands. We describe a data structure called EXPR_ITEM
for this purpose:
//////////////////////////// // A enum...