M
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?
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?