Re: Visitor Pattern Choices
- From: "H. S. Lahman" <h.lahman@xxxxxxxxxxx>
- Date: Thu, 29 Dec 2005 18:08:09 GMT
Responding to Merlin...
Probably there is no right or wrong answer to this but I thought to ask to put my mind at rest. Ok lets say you have a object hierarchy (eg. the Glyph in Lexi from GOF book) and you want to use the visitor pattern. So we place an accept method in the the base class glyph and procede to create the visitor hierarchy. The accept signature will look like this
void Glyph::Accept(Visitor& v);
The Visitor hierarchy will have a Vistor base class that will have a visit method for each different type of Glyph so say if we have an AssignnentNode and VariableNode there will be two methods
void visit(AssignnentNode& n); void visit(VariableNode & n);
and in each concrete visitor we implement these methods to behave as we wish. So far so good...
Imagine you think of 20 different types of operations that you like to perform on your object structure and u like to use the Visitor pattern to do this. So you create 20 different classes each implementing the visitor interface.
Should we inherit all these 20 classes from our visitor base class? If so this means a single Accept method in the object structure is sufficent to serve all of these operations.
Should we create different and separate visitor hierarchies each with its own accept method but grouping related operations together?
Should we go half way and create these groups but all inherit from a common visitor base class. This approach also will require a single accept method.
Currently I am leaning towards the last approach but I am worried that having a single visitor hierarchy has tied 20 operations together (some related while others unrelated). The hierarchy is more bushy and deeper but it still
Whats the general feeling of the developer community on this topic?
Why do you "want" to use Visitor in the first place? Answer that question and you will answer the others.
IOW, which approach is best depends upon the nature of the collaborations in your particular situation. Thus if every flavor of Glyph needs to interact with the Visitor in a different way, you will need the first solution; if all flavors interact the same way, the second approach works; if Glyph flavors can be grouped by a few common interactions, then something in the middle will work.
************* 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
.
- Follow-Ups:
- Re: Visitor Pattern Choices
- From: Merlin
- Re: Visitor Pattern Choices
- From: Mateusz Łoskot
- Re: Visitor Pattern Choices
- References:
- Visitor Pattern Choices
- From: Merlin
- Visitor Pattern Choices
- Prev by Date: Re: OO Unit Test Question
- Next by Date: Re: Visitor Pattern Choices
- Previous by thread: Visitor Pattern Choices
- Next by thread: Re: Visitor Pattern Choices
- Index(es):
Relevant Pages
|