my-server
← Wiki

Delegation pattern

In software engineering, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same code reuse as inheritance.

In delegation, an object handles a request by delegating to a second object (the delegate). The delegate is a helper object, but with the original context. With language-level support for delegation, this is done implicitly by having <code>self</code> in the delegate refer to the original (sending) object, not the delegate (receiving object). In the delegate pattern, this is instead accomplished by explicitly passing the original object to the delegate, as an argument to a method. "Delegation" is often used loosely to refer to the distinct concept of forwarding, where the sending object simply uses the corresponding member on the receiving object, evaluated in the context of the receiving object, not the original object.

This article uses "sending object/receiving object" for the two objects, rather than "receiving object/delegate", emphasizing which objects send and receive the delegation call, not the original call.

Definition

In the Introduction to Gamma et al. 1994, delegation is defined as:

Example

In the example below (using the Kotlin programming language), the class Window delegates the <code>area()</code> call to its internal Rectangle object (its delegate).

Language support

Some languages have special support for delegation built in. For example, in the Kotlin programming language the <code>by</code> keyword delegates to another object's interface:

A more practical example is the following:

In this example, LoggingRepository implements the same interface as Repository, while wrapping another Repository instance. The save method is specialized to add logging, whereas the remaining methods are forwarded automatically to the delegate object.

This avoids boilerplate and makes it possible to extend the behavior of a Repository without being tied to a particular concrete implementation such as FileRepository. By contrast, an inheritance-based solution would require subclassing a specific implementation:

This approach couples LoggingRepository to FileRepository, whereas interface delegation allows the same wrapper to be used with any `Repository` implementation.

In Kotlin, interface delegation using by is implemented by the compiler through generated forwarding methods. For example, the previous Window class is equivalent to code such as:

When interface delegation is combined with manual overriding, only the members not explicitly overridden are forwarded automatically. For example:

This program will print

The result shows that greet is handled by the explicit override in LoudGreeter, whereas greetTwice is forwarded to DefaultGreeter. Inside DefaultGreeter.greetTwice, the receiver remains the delegate object itself, so the internal calls to greet() resolve to DefaultGreeter.greet() rather than LoudGreeter.greet().

For this reason, Kotlin's by feature is often described as delegation, but operationally it is closer to compiler-generated forwarding than to stronger forms of delegation in which the original receiver context is preserved.

See also

References

External links