[Meta-remark: this whole discussion is about concerns, and properly
separating these concerns. For example, in your previous question on
why having a visitor, the overall reason is that there is two concerns:
one is performing an action on elements in a tree (disregarding the
actual form of the tree), another is the tree itself (whatever your
tree needs to do). If you have these two clear concerns, separate them.
This also answers your question regarding'why should I always use a
visitor'. Separation of concerns gives you an answer: if the only thing
your tree needs to do is print, then it might not be worth it (you then
have only one concern). But then let's not forget that it will be
difficult to add another action later on (like displaying). There is no
prefab all-good solution.
For example, in the case of the StarBrowser and SmallWiki we use a
visitor because we use the possibility to switch visitors all the time.
The Classifications tree has only the behaviour to do traversals and
maintain parent links etc. In the case of Soul, for example, it is more
interesting because you see the roles in practice. The problem is that
your parse tree sometimes needs to do different things, but you have to
decompose it along one direction. In Soul, we decided that it was the
main responsibility to implement the unification protocol directly on
the parse tree: unification is stable (it does not change much, apart
from possible bugfixes), and we need it a lot so it should be fast. So
the Soul parse tree implements the unification on the tree, because it
is logically what the tree needs to do (what we decided that it should
do). However, then we have some other operations that we want to do
with this tree, like pretty printing. For these things we used a
visitor. ]
What you factor out with the visitor is the traversal behaviour. So
take the example from the previous mail (with those three classes A, B
and C and two visitors, one for printing and one for displaying.
The printing visitor will typically implement two methods (yes, only
two, not three): doB: and doC:. Likewise for the display visitor.
Now comes a key point: the visitor class itself will implement the
behaviour that in order to process the A node in this hierarchy, the B
and C node has to be processed. So this behaviour (the traversal) can
be shared amongst all visitors. In this small hierarchy it is debatable
whether this is useful, but in bigger hierarchies it becomes noticeable.
Note that you could use a Strategy to have different tree-walking
strategies in this visitor (a depth-first, a breadth-first, etc.) :-)
On Thursday, March 20, 2003, at 10:53 PM, Markus Gaelli wrote:
No, they don't say that, they just say to be
carefully.
I think, it's just that if the verb is responsible for the action
and not the noun, and the "noun"-object stays stupid it might be
a bad smell...
The main question remains, if you are assigning responsibilities
in a right and balanced way.
Personally I haven't understood, why Visitors could not be seen
as Strategies (at least when you want to apply several
printing/rendering...
strategies, literally) and I am looking forward to read a
paper/article (in JOOP?)
Doru told me about, which compares Visitor with Strategy
Cheers
Markus
Am Donnerstag, 20.03.03 um 22:45 Uhr schrieb Lukas Renggli:
that double-dispatch is not OO too?
Lukas
--
Roel Wuyts Software
Composition Group
roel.wuyts(a)iam.unibe.ch University of Bern,
Switzerland
http://www.iam.unibe.ch/~wuyts/
Board Member of the European Smalltalk User Group:
www.esug.org