Delegation (programming)

From Wikipedia, the free encyclopedia

Jump to: navigation, search

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 modelling.

Interestingly, despite delegation being fairly widespread, relatively few 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 handing 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 a wrapper object doesn't pass itself to the wrapped object[1]).

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.[2] 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", "me" 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 being printed. Using the old definition would have resulted in a.bar being printed. Calling

a.foo()

will result in a.bar being printed.

Programming languages in general do not support delegation as a language concept, but there are a few exceptions: Self, Kniesels Lava, and the Tcl object system Snit all support delegation. 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-usable 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

  1. ^ [Gamma98, p.20]
  2. ^ Apple (2006-12-20). "Cocoa Fundamentals Guide: Delegates and Data Sources". Apple Developer Connection. http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html. Retrieved on 2007-03-25. 
  • 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)
  • 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)

[edit] See also

[edit] External links

Personal tools