Double Dispatch in JADE 2.0

I've started looking in depth current JADE 2.0 code (it's being massively re-done from scratch, with even a custom .NET DirectX Wrapper), so I'm going to post more frecuently about it's development.

I'm still learning how what's actually done works, but one of it's coolest features is the Pluto framework (which is now branched into a separate project). Pluto allows to develop applications based on pluggable modules, easily configurable via XML. It does a heavy use of generics and reflection to load them, and even includes a virtual file system to abstract from physical storage.

I strongly encourage you to give a look at it, but right now the feature I'm going to talk about is Double Dispatching.

Most languages use single dispatch, which said in a rude manner is like math's least common multiple. At runtime if the code itself doesn't specifies the type, the program chooses it's type and executes it's implementation. In reusable applications this can mean we could not be able to use derived types without being forced to do specific casting and type checks.

Double dispatch works like math's greatest common divisor. By using reflection and polymorphism, a double dispatch implementation looks for the base class of an object, and then goes up, checking every derived class until it finds the most suitable one, the most "complete" one.
With polymorphism this means we can automatically have derived classes methods getting executed without specific casting.

Double dispatch is based on the Visitor pattern, so we could execute any of the derived class methods. We have a more generic code, just by structuring our application according to the mentioned pattern.

JADE 2.0 uses double dispatch in the scene graph, allowing to walk through it's nodes (entites), applying matrix transformations and node-specific operations. This way only common operations are done at the Node class, and each entity's Update() method is called without any need of code. The mechanics are similar to XNA GameComponent's Update() method, which is automatically called in each game loop pass.


Note
: Double dispatch calls the most suitable method according to the object and the first parameter. And Pluto implementation speeds up reflection by generating IL on the fly. Thanks to Vicente for the tip.

Posted by Kartones on 2007-08-18

Comments?

Share via: Twitter Linkedin Google+ Facebook