Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: doubts about visitor pattern

I know what Visitor Pattern is and how to use it; this question is not a duplicate of this one.


I've got a library where I put most of the reusable code I write, and which I link to most of my projects.

Often I need to add features to some classes, but without adding these new features to the library. Let me use a real example:

In this lib I've got a class Shape, inherited by CircleShape, PolygonShape and CompositeShape.

I'm now developing a graphical application where I need to render these Shape, but don't want to put a virtual function render in the core Shape class, since some of my projects which use Shape don't do any rendering, and other graphical projects could use different rendering engines (I'm using Qt for this project, but for a game I'd use OpenGL, thus the render function will need different implementations).

The most famous way to do this is using Visitor Pattern, of course, but this pops a few doubts into my mind:

Any class of any library could need to be extended as my Shape does. Most of the public libraries (about all of them) don't provide any support for Visitor Pattern; why? why should I?

Visitor Pattern is a way to simulate Double Dispatching in C++. It's not native in C++, and requires to be explicitly implemented, making the class interface more complex: I don't think the applyVisitor function should be at the same level of my class' functions, I see this like breaking abstraction.

Explicitly up-casting Shape with dynamic_cast is more expensive, but to me it looks like a cleaner solution.


So, what should I do? Implementing Double Dispatching in all my library classes? What if the library providing Shape wasn't mine, but some GPL library found on the internet?

like image 668
peoro Avatar asked Nov 14 '10 12:11

peoro


People also ask

What's the point of visitor pattern?

The purpose of a Visitor pattern is to define a new operation without introducing the modifications to an existing object structure. Imagine that we have a composite object which consists of components.

In which scenario would you use the visitor pattern?

The visitor pattern is used when: Similar operations have to be performed on objects of different types grouped in a structure (a collection or a more complex structure). There are many distinct and unrelated operations needed to be performed.

How many participants are there in the visitor pattern?

There are two main participants required to complete the visitor pattern (not including the client code): The elements that have an accept method (by convention we name the method "accept" ) The visitors that define the visit method.

Is the visitor pattern an Antipattern?

Double dispatch is rarely found in mainstream OOP languages (C# has support for that using dynamic type). One more time, the Visitor pattern is a way to simulate the lack of support for double dispatch. It is another anti-OOP design pattern.


1 Answers

First: "Visitor Pattern is a way to simulate Double Dispatching in C++." This is, erm, not fully right. Actually, double dispatch is one form of multiple dispatch, which is a way to simulate (the missing) multi-methods in C++.


Whether operations on a class hierarchy should be implemented by adding virtual functions or by adding visitors is determined by the probabilities of adding classes vs. adding operations:

  • If the number of classes keeps changing more rapidly than the number of operations, use virtual functions. That is because adding a class requires modifying all visitors.
  • If the number of classes is relatively stable compared to the number of operations, use visitors. That is because adding a virtual function requires changing all classes in the hierarchy.

Yes, many libraries do not come with a visitor interface.
When we are just looking at the reasoning above, this would be right if the number of classes changes often. That is, if a library is released often, with new classes being added constantly, then providing a visitor interface wouldn't make much sense, because each time a new release brings new classes, everybody using the library need to adapt all of their visitors. So if we only looked at the above reasoning, a visitor interface would seem to only be helpful if the number of classes in a lib's class hierarchy seldom or never changes.

However, with 3rd-party libraries there's another aspect to that: Usually, users cannot change the classes in the library. That is, if they need to add an operation, the only way they can do this is by adding a visitor - if the library provides the hooks for them to plug into it.
So if you are writing a library and feel like users should be able to add operations to it, then you need to provide a way for them to plug their visitors into your lib.

like image 149
sbi Avatar answered Oct 21 '22 08:10

sbi