Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visitor Pattern and traversal mechanism

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?

like image 571
Y.H. Avatar asked Jul 26 '12 10:07

Y.H.


1 Answers

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.

like image 179
Anders Johansen Avatar answered Nov 01 '22 23:11

Anders Johansen