开发者

method with two parameters which both need to be double dispatched

开发者 https://www.devze.com 2022-12-31 01:07 出处:网络
lets say i have a method which ha开发者_开发百科s two parameters. i have been implementing them as:

lets say i have a method which ha开发者_开发百科s 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.


If possible I would go with the visitor pattern.

That is, the class that defines the method, (or rather, the methods 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消