473,800 Members | 2,332 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
17 2610
Gavin Deane wrote:
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? ;-)


Yup! I suppose you are pointing the problem considered by John Lakos in
his book about bloated interface of sample String class.

I can't count such big number of elements in std::string interface ;-)

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

Mateusz Loskot wrote:
Gavin Deane wrote:
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? ;-)


Yup! I suppose you are pointing the problem considered by John Lakos in
his book about bloated interface of sample String class.


I've not read Lakos's book, but I imagine he's making a similar point
to Herb Sutter here

http://www.gotw.ca/gotw/084.htm
I can't count such big number of elements in std::string interface ;-)


Including overloads, apparently it's 103.

Gavin Deane

Dec 29 '05 #12
Gavin Deane wrote:
Mateusz Loskot wrote:
Gavin Deane wrote:
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? ;-)


Yup! I suppose you are pointing the problem considered by John Lakos in
his book about bloated interface of sample String class.


I've not read Lakos's book, but I imagine he's making a similar point
to Herb Sutter here

http://www.gotw.ca/gotw/084.htm


Yup! That's very similar.
I can't count such big number of elements in std::string interface ;-)


Including overloads, apparently it's 103.


I don't count overloads. Indeed, from your point of
view std::string is a bog one.

Hm, I think I should drop it :-)))

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 29 '05 #13
Merlin wrote:
You will never be the client of a class with 100 member functions.
As Gavin Deane has pointed I'am -> std::string :-)
Talking about your problem, I'd say I don't know all your assumptions so
it's hard to guess.
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.
I know that, but I also suppose that's not much of types.
In other words only a few methods will be in Visitor.
Yes.
BUT there will be 100 derived classes overriding these few methods in
different ways, each specific to the type of operation being carried
out.
Yes, but it's better solution with respect to extensibility and
future changes. IMHO, it's easier to add new subclass than to add new
member function.
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.
Yup.

Now back to my original problem. 3 options exist

1) Have one visitorBase with 100 derived concrete visitors. One
accept method in Glyph
I vote for this one.
2) Have one visitorBase with the 100 concrete
visitors grouped in smaller hierarchies that derive from visitorBase.
One accept method in Glyph
This is a kind of variation of 1) solution. Depending on project
assumptions I'd use this too.
3) Have a family of visitors each with
its own baseclass and a corresponding accept method in glyph(ie
multiple accept methods)
As I said, I feel it's not a good idea :-)

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.


I'd say, it consists of specialized (x 100) implementations of visit
member function but the interface is common.

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Dec 29 '05 #14
Hi Murali
How would u go about using templates in such a circumstance. Can u give
an example. Make one up if mine is not any good for ur purposes. I am
curious in what you have said.

Thanks

Dec 29 '05 #15

Merlin wrote:
Hi Murali
How would u go about using templates in such a circumstance. Can u give
an example. Make one up if mine is not any good for ur purposes. I am
curious in what you have said.

Thanks


Hi Merlin,

I dont have GOF. B'coz of that I could not Understand your Question.
visit this page: http://home.earthlink.net/~huston2/dp/VisitorDemosCpp
an example starts with "// Purpose. Visitor design pattern"

Visitor class is like this..

class Visitor { public:
virtual void visit( This* e ) = 0;
virtual void visit( That* e ) = 0;
virtual void visit( TheOther* e ) = 0;
};

One of Visitor Child class is like this..

class UpVisitor : public Visitor {
/*virtual*/ void visit( This* e ) {
cout << "do Up on " + e->thiss() << '\n'; }
/*virtual*/ void visit( That* e ) {
cout << "do Up on " + e->that() << '\n'; }
/*virtual*/ void visit( TheOther* e ) {
cout << "do Up on " + e->theOther() << '\n'; }
};

and some other classes...

// 1. Add an accept(Visitor) method to the "element" hierarchy
class Element { public:
virtual void accept( class Visitor& v ) = 0;
};

class This : public Element { public:
/*virtual*/ void accept( Visitor& v );
string thiss() { return "This"; }
};

Similarly That class, TheOther classes are defined in the same way.
accept method is overridden so we cannot use templates on that.

/*virtual*/ void This::accept( Visitor& v ) { v.visit( this ); }
/*virtual*/ void That::accept( Visitor& v ) { v.visit( this ); }
/*virtual*/ void TheOther::accep t( Visitor& v ) { v.visit( this ); }

Class hierarchy is this..

Element <- This, That, TheOther
Visitor <- UpVisitor, DownVisitor

but.. visit method is overloaded in UpVisitor & DownVisitor. we can use
templates on that.
Instead of e->thiss(), e->That(), e->TheOTher() calls used in Visit
method, if we create a common method in all classes (This, That &
TheOther), say getName() we can use templates. Other wise there is no
other way. If the Visit method has to behave differently for different
parameters, you have to overload one by one.

Plz let me know if this is near to your Question.

Wish You all Happy New Year,
Murali Krishna.

Dec 31 '05 #16
Merlin,

I worked on this after giving the above example.
I am able to use templates but this solution has a problem. I think
templatizing will not work.
The problem is..
The Elements derived classes also uses Visitor Objects in accept fn. So
we have to use templates. which means we have to define a templatized
method for every type and UpVisitor objects have to be declared for
every type.
ex:- UpVisitor<This> upThis; UpVisitor<That> upThat;
UpVisitor<TheOt her> upTheOther;

which completely deviates the main Idea and requirement.
I think my solution is not correct or I am not able to use templates
porperly.

---
Murali Krishna.

Dec 31 '05 #17
Thanks Murali for trying. I am unlikely to use templates as my visitors
are unlikely to be used on any other structure than the one I am
designing it for. I was just curious to see how you would go about
doing this.

Happy New Year to you too!

Jan 2 '06 #18

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
1490
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
3042
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
6530
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
1514
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
2315
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
9691
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
9551
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
10505
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
10276
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10035
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
9090
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
7580
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
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.