473,788 Members | 2,694 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Visitor Pattern Choices

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(V isitor& 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(Assignnen tNode& n);
void visit(VariableN ode & 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?

Dec 29 '05 #1
17 2609
I did not completly understand what you said.
I am not able to understand the class hierarchy.

but I am able to understand that you want to overload the visit
function 20 times. That is not really needed and it is very painful.

Use templates instead of that.

If you know templates, you will understand need for reusability,
especially when you want to overlaod several hundred times. You will
create only one function and you use it for any kind of datatype
(including user-defined)

Dec 29 '05 #2
Murali Krishna wrote:
I did not completly understand what you said.
I am not able to understand the class hierarchy.


Please quote the message you are answering to and do read about the
visitor pattern.
Jonathan

Dec 29 '05 #3
Merlin wrote:
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?


I'm not sure what is the problem here and what you are asking about.
If I understand it correctly, you try to have different visitors
implementing different algorithms. Am I right?

If I am, then I'd try to combine Visitor pattern with Strategy pattern.
I'm not sure about possible redundancy here so may be someone else
would like to comment it.

In other words, if you have GoF in front of you take a look at the
Strategy (page 315) and there are two classes in sample: Composition and
Compositor. My idea is to make Composition a Visitor whish uses
different strategies. Does it make sens to anybody? I'm curious too :-)
Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 29 '05 #4
Hi Jonathan,

Thanks for that. Any way I am new to this Google group. I wanted to
Quote. Frankly, I dont know how to do that. Help me how to quote. Next
time I'll start quoting.
Any way did my answer gave any solution?

Dec 29 '05 #5
Firstly thank you for your replies guys but I think maybe you are
unfamiliar with the visitor pattern.
Let me rephrase my question.
Is it a good idea to have just a single Accept method in the Glyph
class for all the visitor classes even if there are 100's? Or should we
group these
visitors logically and introduce different Accept methods in the Glyph
class. I dont see whats not clear about this question... maybe I need
some sleep!

Regards

Dec 29 '05 #6
Murali Krishna wrote:
Hi Jonathan,

Thanks for that. Any way I am new to this Google group. I wanted to
Quote. Frankly, I dont know how to do that. Help me how to quote. Next
time I'll start quoting.
Any way did my answer gave any solution?


It's not a Google group, it's a Usenet group. Google groups is just one
of the ways of reading and posting.

Instead of clicking Google's "Reply" link at the bottom of the message,
click "Show Options" at the top of the message then use the "Reply"
link revealed there. You will find the message you are replying to is
automatically quoted for you. Remember to post your message below or
inline with what you are replying to (but not above) and to snip
unnecessary context.

I posted this message from Google Groups and quoted you without a
problem.

Gavin Deane

Dec 29 '05 #7
Merlin wrote:
Firstly thank you for your replies guys but I think maybe you are
unfamiliar with the visitor pattern.
I'm pretty familiar with Visitor but may be you don't give us clear
picture of the problem. I think both are possible :-)
Let me rephrase my question.
OK, let's try again.
Is it a good idea to have just a single Accept method in the Glyph
class for all the visitor classes even if there are 100's? Or should
we group these visitors logically and introduce different Accept
methods in the Glyph class.


Hard to say because I don't know your structure much.
So, I'll ask additional question.

Can all those Visitors be unified in terms of common interface?
May be you can solve your problem by inheritance from common Visitor
interface. Then all Visitors would be used in unified way (common
interfaace) but they could still distinguish in terms of implementation.
In such case you need only one Accept function which takes Visitor by
it's interface (pointer to Visitor base class).

Please, not I said "may be", so try to fit my idea (if you like) to your
problem becasue you know it (the problem) best.

What I'm sure in 99% about is that having class with 100 member
functions is definitely not a good idea. Would you like to be a client
of such class? I would not ;-)

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 29 '05 #8

Mateusz Loskot wrote:
What I'm sure in 99% about is that having class with 100 member
functions is definitely not a good idea. Would you like to be a client
of such class? I would not ;-)


Ever used std::string? ;-)

Gavin Deane

Dec 29 '05 #9
Hi Mateusz

You will never be the client of a class with 100 member functions. You
keep forgetting that the visitor base class will have as many visit
methods as there are nodes in the Glyph hierarchy, eg Row, Column,
Graphics, etc look in GOF. In other words only a few methods will be in
Visitor. BUT there will be 100 derived classes overriding these few
methods in different ways, each specific to the type of operation being
carried out.

When the time comes to use one these visitors, you only create an
OperationalVisi tor that you need and then apply it to your glyph.

Now back to my original problem. 3 options exist

1) Have one visitorBase with 100 derived concrete visitors. One accept
method in Glyph
2) Have one visitorBase with the 100 concrete visitors grouped in
smaller hierarchies that derive from visitorBase. One accept method in
Glyph
3) Have a family of visitors each with its own baseclass and a
corresponding accept method in glyph(ie multiple accept methods)

The common interface exists for all 100 visitors. It consists of the
visit methods for the various different types of nodes for the glyph
hierarchy.
Thanks

Dec 29 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2359
by: Wavemaker | last post by:
I've been playing with C# v2.0 today, and having quite a bit of fun. The new version has added iterators. The iterators are coded directly into the class to be iterated. For example: public IEnumerable<int> Visit(SomeVisitor visitor) { int position = 0; for(int i = 0; i < Count; i++) {
2
1488
by: Imre Palik | last post by:
Hi, I am trying to create a framework, that automatically generates a base class for the visitor pattern: template <typename param, typename ret = void> struct visitor { typedef ret return_type; virtual return_type visit(param *) = 0;
12
3040
by: FluffyCat | last post by:
New on November 28, 2005 for www.FluffyCat.com PHP 5 Design Pattern Examples - the Visitor Pattern. In the Visitor pattern, one class calls a function in another class and passes an instance of itself. The called class has special functions for each class that can call it. With the visitor pattern, the calling class can have new operations added without being changed itself.
0
1919
by: Siphiuel | last post by:
Hi everyone. When using visitor pattern, we have a nasty dependence on the types of visitable objects that is coded way on top on the visitor hierarchy. i mean, like this: class AbstractVisitor { public: virtual void visit(Object_type_1 *); virtual void visit(Object_type_2 *)
1
2373
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
6527
by: JosAH | last post by:
Greetings, this week we let go of all that algebraic stuff and concentrate a bit more on what object oriented programming is all about. Java claims to support OO, so why not use it? In this week's article we're going to talk a bit about when and why to apply certain patterns. We'll start with the Visitor pattern. The pattern is also named 'double dispatch' which will become clear near the end of this little article. Here's the...
0
11630
weaknessforcats
by: weaknessforcats | last post by:
Design Patterns: Visitor Introduction Polymorphism requires a class hierarchy where the interface to the hierarchy is in the base class. Virtual functions allow derived classes to override base class functions. Applications using polymorphism typically have functions with base class pointers or references as arguments. Then derived objects are created and used as arguments to these functions. Inside the function, only the base class methods...
3
1512
by: C# | last post by:
Dear Freinds, I was going through Stratergy and Visitor patterns. I am really confused. Both look same passing the logic object in to the a business object and the logic object works on the business object. Please clarify. Regards, Design Pattern Learner
3
2314
by: aaragon | last post by:
Hello everyone, I've been trying to work with the visitor design pattern, and it works fine except for the following. Let's suppose that we have a fixed hierarchy of classes (many of them) which I cannot modify. I decided to use the visitor design pattern depending on the actual type of the classes because those classes already support the loki visitor. #include <Loki/Visitor.h>
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7519
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.