I have a graph and a set of very simple visitors that do some calculations on vertices/edges of the graph such as getting the total weight of edges and pretty-printing the graph.
I would like to go one step further and have visitors that visit the graph in DFS fashion for example.
However, I think the visitor pattern should be separate from the traversal mechanism, and the traversal method should be created using other pattern such as iterator. Am I correct or do I miss something?
As the comments say, Visitor is not concerned with the traversal mechanism as such. That is a job for an Iterator, or some other concrete traversal method, to decide.
The Visitor is not as such "unneeded" if you have an Iterator. It depends on the complexity of the operation(s) you want to apply to the items you are iterating over.
Visitor is logically an extension of the existing objects without changing their implementation. Therefore its purpose is to encapsulate this extension for reuse and further extension.
If you have an Iterator and a trivial operation that you want to apply to all elements, then there's no need for a Visitor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With