I implemented the visitor pattern according to cpppaterns.com to traverse a tree. I always need to traverse the tree in the same order, hence I moved the logic which determines which child to visit next, into the base class Visitor
. The individual visitors perform specific operations on the tree nodes and then call the visit
method in the base Visitor
. This works pretty good.
In some cases it is needed to perform actions implemented among multiple visitors (e.g., VisitorRewriteX
, VisitorPrintX
) on one tree. The naive way is to just execute the visitors sequentially. However, this requires traversing the tree multiple times which is inefficient. Of course, I could also create a new visitor (e.g., VisitorRewritePrintX
) which just calls the other two visitors—but I think that's not clean code either. Is there some other way (e.g., pattern) which could help me to somehow allow 'stacking' of the actions implemented in the visitors?
Aucun commentaire:
Enregistrer un commentaire