Student Originated Software 1997-1998
Fall Quarter

A Software Engineering Course at
The Evergreen State College


Object Oriented Programming

About Double Dispatching

Double dispatching is a useful technique for efficiently choosing an algorithm
based on the classes of one or more of the arguments of a message as well
as the class of the receiver. For example, we might wish to evaluate the
following expression:
    3 + 4.0
When the SmallInteger 3 receives the message #+, it may have to choose
among algorithms designed for Integers, Fractions, and Floats. The fastest
way to do this is for it to simply send the argument of the message another
message--as shown here:
    + aNumber
    <primitive: 1>
    ^aNumber sumFromInteger: self
If we replace variable names with values, this translates to:
    4.0 sumFromInteger: 3
Since the float knows that the argument must be an Integer, it knows immediately
what must be done to add itself to the integer. Note that the order of the numbers
has changed, so that in methods such as Fraction>>quotientFromInteger:, it is the
argument that is divided by the receiver, and not the receiver divided by the argument

In the system as delivered, there are five operations that are subject to
double-dispatching:

  •  + (addition)
    • For addition, the dispatch operator takes the form #sumFrom[a class name]:
  •  - (subtraction)
    • For subtraction, the dispatch operator takes the form #differenceFrom[a class name]:
  •  * (multiplication)
    • For multiplication, the dispatch operator takes the form #productFrom[a class name]:
  •  / (division)
    • For division, the dispatch operator takes the form #quotientFrom[a class name]:
  •  < (comparison--the rest of the comparison operators can be derived)
    • For comparison, the dispatch operator takes the form #lessFrom[a class name]:


For more information contact
[ Evergreen Home Page | Academic Programs ]


Created by: SoSwEbGrOuP
E-mail: ringert@evergreen.edu