Introduction to OOP Chapter 24: Design Patterns : next previous audio real text

Double Dispatch (Multiple Polymorphism)

Problem: You have variation in two or more polymorphic variables.

Solution: Make each a receiver in turn, each message ties down one source of variation.

Example, suppose we have a hierarchy of Shapes (Triangle, Square) and Device (Printer, Terminal). Two variables, one a Shape and one a Device.

First, pass a message to the device, passing the shape as argument:

Shape aShape = ... ;
Device aDevice = ...;

aDevice.display(aShape);

function Printer.display (Shape aShape)
begin
	aShape.displayOnPrinter (self);
end;

function Terminal.display (Shape aShape)
begin
	aShape.displayOnTerminal (self);
end;
One message fixes the device, but how to fix the shape?
Intro OOP, Chapter 24, Slide 14