Delegation pattern

Short description: Design pattern in object-oriented programming

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 `self` 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 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.

## Contents

* 1 Definition
* 2 Example
* 3 Language support
* 4 See also
* 5 References
* 6 External links

## Definition

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 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]

## Example

In the example below (using the Kotlin programming language), the class Window delegates the `area()` call to its internal Rectangle object (its delegate).

class Rectangle(val width: Int, val height: Int) {
fun area() = width * height

class Window(val bounds: Rectangle) {
// Delegation
fun area() = bounds.area()

## Language support

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

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

## See also

* Delegation (object-oriented programming)
* Forwarding (object-oriented programming)
* Aspect-oriented programming
* Delegation (computing)
* Design pattern
* Facade pattern
* Schizophrenia (object-oriented programming)

## References

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. https://archive.org/details/designpatternsel00gamm/page/20.
3. ↑ "Delegation - Kotlin Programming Language". https://kotlinlang.org/docs/reference/delegation.html.

## External links

* What Is Delegation, WikiWikiWeb
* Delegation on Rosetta Code

* v
* t
* e

Software design patterns

Creational|

* Abstract factory
* Builder
* Dependency injection
* Factory method
* Lazy initialization
* Multiton
* Object pool
* Prototype
* RAII
* Singleton

Structural|

* Adapter
* Bridge
* Composite
* Decorator
* Delegation
* Facade
* Flyweight
* Front controller
* Marker interface
* Module
* Proxy
* Twin

Behavioral|

* Blackboard
* Chain of responsibility
* Command
* Interpreter
* Iterator
* Mediator
* Memento
* Null object
* Observer
* Servant
* Specification
* State
* Strategy
* Template method
* Visitor

Functional|

* Closure
* Currying
* Function composition
* Functor
* Monad
* Generator

Concurrency|

* Active object
* Actor
* Balking
* Barrier
* Binding properties
* Coroutine
* Compute kernel
* Double-checked locking
* Event-based asynchronous
* Fiber
* Futex
* Futures and promises
* Guarded suspension
* Immutable object
* Join
* Lock
* Messaging
* Monitor
* Nuclear
* Proactor
* Reactor
* Read write lock
* Scheduler
* Thread pool
* Thread-local storage

Architectural|

* ADR
* Active record
* Broker
* Client–server
* CBD
* DAO
* DTO
* DDD
* ECB
* ECS
* EDA
* Front controller
* Identity map
* Interceptor
* Implicit invocation
* Inversion of control
* Model 2
* MOM
* Microservices
* MVA
* MVC
* MVP
* MVVM
* Monolithic
* Multitier
* Naked objects
* ORB
* P2P
* Publish–subscribe
* PAC
* REST
* SOA
* Service locator
* SN
* SBA
* Specification

Cloud
Distributed|

* Ambassador
* Anti-Corruption Layer
* Bulkhead
* Cache-Aside
* Circuit Breaker
* CQRS
* Compensating Transaction
* Competing Consumers
* Compute Resource Consolidation
* Event Sourcing
* External Configuration Store
* Federated Identity
* Gatekeeper
* Index Table
* Leader Election
* MapReduce
* Materialized View
* Pipes
* Filters
* Priority Queue
* Publisher-Subscriber
* Queue-Based Load Leveling
* Retry
* Scheduler Agent Supervisor
* Sharding
* Sidecar
* Strangler
* Throttling
* Valet Key

Other|

* Business delegate
* Composite entity
* Intercepting filter
* Lazy loading
* Mangler
* Mock object
* Type tunnel
* Method chaining

Books|

* Design Patterns
* Enterprise Integration Patterns

People|

* Christopher Alexander
* Erich Gamma
* Ralph Johnson
* John Vlissides
* Grady Booch
* Kent Beck
* Ward Cunningham
* Martin Fowler
* Robert Martin
* Jim Coplien
* Douglas Schmidt
* Linda Rising

Communities|

* The Hillside Group
* The Portland Pattern Repository

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Delegation pattern. Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Delegation_pattern&oldid=3386115"
*[v]: View this template
*[t]: Discuss this template
*[e]: Edit this template