In object-oriented programming, delegation refers to evaluating a member of one object in the context of another original object. Delegation can be done explicitly, by passing the sending object to the receiving object, which can be done in any object-oriented language; or implicitly, by the member lookup rules of the language, which requires language support for the feature. Implicit delegation is the fundamental method for behavior reuse in prototype-based programming, corresponding to inheritance in class-based programming. The best-known languages that support delegation at the language level are Self, which incorporates the notion of delegation through its notion of mutable parent slots that are used upon method lookup on self calls, and JavaScript; see JavaScript delegation. The term delegation is also used loosely for various other relationships between objects; see delegation for more. Frequently confused concepts are simply using another object, more precisely referred to as consultation or aggregation; and evaluating a member on one object by evaluating the corresponding member on another object, notably in the context of the receiving object, which is more precisely referred to as forwarding. The delegation pattern is a software design pattern for implementing delegation, though this term is also used loosely for consultation or forwarding.
Overview
This sense of delegation as programming language feature making use of the method lookup rules for dispatching so-called self-calls was defined by Lieberman in his 1986 paper "Using Prototypical Objects to Implement Shared Behavior in Object-Oriented Systems". 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 macOS as a means of customizing the behavior of program components. 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 window manager sends the delegate a windowShouldClose: call, and the delegate can delay the closing of the window, if there is unsaved data represented by the window's contents. Delegation can be characterized as late binding of self: That is, the in a method definition in the receiving object is not statically bound to that object at definition time, but rather at evaluation time, it is bound to the original object. It has been argued that delegation may in some cases be preferred to inheritance to make program code more readable and understandable. Despite explicit delegation being fairly widespread, relatively few major programming languages implement delegation as an alternative model to inheritance. The precise relationship between delegation and inheritance is complicated; some authors consider them equivalent, or one a special case of the other.
Language support for delegation
In languages that support delegation via method lookup rules, method dispatching is defined 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. 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, by contrast, 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. Delegation can be termed "run-time inheritance for specific objects." Here is a pseudocode example in a C#/Java like language: class A ; class B ; a = new A; b = new B; // establish delegation between two objects
Calling will result in b.bar being printed, since refers to the original receiver object,, within the context of. The resulting ambiguity of is referred to as object schizophrenia. Translating the implicit into an explicit parameter, the call translates to, using the type of for method resolution, but the delegating object for the argument. Using inheritance, the analogous code is: class A ; class B extends A ; b = new B;
Calling will result in B.bar. In this case, is unambiguous: there is a single object,, and resolves to the method on the subclass. Programming languages in general do not support this unusual form of delegation as a language concept, but there are a few exceptions.
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
This calls for additional rules for method lookup, as there are now potentially two methods that can be denoted as the most specific.
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 claiming to use delegation. This should not be considered cheating, merely the plural definitions of what delegation means. More recently work has also been done on distributing delegation, so e.g. clients of a search engine 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.