views:

57

answers:

1

lets say i have a method which has two parameters. i have been implementing them as:

if(aObj instance of Marble)   {
   if(bObj instance of Bomb)   {
      this.resolve((Marble)aObj,(Bomb)bObj);
   }
}

as you can see its not a very pretty solution. i plan to implement using double dispatching, but with two parameters which both need double dispatching, im afraid im a bit stumped. any ideas please.

im implementing in java btw.

+1  A: 

If possible I would go with the visitor pattern.

That is, the class that defines the method, (or rather, the method*s* for each type) implements an interface called visitor. Instead of doing instance-of checks, you then call object.accept(this). The object then calls the correct "visit"-method of the visitor.

aioobe
yeah. the problem is that the visited object calls the method from the visitor. the problem is that the method in the visitor needs to parameters, the another parameter needs also to implement the visitor pattern..
mixm
I see. I guess you could let one of the arguments implement the visitor interface as well, and simply resolve the other argument (by visiting it) and then visit the original visitor.
aioobe
Have a look at http://sourcemaking.com/design_patterns/visitor/java/2
aioobe