Re: Double Dispatch Problem: Mobile Creatures and Projectiles in a Game World



On Fri, 30 Mar 2007 12:55:27 +0200, Ed Kirwan wrote:

It appears OO has a problems when comparing classes,

Umm, why? Classes are ordered by the <: relation. So you can compare them.
It is not OO problem.

and RTTI in
general. I know of no good solution to your problem, just the visitor;
indeed I experienced it myself when I wrote some interactive fiction.

I had a Verb class and a Thing class. Liquid and Solid were subclasses
of Thing; Take was a subclass of Verb.

When someone took something, I needed the visitor pattern to see what to
do. I don't know how else to do it (without instanceof and other such
unpronouncibles):

That's because in general case multiple dispatch cannot be reduced to
single dispatch. In some specific cases you can simulate it with single
dispatch and re-dispatch, but not always.

In your example it is simple, as you have only Take in the hierarchy of
verbs. Add Drill and it will become impossible.

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
.



Relevant Pages