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?
Attempt at understanding the double-dispatch pattern
design patterns;ruby
I've answered the question at SO. This is essentially the same answer as over there.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 calleddentist.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); ...; endendLanguages 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(param)       param.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)    endendclass Adult    def dispatch_work(dentist)      dentist.work_on_adult(self)    endendclass Child    def dispatch_work(dentist)      dentist.work_on_child(self)    endendThe 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.Looking at your gists, your first gist doesn'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  endendclass Beef  def make_dispatch dish    dish.make_with_beef self  endendmodule Dish  def make meat    meat.make_dispatch self  endendclass Sandwich  include Dish  def make_with_chicken chicken    puts Grilled Chicken Sandwich  end  def make_with_beef beef    puts Roast Beef Sandwich  endendclass Stew  include Dish  def make_with_chicken chicken    puts Thai curry  end  def make_with_beef beef    puts Beef stew  endendclass Casserole  include Dish  def make_with_chicken chicken    puts Chicken Pot Pie--or something  end  def make_with_beef beef    puts Shepard's Pie  endendSandwich.new.make(Chicken.new)Stew.new.make(Chicken.new)Casserole.new.make(Beef.new)