or

Single, Dynamic, Multiple and Double Dispatching

Today I was reading couple articles about programming languages and I noticed that there is still some confusion about dispatching. So I’ll try to explain as much as I can. Everbody is welcome to correct!

What is Dispatching?

This is also just another OOP term. It’s not valid outside of the OOP context. I’m not gonna dive into linguistic details. Basically, it’s an invocation of a function on a specific type as in object.function(with params). For more details you can #wiki it. There are 3 types of dispatching mechanism:

  • Single/Dynamic Dispatching
  • Multiple Dispatching
  • Double Dispatching

You might also find 2 other topics related to this subject, but I’m not gonna cover them today. You can quickly #wiki it: Monkey Patching and Duck Typing. Anyways, so let’s get back to our subject:

Single/Dynamic Dispatching

Although there is a subtle difference between them, I covered these 2 topics under the same title. So basically, when you invoke a function on a type, it’s called single dispatch as in:

    class Messenger {
        public void send(String message) { }
    }
Messenger messenger = new Messenger(); messenger.send("Hello World");

If there is no polymorphic structure, compiler knows this at compile time. There can be multiple send functions under different types (classes). Compiler binds them to the correct ones in the compile time:

    class Messenger {
        public void send(String message) { }
    }
class SMSGateway { public void send(String sms) { } }
Messenger messenger = new Messenger(); messenger.send("Hello World");
SMSGateway gateway = new SMSGateway(); gateway.send("Cya in 10!");

In above example, compiler bound send(message) to Messenger and send(sms) to SMSGateway types. These are all single dispatch examples. No dynamic dispatching needed so far. However, if you have a polymorphic structure as in:

    interface Messenger {
        void send(String message);
    }
class TextMessenger implements Messenger { public void send(String message) { System.out.println("TEXT: " + message); } }
class XMLMessenger implements Messenger { public void send(String message) { System.out.println("" + message + ""); } }
class Demo { public void useMessenger(Messenger messenger) { messenger.send("Which messenger am I using?"); } }

As you may quickly notice, this requires dynamic dispatching (run-time decision) since compiler cannot decide the actual type of the messenger which Demo.useMessenger uses. Lots of known design patterns are already based on this mechanism (Strategy, Bridge, etc..) Again I’m not going go into details, you can find more information about this on #wiki: Single/Dynamic Dispatch and #c2wiki: Single Dispatch / Dynamic Dispatch

Multiple Dispatching

Roughly it is the idea behind overloading in most programming languages. Your type has so many functions with the same name, but they all have different parameters.

    class Messenger {
        public void send(String message) { }
        public void send(String message, Person to) { }
        public void send(String message, String subject, Person to) { }
    }

You can figure out how it would work. However multiple dispatching is not just about this. I guess overloading example is misleading too. Anyways, let’s consider following example:

    interface Messenger {
        void send(String message);
    }
    class TextMessenger implements Messenger {
        public void send(String message) {
            System.out.println("TEXT: " + message);
        }
    }
class XMLMessenger implements Messenger { public void send(String message) { System.out.println("" + message + ""); } } class Demo { public void useMessenger(Messenger messenger) { messenger.send("Which messenger am I using?"); } public void useMessenger(TextMessenger messenger) { messenger.send("I'm TextMessenger for sure, but?"); } }

So what do you think will happen when I try to invoke Demo.useMessenger(TextMessenger). If your language doesn’t support true multiple dispatch mechanism, you’ll always end up invoking useMessenger(Messenger) function no matter what you provide. With true multiple dispatching, you’ll invoke useMessenger(TextMessenger) when you provide TextMessenger to the function. More information, again use #wiki Multiple Dispatch or #c2wiki: Multiple Dispatch BTW, #Java doesn’t support true multiple dispatching, so don’t cause any bugs while creating overloaded methods.

Double Dispatching

Well this one is pretty cool. Some define this as true dispatching :) Anyways, to me all of them are true :) In real world, all relations are actually two-way. It’s not only you’re telling somebody about something, he/she is also listening to you. Double dispatching is like this: two-way. Visitor pattern is actually good way of explaining this topic. Let’s look at the following example:

    // messenger
    interface Messenger {
        void send(Message message);
    }
    class TextMessenger implements Messenger {
        public void send(Message message) {
            // some custom operations
            message.print(this);
        }
    }
    class XMLMessenger implements Messenger {
        public void send(Message message) {
            // some other custom operations
            message.print(this);
        }
    }
    // and message
    class Message {
        public void print(TextMessenger messenger) {
            // do something
        }
        public void print(XMLMessenger messenger) {
            // do some other thing
        }
    }

As you can easily see, objects are related to each-other both ways. Message is deciding to do something based on other types.

Hope this clarifies things a bit more.



5 Responses

  1. ynov

    Single/Dynamic Dispatching

    isn’t this the same as static/dynamic linking?

    March 23, 2010, 12:00 PM
  2. Isa Goksu

    U can think of that as well. Dispatching = Linking, Typing = Binding :)

    March 25, 2010, 4:28 PM
  3. halil

    isa abi bana yardımcı olurmusun ben pro player tam kulanamıyorum kendi logmu vide oalrın açılmadan önüne koymak istiyorum ama olmuyo bana yardımcı olurmusun lütfen yardımların için teşekur ediyorum şimdiden ..

    May 23, 2010, 4:04 PM
  4. Jp

    It wont play the video after I put the copied URL code it. What should I do? I tried everything! Its seems simple but for some reason it isn’t working

    December 9, 2010, 4:01 AM
  5. Maniganda Prakash

    In the last para of ‘Multiple dispatching’, when you say ‘Demo.useMessenger(TextMessenger)’, it might be mistaken that ‘new TextMessenger()’ is passed, so additionally you should add one more line where you say ‘new TextMessenger()’ is assigned to ‘Messenger messenger’ and ‘messenger’ is passed, otherwise it is little confusing.

    January 1, 2011, 11:40 PM

Leave a Reply

Name (required)
Mail (required)
Website