Delegation (programming)
|
In object-oriented programming there are two notions of delegation. In older times delegation was referring to passing on the execution to some other object. In recent times however, it means the method lookup rules when dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems" featuring the elephant Clyde. Despite the mature age of the Liebermann paper, delegation has been observed to be used in a "wrong" fashion as late as mid 1990s. Below is an example of the old definition.
Contents |
Old definition example
It is a very simple yet very powerful concept: Handing a task over to another part of the program. In OO it is used to describe the situation wherein an object passes a task to another object. Generally today, however, this mechanism is widely known as aggregation, consultation or forwarding.
In a C++ like language say there is a class A defined as...
class A { public: foo() { print("Object A doing the job"); } }
Now if there is another class B defined as...
class B { public: A a; foo() { a.foo(); } }
... any object of class B will delegate the execution of its function foo to an object of class A.
Modern definition
The short definition is that delegation defines method dispatching like how it is defined for virtual methods in inheritance: It is always the most specific method which is chosen during method-lookup --- Hence it is the original receiver entity which 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 cannot (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/C# like language
class A { foo() { self.bar() // self is also known under the names "current" and "this" in other languages } bar() { print("a.bar") } }
class B { delegationlink A a foo() { a.foo() // call foo() on the a-instance } bar() { print("b.bar") } }
a = new A() b = new B(a) // establish delegation between two objects
calling b.foo()
will result in b.bar to be printed on the screen. Using the old definition the result would have been a.bar
It should be noted that support for delegation as a language concept is generally not available in programming languages. A few exceptions exists though, for example the languages Self and Kniesels Lava. Lava utilizes an explicit delegation link which can never be null, and can never change during an objects lifetime. Self has the notion of explicit parent slots which 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.
Dual inheritance
If the language supports both delegation and inheritance one can do dual inheritance by utilizing both mechanisms at the same time as in
class C extends A { delegationlink D d }
This calls for additional rules for method lookup, as there are now potentially two methods which can be denoted as the most specific (due to the two lookup paths).
Related areas
Delegation can be described as a low level mechanism for sharing code and data between entities. Thus it builds the foundation for other language constructs. Notably Role-Oriented Programming languages have been utilizing delegation but especially the older ones factually used aggregation while stating the use of delegation. This should not be considered cheating, merely the plural definitions of what delegation means (as described above).
In recent time however work has also been carried out in distributing delegation, so e.g. clients of a search engine (finding cheap hotel rooms) can use a shared entity using delegation to share best hits and general re-useable functionality.
Delegation has also been suggested for advice resolution in aspect-oriented programming by Ernst and Lorenz in 2003.
Delegation is a fundamental technique used in languages of prototype-based programming (such as JavaScript)
References
- Henry Lieberman: Using prototypical objects to implement shared behavior in object-oriented systems. In: Conference proceedings on Object-oriented programming systems, languages and applications. Portland 1986, p. 214-223 ISSN 0362-1340 (Online at MIT Labs (http://lcs.www.media.mit.edu/people/lieber/Lieberary/OOP/Delegation/Delegation.html)) - easy to read and understand. In fact its quite entertaining! The famous elephant Clyde ought to be known to any programmer!
- Lynn Andrea Stein, Henry Liberman, David Ungar: A shared view of sharing: The Treaty of Orlando. In: Won Kim, Frederick H. Lochovsky (Eds.): Object-Oriented Concepts, Databases, and Applications ACM Press, New York 1989, ch. 3, pp. 31-48 ISBN 0-201-14410-7 (online at Citeseer (http://citeseer.ist.psu.edu/stein89shared.html)) - formal and somewhat boring, but denotes a 'document of peace' where the war between delegation and inheritance was buried
External links
- The Darwin Project - The Lava programming language and general articles on delegation (http://javalab.cs.uni-bonn.de/research/darwin/)