Attempt at understanding the double-dispatch pattern

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?

1

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 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(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)
    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.


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
  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)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật