Delegation (programming)
From Wikipedia, the free encyclopedia
In object-oriented programming there are two related notions of delegation.
- Most commonly, it refers to a programming language feature making use of the method lookup rules for dispatching so-called self-calls as defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems".
- In its original usage, delegation refers to one object relying upon another to provide a specified set of functionalities. In research, this is often referred to as consultation or as aggregation in modeling.
Interestingly, despite delegation being fairly widespread, no major programming languages implement delegation as an alternative model to static inheritance. The Self programming language incorporates the notion of delegation through its notion of mutable parent slots which are used upon method lookup on self calls.
Contents |
[edit] As a design pattern
Delegation is the simple yet powerful concept of handling a task over to another part of the program. In object-oriented programming it is used to describe the situation wherein one object defers a task to another object, known as the delegate. This mechanism is sometimes referred to as aggregation, consultation or forwarding (when wrapper object doesn't pass itself to wrapped object [Gamma98, p.20]).
For example, consider a C++-like language in which a class A is defined as:
class A { public: virtual void foo() { printf("Object A doing the job."); } };
Now if there is another class B defined as:
class B { public: A a; virtual void foo() { a.foo(); } };
Any object of class B will delegate the execution of its function foo to an object of class A. To make this more useful, we could add a means to set the delegate which a given instance of B is using:
class B { public: B(A* new_a) : a(new_a) {} A* a; void foo() { a->foo(); } };
Now, one can change the behavior of objects of class B without having to change the code of class B itself, by deriving new delegates from A, such as:
class AA : public A { public: virtual void foo() { printf("AA at work."); } };
One can then create two instances of class B with different behavior:
B b1(new A); B b2(new AA);
b1.foo() and b2.foo() will produce different results.
Delegation is dependent upon dynamic binding, as it requires that a given method call can invoke different segments of code at runtime. It is used throughout Mac OS X (and its predecessor NeXTStep) as a means of customizing the behavior of program components [1]. It enables implementations such as making use of a single OS-provided class to manage windows because the class takes a delegate that is program-specific and can override default behavior as needed. For instance, when the user clicks the close box, the delegate is sent a windowShouldClose: call, and the delegate can delay the closing of the window if there is unsaved data represented by the window's contents.
[edit] As a 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 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 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 Java/C# like language
class A { void foo() { this.bar() // "this" is also known under the names "current" and "self" in other languages } void bar() { print("a.bar") } }
class B { delegationlink 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 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. Calling a.foo()
will result in a.bar to be printed on the screen.
Programming languages in general do not support delegation as a language concept. A few exceptions exist though, for example the languages Self and Kniesels Lava, as well as the Tcl object system Snit. Lava uses an explicit delegation link which can never be null, and can never change during an object's 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.
[edit] 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). This is elaborated in K. Graversen's Ph.D. thesis on roles.
[edit] 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)
[edit] 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) - easy to read and understand.
- 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) - formal and somewhat boring, but denotes a 'document of peace' where the war between delegation and inheritance was buried