Delegation Pattern
   HOME

TheInfoList



OR:

In
software engineering Software engineering is a branch of both computer science and engineering focused on designing, developing, testing, and maintaining Application software, software applications. It involves applying engineering design process, engineering principl ...
, the delegation pattern is an object-oriented design pattern that allows object composition to achieve the same
code reuse Code reuse is the practice of using existing source code to develop software instead of writing new code. ''Software reuse'' is a broader term that implies using any existing software asset to develop software instead of developing it again. An as ...
as
inheritance Inheritance is the practice of receiving private property, titles, debts, entitlements, privileges, rights, and obligations upon the death of an individual. The rules of inheritance differ among societies and have changed over time. Offi ...
. 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
self In philosophy, the self is an individual's own being, knowledge, and values, and the relationship between these attributes. The first-person perspective distinguishes selfhood from personal identity. Whereas "identity" is (literally) same ...
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 Class, Classes, or The Class may refer to: Common uses not otherwise categorized * Class (biology), a taxonomic rank * Class (knowledge representation), a collection of individuals or objects * Class (philosophy), an analytical concept used d ...
Window delegates the area() call to its internal Rectangle object (its delegate). class Rectangle(val width: Int, val height: Int) class Window(val bounds: Rectangle)


Language support

Some languages have special support for delegation built in. For example, in the Kotlin programming language the by keyword delegates to another object's interface: interface ClosedShape class Rectangle(val width: Int, val height: Int) : ClosedShape // The ClosedShape implementation of Window delegates to that of the Rectangle that is bounds class Window(private val bounds: Rectangle) : ClosedShape by bounds


See also

*
Delegation (object-oriented programming) In object-oriented programming, delegation refers to evaluating a member (property or method) of one object (the receiver) in the context of another original object (the sender). Delegation can be done explicitly, by passing the responsibilitie ...
* Forwarding (object-oriented programming) * Aspect-oriented programming * Delegation (computing) * Design pattern * Facade pattern * Schizophrenia (object-oriented programming)


References


External links


What Is Delegation
WikiWikiWeb
Delegation
on Rosetta Code {{DEFAULTSORT:Delegation Pattern Articles with example C++ code Articles with example Java code Software design patterns