Language Feature
The short definition is that delegation defines method dispatching the way it is defined for virtual methods in inheritance: It is always the most specific method that is chosen during method-lookup. Hence it is the original receiver entity that is the start of method lookup even though it has passed on control to some other object (through a delegation link, not an object reference). Delegation has the advantage that it can take place at run-time and affect only a subset of entities of some type and can even be removed at run-time. Inheritance on the other hand typically targets the type rather than the instances and is restricted to compile time. On the other hand, inheritance can be statically type-checked while delegation generally cannot without generics (G. Kniesel has shown that a restricted version of delegation can be statically typesafe). Delegation can be termed "run-time inheritance for specific objects".
Example in a C#/Java like language
class A { void foo { // "this" also known under the names "current", "me" and "self" in other languages this.bar; } void bar { print("a.bar"); } }; class B { private A a; // delegation link public B(A a) { this.a = a; } void foo { a.foo; // call foo on the a-instance } void bar { print("b.bar"); } }; a = new A; b = new B(a); // establish delegation between two objectsCalling b.foo
will result in a.bar being printed, since class B
"delegates" the method foo
to a given object of class A
.
Programming languages in general do not support delegation as a language concept, but there are a few exceptions, most notably ECMAScript. Self, Kniesels Lava, and the Tcl object system Snit also support delegation. Lava uses an explicit delegation link that can never be null, and can never change during an object's lifetime. Self has the notion of explicit parent slots that can change at run-time. As there were several parent slots, essentially Self has multiple inheritance. As with dual inheritance (described below) this entails a carefully designed method-lookup scheme.
Read more about this topic: Delegation (programming)
Famous quotes containing the words language and/or feature:
“Our language has wisely sensed these two sides of mans being alone. It has created the word loneliness to express the pain of being alone. And it has created the word solitude to express the glory of being alone. Although, in daily life, we do not always distinguish these words, we should do so consistently and thus deepen our understanding of our human predicament.”
—Paul Tillich (18861965)
“Columbus stood in his age as the pioneer of progress and enlightenment. The system of universal education is in our age the most prominent and salutary feature of the spirit of enlightenment, and it is peculiarly appropriate that the schools be made by the people the center of the days demonstration. Let the national flag float over every schoolhouse in the country and the exercises be such as shall impress upon our youth the patriotic duties of American citizenship.”
—Benjamin Harrison (18331901)