I am trying to use a visitor (in the sense of “separating algorithm from data structure”). All of the examples I see do something like a sum – where the visitor is SumVisitor and stores an ivar “Sum” that simply adds the value of each element that it visits. However, what if you want to associate the result of the computation with each element? As an easy case, say you want the algorithm (the visitor) to square each element that it visits. Where do you store the results? The only thing I can think of is something like a vector > that is stored in the visitor, but that seems strange. Any suggestions on how to do something like this?
Share
You have four options:
1) Store them in the original data structure somehow if there’s somewhere to put them.
2) Store them in the visitor (e.g. use a map from elements to results).
3) Store them elsewhere, and pass ‘elsewhere’ to the visitor so it knows where to put the results.
4) Create a new data structure to store them as you go along.
Which you pick depends on what you’re trying to achieve.