SE450: Review: OO Mechanisms: Delegation [15/25] Previous pageContentsNext page

(Partial) responsibility for a message is given to another object.

The receiving object forwards messages to another object, perhaps doing some processing before and after the call.

class C { public void m() { System.out.println("C.m"); }
class D {
  C z = new C();
  public void f() { z.m(); System.out.println("D.f"); }
  public void m() { z.m(); }  // pure delegation
class Main {
  public static void main(String[] args) {
    D x = new D();
    x.f(); // prints "C.m D.f"
    x.m(); // prints "C.m"

Note that delegation is often referred to as composition. This is different from the strict sense of composition used in the UML.

Previous pageContentsNext page