Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Delegation pattern
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Design pattern in object-oriented programming}} In [[software engineering]], the '''delegation pattern''' is an [[object-oriented programming|object-oriented]] [[design pattern]] that allows [[object composition]] to achieve the same [[code reuse]] as [[inheritance (object-oriented programming)|inheritance]]. In delegation, an [[object (computer science)|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 (computer science)|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.<ref>Gamma et al. 1994</ref> "Delegation" is often used loosely to refer to the distinct concept of [[forwarding (object-oriented programming)|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: {{quote |'''Delegation''' is a way to make composition as powerful for reuse as inheritance [Lie86, JZ91]. In delegation, ''two'' objects are involved in handling a request: a receiving object delegates operations to its '''delegate'''. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through the <code>this</code> member variable in C++ and <code>self</code> in Smalltalk. To achieve the same effect with delegation, the receiver passes itself to the delegate to let the delegated operation refer to the receiver.<ref>{{cite book|last1=Gamma|first1=Erich|last2=Helm|first2=Richard|last3=Johnson|first3=Ralph|last4=Vlissides|first4=John|title=Design patterns : elements of reusable object-oriented software|date=1995|publisher=Addison-Wesley|location=Reading, Mass.|isbn=0-201-63361-2|page=[https://archive.org/details/designpatternsel00gamm/page/20 20]|edition=14. print.|url-access=registration|url=https://archive.org/details/designpatternsel00gamm/page/20}}</ref>}} == Example == In the example below (using the [[Kotlin (programming language)|Kotlin]] programming language), the [[class (computer programming)|class]] Window [[delegation (object-oriented programming)|delegates]] the <code>area()</code> call to its internal Rectangle object (its delegate). <syntaxhighlight lang="kotlin">class Rectangle(val width: Int, val height: Int) { fun area() = width * height } class Window(val bounds: Rectangle) { // Delegation fun area() = bounds.area() }</syntaxhighlight> == Language support == Some languages have special support for delegation built in. For example, in the [[Kotlin (programming language)|Kotlin]] programming language the <code>by</code> keyword<ref>{{Cite web|url=https://kotlinlang.org/docs/reference/delegation.html|title=Delegation - Kotlin Programming Language|website=Kotlin|access-date=2019-03-23}}</ref> delegates to another object's interface: <syntaxhighlight lang="kotlin">interface ClosedShape { fun area(): Int } class Rectangle(val width: Int, val height: Int) : ClosedShape { override fun area() = width * height } // The ClosedShape implementation of Window delegates to that of the Rectangle that is bounds class Window(private val bounds: Rectangle) : ClosedShape by bounds</syntaxhighlight> == See also == * [[Delegation (object-oriented programming)]] * [[Forwarding (object-oriented programming)]] * [[Aspect-oriented programming]] * [[Delegation (computing)]] * [[Software design pattern|Design pattern]] * [[Facade pattern]] * [[Schizophrenia (object-oriented programming)]] ==References== {{Reflist}} ==External links== * [http://c2.com/cgi/wiki?WhatIsDelegation What Is Delegation], WikiWikiWeb * [http://rosettacode.org/wiki/Delegate Delegation] on [[Rosetta Code]] {{Design Patterns Patterns}} {{DEFAULTSORT:Delegation Pattern}} <!--Categories--> [[Category:Articles with example C++ code]] [[Category:Articles with example Java code]] [[Category:Software design patterns]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Cite web
(
edit
)
Template:Design Patterns Patterns
(
edit
)
Template:Quote
(
edit
)
Template:Reflist
(
edit
)
Template:Short description
(
edit
)