Welcome! Log In Create A New Profile


Visitor Pattern

Posted by slow_eddy 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
avatar Visitor Pattern
May 07, 2012 07:16PM
For what it's worth, I reckon the example of Visitor pattern they gave us in the notes is wrong. It's certainly different to what you'll find on "the usual culprits" sites online, with that specialization of the accept() function in the object structure.

If you track the "traditional" examples you'll see they work better. I'm imagining the client would have objects of (super)class Element and Visitor, and these can be assigned pointers to subtypes (by the substitutibility of subtypes, according to our good friend Xiaoping Jia). To the methods of these you just pass the right kind of pointer, in turn. In other words in the client code (as I understand it), you would choose your operation: "Sort" or "BehaveAggressively" etc. (with a generic accept() method) but from there on you can rely on the properly set up Visitor pattern to do the rest for you.

How? Well accept(Visitor* v) always calls the same line: v -> visit(this), so regardless of what this turns out to be you'll be using ordinary old function name overloading when your code scuttles back over to Visitor to scuttle back over to this to do its long-handed operation. You've passed the right parameter in a pretty foolproof way.

Or am I wrong?
Sorry, only registered users may post in this forum.

Click here to login