Re: When and where to use Visitor Pattern?



On Wed, 27 Apr 2005 00:51:29 -0400, "Daniel Parker"
<danielaparker@spam?nothanks.windupbird.com> wrote:

>"Rich MacDonald" <rich@@clevercaboose.com> wrote in message
>news:Xns9644C928CC626richclevercaboosecom@xxxxxxxxxxxxxxx
>> "Ilja Preuß" <it@xxxxxxxxxxxxx> wrote in
>> news:426ba2dc@xxxxxxxxxxxxxxxxxxxxxxx:
>>
>> Or the fact that that original class(es) needs to "do a hundred things".
>> Rather than add all that stuff into the class and create a mess, you keep
>> it lean and delegate off to a hundred visitors.
>>
>> Done well, I consider it the most useful and powerful pattern I know.
>
>I think it's fair to point out, though, that this sentiment is not
>universal, see for example http://nice.sourceforge.net/visitor.html. To the
>extent that the vistor pattern is just a poor man's substitute for
>multimethods, wouldn't "instanceof" do just as well? And be a little
>clearer? With less clutter in the core class (no visit method)?

No, instanceof does not do just as well. To use instanceof you have
to create a if/else statement that depends on every possible
derivative:

if (x instanceof D1)...
else if (x instanceof D2)...
else if (x instanceof D3)...
....

Not only is this time consuming, it's also heavy with dependencies.
visitor (especially acylic visitor) mitigates both of these problems.


-----
Robert C. Martin (Uncle Bob) | email: unclebob@xxxxxxxxxxxxxxxx
Object Mentor Inc. | blog: www.butunclebob.com
The Agile Transition Experts | web: www.objectmentor.com
800-338-6716


"The aim of science is not to open the door to infinite wisdom,
but to set a limit to infinite error."
-- Bertolt Brecht, Life of Galileo
.


Quantcast