/ Published in: Java
In software engineering, the delegation pattern is a design pattern in object-oriented programming where an object, instead of performing one of its stated tasks, delegates that task to an associated helper object.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
interface I { void f(); void g(); } class A implements I { } class B implements I { } // changing the implementing object in run-time (normally done in compile time) class C implements I { I i = null; // delegation public C(I i){ setI(i); } public void f() { i.f(); } public void g() { i.g(); } // normal attributes public void setI(I i) { this.i = i; } } public class Main { C c = new C(new A()); c.f(); // output: A: doing f() c.g(); // output: A: doing g() c.setI(new B()); c.f(); // output: B: doing f() c.g(); // output: B: doing g() } }
URL: https://en.wikipedia.org/wiki/Helper_object