词条 | 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 Note that 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. DefinitionIn the Introduction to Gamma et al. 1994, Grady Booch defined delegation 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 this member variable in C++ and self 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]}} ExampleIn the example below (using the Kotlin programming language), the class Window delegates the } class Window(val bounds: Rectangle) { // Delegation fun area() = bounds.area() } Language supportSome languages have special support for delegation built in.[3] See also
References1. ^Gamma et al. 1994 2. ^{{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=20|edition=14. print.}} 3. ^{{Cite web|url=https://kotlinlang.org/docs/reference/delegation.html|title=Delegation - Kotlin Programming Language|website=Kotlin|access-date=2019-03-23}} External links
3 : Articles with example C++ code|Articles with example Java code|Software design patterns |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。