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



Aaron J. M. wrote:


But how would a Collision check whether the colliding Mobiles are one
of each
without downcasting, double dispatch, or the Visitor pattern?

I might end up doing the Visitor pattern, but I want to put it off for
now to
see if I can find an easier solution.


It appears OO has a problems when comparing classes, 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):

Thing extends Visitor {
void execute(Thing thing) {
thing.visit(this);

void accept(Liquid liquid) {
System.out.println("The " + liquid + " seeps through your fingers.");
}

void accept(Solid solid) {
System.out.println("You take the " + solid);
}
}

class Solid extends Thing {
void visit(Verb verb) {
verb.accept(this);
}
}

etc.

I'm trying to get my head around the good Mister Lahman's suggestion from your link that, "If some types of active
tokens are limited in what they can do with some types of inanimate
objects, that has to be expressed in the relationships properly. "

I'd love if that line held the answer for me ...

(OT: I can never remember which takes the accept() and which takes the visit(); must dust off GOF again.)

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.

Download Fractality, free Java code analyzer:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html
.



Relevant Pages

  • Re: When and where to use Visitor Pattern?
    ... Visitor pattern allows exactly that. ... > abstract void visitIntExp; ... >class IntExp extends Expression ...
    (comp.object)
  • Re: Static vs Dynamic
    ... >> Do you have an example of a good OO design that Java forces you to ... > static void m{ ... that's not an implementation of the Visitor Pattern. ... implement the visitor pattern in Lisp because of run-time dispatching, ...
    (comp.lang.lisp)
  • Re: "virtual pair"
    ... Valery wrote: ... > struct S; ... The Visitor Pattern would be my first choice to consider to implement ... void go1{ ...
    (comp.lang.cpp)
  • Re: Cast best practice question
    ... Yes the visitor pattern part doesn't involve any cast. ... Visitor pattern just for expaining why I didn't create a struct but a vector. ... void accept{ ...
    (microsoft.public.vc.language)
  • Re: Abstract public member variales?
    ... How did you use the Visitor pattern and what went wrong? ... I'm still not certain that your adventure game is the same type as I ... game: Verb and Thing. ... public void visit{ ...
    (comp.object)