473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Visitor design pattern - breaking dependence on the target hierarchy

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_ty pe_1 *);
virtual void visit(Object_ty pe_2 *)
............... ..............
virtual void visit(Object_ty pe_N *;
};

That is so nice that visitable objects are aware only of the
AbstractVisitor type, but that's not so good that every concrete
visitor must know about the whole multitude of visitable object types!

I've encountered this problem when trying to separate visitor/visitable
classes into libraries. That is, suppose there is a base library Base,
that contains definitions for AbstractVisitor and some AbstractObject
class, which is the base class for all visitable objects. Pointers to
AbstractObject' s are stored in some STL container inside the Base
library. Objects can be manipulated by visitors only through some
method in the container class:

// ------ Classes contained in the Base library ------
// Abstract.h
class AbstractVisitor ;
class AbstractObject
{
public:
void accept(Abstract Visitor *);
};

// Container.h
#include "AbstractVisito r.h"
#include "AbstractObject .h"

class Container
{
private:
std::set<Abstra ctObject *> objects_;.
public:
void manipulate(Abst ractObject *, AbstractVisitor *);
};

There could be multiple client libraries that have their specific
AbstractObject descendants and visitors, which are run only on the
objects defined in the same library. For instance:

// ------ Classes contained in ClientLibrary1 ------
class ClientObject1
:
public AbstractObject
{
public:
void accept(Abstract Visitor *);
};

class ClientVisitor1
:
public AbstractVisitor
{
public:
// It is desirable that here are contained only methods pertaining to
relevant objects
void visit(ClientObj ect1 *);
// other visit()'s
};

Naturally, it is not good for ClientLibrary1 to know about object types
contained in some ClientLibrary2, for instance.

The aim can be achieved through the use of dynamic_cast'in g, though. In
order to accomplish, we modify the visitor object hierarchy slightly by
inserting a templatized ancestor class:

// ------ Base library code ------
class AbstractVisitor
{
public:
// AbstractVisitor now knows nothing about the objects
// on which it's descendants operate
virtual void visit(void *); // default method
};

template<class TObjectType>
class InterimVisitor
:
virtual public AbstractVisitor
{
public:
virtual void visit(TObjectTy pe *);
};

The concrete visitor classes can now be defined as follows:

// ------ Client library code
class ConcreteVisitor
:
public InterimVisitor< ObjectType1>,
public InterimVisitor< ObjectType2>
{
public:
virtual void visit(ObjectTyp e1 *);
virtual void visit(ObjectTyp e2 *);
};

And finally, the use of dynamic_cast does the right job in overloaded
accept methods:

class SomeObject
:
public AbstractObject
{
public:
void accept(Abstract Visitor * v)
{
Interim<SomeObj ect> * i = dynamic_cast<In terim<SomeObjec t> *>(v);
if (i)
{
// v is allowed to visit this object
v->visit(this);
}
}
};

I'm sorry for including this much of code and statements that you all
probably have seen thousand times :-). And the main question is - are
there any other solutions that do not incur performance penalties
caused by the use of dynamic_cast? Thanks in advance

Mar 7 '06 #1
0 1904

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

Similar topics

18
1961
by: George Sakkis | last post by:
I'm looking for a design to a problem I came across, which goes like this (no, it's not homework): 1. There is a (single inheritance) hierarchy of domain classes, say A<-B<-..<-Z (arrows point to the parent in the inheritance tree). 2. This hierarchy evolved over time to different versions for each class. So for example, version's 1 hierarchy would be A_v1 <-B_v1 <-..<-Z_v1. 3. At runtime, only one version is selected by a factory...
2
2349
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++) {
12
3029
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.
17
2594
by: Merlin | last post by:
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...
25
2474
by: lovecreatesbeauty | last post by:
Could you talk something about the general rules on the interface (function) design in C program that recognized publically? Or introduce some good advice of yourself. How do you think about some of those advices like following? a. keep the interface clean and clear (What does clean or clear mean and how to achieve that?). b. avoid using static variables in local function if possible. c. avoid using global variables for local function...
1
2366
by: RedLars | last post by:
Hi, Given this class definition, public class Node { Node parent; object current; ArrayList children;
1
6511
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
11611
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
2299
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
7993
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7920
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
8401
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...
1
8054
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6730
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 project—planning, coding, testing, and deployment—without 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...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1254
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.