SE450: Review: OO Mechanisms: Double Dispatch [14/25] Previous pageContentsNext page

The method body depends upon the actual type of two objects.

GoF: "Double-dispatch" simply means the operation that gets executed depends on the [name] of request and the types of two receivers.

interface I {
  public void accept (V z);
}
class C implements I {
  public void accept (V z) { z.visitC(this); }
}  
class D implements I {
  public void accept (V z) { z.visitD(this); }
}  
interface V {
  public void visitC(C x);
  public void visitD(D x);
}
class U implements V {
  public void visitC(C x) { System.out.println("C accepted U"); }
  public void visitD(D x) { System.out.println("D accepted U"); }
}
class W implements V {
  public void visitC(C x) { System.out.println("C accepted W"); }
  public void visitD(D x) { System.out.println("D accepted W"); }
}
class Main {
  public static void main(String[] args) {
    I x = new D();
    V z = new W();
    x.accept(z);
  }
}

GoF: Accept is a double-dispatch operation. Its meaning depends on two types: the Visitor's and the Element's. Double-dispatching lets visitors request different operations on each class of element.

Cecil and other languages support multiple dispatch syntactically.

This is much easier in Cecil!

Previous pageContentsNext page