Re: When and where to use Visitor Pattern?



Responding to Hwang...

Why make it so complicated? Can't we use this one instead?
class IntExp1 extends Expression

<snip>
I dont know in what cirumstances the pattern is used sensablely. I hope
someone can address my confusion. Thanks!

Visitor addresses a very complex dynamic accessing problem where there is a combinatorial number of direct relationships between subclasses of two trees:


                 [A]
                  A
                  |
         +--------+-------+--...
         |        |       |
        [B]      [C]     [D]

                 ...

        [W]      [X]     [Y]
         |        |       |
         +--------+-------+---....
                  |
                  -
                  V
                 [Z]

where every subclass of [A] can have a direct 1:1 relationship to every subclass of [Z]. Similarly, every subclass of [Z] can have a direct 1:1 relationship with every subclass of [A]. IOW, there is a combinatorial set of 1:1 relationships between all the subclasses of each relationship. Since those relationships are between subclasses, the specializations are being accessed, which means each relationship must access a unique set of properties. Just to make things a bit worse, the particular relationship being navigated needs to be dynamically determined based upon run-time context.

That gets to be very messy if there are a lot of subclasses. Visitor offers a very elegant way to handle that in a cookbook fashion. OTOH, if not /all/ of those factors are relevant, then Visitor becomes overkill.


************* There is nothing wrong with me that could not be cured by a capful of Drano.

H. S. Lahman
hsl@xxxxxxxxxxxxxxxxx
Pathfinder Solutions  -- Put MDA to Work
http://www.pathfindermda.com
blog: http://pathfinderpeople.blogs.com/hslahman
(888)OOA-PATH



.



Relevant Pages

  • Re: Removing inheritance (decorator pattern ?)
    ... Naturally, the decorator pattern ... My multiple inheritance classes though play a double role: ... I was thinking the Strategy pattern, ... could end up with as many Strategy classes as subclasses, ...
    (comp.lang.python)
  • Re: Adapting code to multiple platforms
    ... My plan is to have a superclass ... > that performs the generic functions and subclasses to define methods ... You may be interested to know this is called the Template Pattern. ...
    (comp.lang.python)