Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Delegation pattern

From Wikipedia, the free encyclopedia
Design pattern in object-oriented programming

Insoftware engineering, thedelegation pattern is anobject-orienteddesign pattern that allowsobject composition to achieve the samecode reuse asinheritance.

In delegation, anobject handles a request by delegating to a second object (thedelegate). The delegate is ahelper object, butwith the original context. With language-level support for delegation, this is done implicitly by havingself 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.[1] "Delegation" is often used loosely to refer to the distinct concept offorwarding, where the sending object simply uses the corresponding member on the receiving object, evaluated in the context of thereceiving 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

[edit]

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

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 itsdelegate. This is analogous to subclasses deferring requests to parent classes. But with inheritance, an inherited operation can always refer to the receiving object through thethis member variable in C++ andself 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.[2]

Example

[edit]

In the example below (using theKotlin programming language), theclass Windowdelegates thearea() call to its internal Rectangle object (its delegate).

classRectangle(valwidth:Int,valheight:Int){funarea()=width*height}classWindow(valbounds:Rectangle){// Delegationfunarea()=bounds.area()}

Language support

[edit]

Some languages have special support for delegation built in. For example, in theKotlin programming language theby keyword[3] delegates to another object's interface:

interfaceClosedShape{funarea():Int}classRectangle(valwidth:Int,valheight:Int):ClosedShape{overridefunarea()=width*height}// The ClosedShape implementation of Window delegates to that of the Rectangle that is boundsclassWindow(privatevalbounds:Rectangle):ClosedShapebybounds

See also

[edit]

References

[edit]
  1. ^Gamma et al. 1994
  2. ^Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John (1995).Design patterns : elements of reusable object-oriented software (14. print. ed.). Reading, Mass.: Addison-Wesley. p. 20.ISBN 0-201-63361-2.
  3. ^"Delegation - Kotlin Programming Language".Kotlin. Retrieved2019-03-23.

External links

[edit]
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Delegation_pattern&oldid=1182341440"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp