Delegation Pattern
   HOME

TheInfoList



OR:

In
software engineering Software engineering is a systematic engineering approach to software development. A software engineer is a person who applies the principles of software engineering to design, develop, maintain, test, and evaluate computer software. The term '' ...
, the delegation pattern is an
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
that allows
object composition In computer science, object composition and object aggregation are closely related ways to combine objects or data types into more complex ones. In conversation the distinction between composition and aggregation is often ignored. Common kind ...
to achieve the same
code reuse In software development (and computer programming in general), code reuse, also called software reuse, is the use of existing software, or software knowledge, to build new software, following the reusability principles. Code reuse may be achiev ...
as
inheritance Inheritance is the practice of receiving private property, Title (property), titles, debts, entitlements, Privilege (law), privileges, rights, and Law of obligations, obligations upon the death of an individual. The rules of inheritance differ ...
. In delegation, an
object Object may refer to: General meanings * Object (philosophy), a thing, being, or concept ** Object (abstract), an object which does not exist at any particular time or place ** Physical object, an identifiable collection of matter * Goal, an ...
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 The self is an individual as the object of that individual’s own reflective consciousness. Since the ''self'' is a reference by a subject to the same subject, this reference is necessarily subjective. The sense of having a self—or ''selfhood ...
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. Note that "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. 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.


Definition

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


Example

In the example below (using the Kotlin programming language), the
class Class 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 differentl ...
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, we could write: interface ClosedShape class Rectangle(val width: Int, val height: Int) : ClosedShape class Window(private val bounds: ClosedShape) : 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 sending object to ...
*
Forwarding (object-oriented programming) In object-oriented programming, forwarding means that using a member of an object (either a property or a method) results in actually using the corresponding member of a different object: the use is ''forwarded'' to another object. Forwarding is use ...
*
Aspect-oriented programming In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding behavior to existing code (an advice) ''without'' modifying t ...
*
Delegation (computing) In computing or computer programming, delegation refers generally to one entity passing something to another entity,Barry Wilkinson, ''Grid Computing: Techniques and Applications'' (2009), p. 164, . and narrowly to various specific forms of relat ...
*
Design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
*
Facade pattern The facade pattern (also spelled ''façade'') is a software-design pattern commonly used in object-oriented programming. Analogous to a facade in architecture, a facade is an object that serves as a front-facing interface masking more complex und ...
* 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