Re: When and where to use Visitor Pattern?




"Rich MacDonald" wrote:
> "Ilja Preu_" wrote:
>
>>> Why make it so complicated?
>>
>> Decoupling. There are simply situations where you don't want to touch
>> the original class when you need to change a specific operation, or
>> where you even want to allow the definition of additional operations
>> without having to touch them.
>
> 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 I discovered this by accident. For the past couple of years,
I've been writing a small MIDI toolkit. One of the classes in the
toolkit is a Track class which is more or less a collection of MIDI
messages.

There are a lot of things you can do to with a Track class. You can
filter it, play it, save it, quantize it, edit it in a myriad of ways...
it goes on and on. Rather than put all of that functionality into the
Track class, I've been using the Visitor pattern to create a lot of
small classes, each dedicated to one operation or algorithm. It's been
an indispensable approach to managing complexity.



.