What is delegation in Java? Can anyone give me a proper example?
Asked
Active
Viewed 2.9k times
4 Answers
49
That's delegation - exactly like in the real world:
public interface Worker() {
public Result work();
}
public class Secretary() implements Worker {
public Result work() {
Result myResult = new Result();
return myResult;
}
}
public class Boss() implements Worker {
private Secretary secretary;
public Result work() {
if (secretary == null) {
// no secretary - nothing get's done
return null;
}
return secretary.work();
}
public void setSecretary(Secretary secretary) {
this.secretary = secretary;
}
}
(Added Worker interface to get closer to the Delegator pattern)

Andreas Dolk
- 113,398
- 19
- 180
- 268
-
Should an object of `private Secretary secretary = new secretary();` be created to call `return secretary.work();`? – gbox May 12 '20 at 06:22
-
1@gbox Sure. The Secretary has to be hired by HR and `@inject`ed into the Boss's office :) – Andreas Dolk May 23 '20 at 11:59
19
If you're referring to the delegation pattern, wikipedia has a great example, written in java.
I believe the longer example of the page above is the best one:
interface I {
void f();
void g();
}
class A implements I {
public void f() { System.out.println("A: doing f()"); }
public void g() { System.out.println("A: doing g()"); }
}
class B implements I {
public void f() { System.out.println("B: doing f()"); }
public void g() { System.out.println("B: doing g()"); }
}
class C implements I {
// delegation
I i = new A();
public void f() { i.f(); }
public void g() { i.g(); }
// normal attributes
void toA() { i = new A(); }
void toB() { i = new B(); }
}
public class Main {
public static void main(String[] args) {
C c = new C();
c.f(); // output: A: doing f()
c.g(); // output: A: doing g()
c.toB();
c.f(); // output: B: doing f()
c.g(); // output: B: doing g()
}
}

aioobe
- 413,195
- 112
- 811
- 826
-
thank you! A benefit of Delegation over inheritance is the runtime change, but all the examples I could come up with resembled composition more than delegation. This example clearly conveys what the authors of articles I have read meant. – Chaitanya Apr 22 '12 at 04:39
-
1This answer should be marked as the best one, because it's describing the principle of delegation best! – Train Jul 25 '18 at 20:06
3
Same example as aioobe but changed the class names to more intuitive ones. Deriving analogy to real world examples.
public static void main(String[] args) {
Boss boss = new Boss();
boss.toDeveloper();
boss.f();
boss.g();
boss.toSrDeveloper();
boss.f();
boss.g();
}
interface I {
void f();
void g();
}
class Developer implements I {
public void f() {
System.out.println("Developer: f() is too hard for me.");
}
public void g() {
System.out.println("Developer: g() is not in my domain.");
}
}
class SrDeveloper implements I {
public void f() {
System.out.println("Sr. Developer: Okay, I'll see f()");
}
public void g() {
System.out.println("Sr. Developer: I'll do g() too.");
}
}
class Boss implements I {
// delegation
I i;
public void f() {
i.f();
}
public void g() {
i.g();
}
void toDeveloper() {
i = new Developer();
}
void toSrDeveloper() {
i = new SrDeveloper();
}
}
0
Here is a simple example of how Delegation is used:
interface IDogBehaviour {
public void doThis();
}
class BarkSound implements IDogBehaviour {
public void doThis() {
System.out.println("Bark!");
}
}
class WagTail implements IDogBehaviour {
public void doThis() {
System.out.println("Wag your Tail!");
}
}
class Dog {
private IDogBehaviour sound = new BarkSound();
public void doThis() {
this.sound.doThis();
}
public void setNewBehaviour( IDogBehaviour newDo ){
this.sound = newDo;
}
}
class DelegationDemo {
public static void main( String args[] ){
Dog d = new Dog();
//delegation
d.doThis();
//change to a new behaviour type - wag tail
IDogBehaviour wag = new WagTail();
d.setNewBehaviour( wag );
//Delegation
d.doThis();
}
}

t j
- 7,026
- 12
- 46
- 66

Jolly Good
- 452
- 7
- 17