I've been trying to grok the double-dispatch pattern and having a hard time. I finally attempted a sample program to help myself understand. Here's the gist. But then I decided to attempt it without Double dispatch and the solution didn't look any more terrible than usual. What am I doing wrong?
Edit: as per suggestion, I've posted this question here. Keeping this link around for redirects.
In single dispatch---what you see in most modern OO languages---a method is dispatched based on the run-time type of a single object. This shows up as the dot operator (in ruby, java, javascript, etc.) or the arrow operator (perl, c++).
# look, ma single dispatch!
# method on obj's run-time type that is called
dentist.work_on(patient)
Double dispatch, then, would be based on the run-time type of two objects. There are a few ways this could look; and on which object should the method live?
# Hmm, this looks weird.
# Is the method in to dentist.class or patient.class?
(dentist, patient).do_dentistry()
# okay, this looks more familiar; the method lives on obj1.class
# This only works in static-typed languages which support double dispatch
# in which you declare the type of the method parameters.
dentist.work_on(patient)
class Dentist
def work_on(Adult patient); ...; end
def work_on(Child patient); ...; end
end
Languages like groovy that have multiple dispatch generalize the second example above; they consider the run-time types of all parameters when choosing which method to run. See for example this blog post about groovy and multiple dispatch.
Most modern OO languages only have single dispatch and the Multiple Dispatch Pattern is an attempt to get the benefits of multiple dispatch into the language. It even works for dynamic languages like ruby. It works by doing single dispatch twice in a row. The first method call will call a method on the second object.
class Dentist
def work_on(patient)
patient.dispatch_work(self)
end
def work_on_adult(patient)
drill_as_hard_as_you_can(patient)
end
def work_on_child(patient)
use_bubble_gum_toothpaste(patient)
give_toothbrush_to(patient)
end
end
class Doctor
def work_on(patient)
patient.dispatch_work(self)
end
def work_on_adult(patient)
do_checkup(patient)
end
def work_on_child(patient)
assure_presence_of(patient.guardian)
ask_questions_to(patient.guardian)
do_checkup(patient)
give_cheap_toy_to(patient)
end
end
class Adult
def dispatch_work(dentist)
dentist.work_on_adult(self)
end
end
class Child
def dispatch_work(dentist)
dentist.work_on_child(self)
end
end
The double dispatch pattern is what I call a low-level pattern because other patterns are built on it. For example, the Visitor pattern relies heavily on the double dispatch pattern.
Update Just saw your gists. Your first gist isn't really doing double dispatch. Sure, you're dispatching twice, but you're not changing the behavior in that second dispatch. To change it to double dispatch I'd do something like this.
class Chicken
def make_dispatch dish
dish.make_with_chicken self
end
end
class Beef
def make_dispatch dish
dish.make_with_beef self
end
end
module Dish
def make meat
meat.make_dispatch self
end
end
class Sandwich
include Dish
def make_with_chicken chicken
puts "Grilled Chicken Sandwich"
end
def make_with_beef beef
puts "Roast Beef Sandwich"
end
end
class Stew
include Dish
def make_with_chicken chicken
puts "Thai curry"
end
def make_with_beef beef
puts "Beef stew"
end
end
class Casserole
include Dish
def make_with_chicken chicken
puts "Chicken Pot Pie--or something"
end
def make_with_beef beef
puts "Shepard's Pie"
end
end
Sandwich.new.make(Chicken.new)
Stew.new.make(Chicken.new)
Casserole.new.make(Beef.new)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With