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:
“Denotation by means of sounds and markings is a remarkable abstraction. Three letters designate God for me; several lines a million things. How easy becomes the manipulation of the universe here, how evident the concentration of the intellectual world! Language is the dynamics of the spiritual realm. One word of command moves armies; the word liberty entire nations.”
—Novalis [Friedrich Von Hardenberg] (17721801)
“When delicate and feeling souls are separated, there is not a feature in the sky, not a movement of the elements, not an aspiration of the breeze, but hints some cause for a lovers apprehension.”
—Richard Brinsley Sheridan (17511816)