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 = ...;


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

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