Home

Delegation

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.

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.

Kotlin Example

class Rectangle(val width: Int, val height: Int) { fun area() = width * height } class Window(val bounds: Rectangle) { // Delegation fun area() = bounds.area() }

TypeScript Example

class Rectangle { private _width: number; private _height: number; constructor(width: number, height: number) { this._width = width; this._height = height; } area() { return this._width * this._height; } } class Window { private _bounds: Rectangle; constructor(bounds: Rectangle) { this._bounds = bounds; } area() { return this._bounds.area(); } }

Repository

https://github.com/okeeffed/developer-notes-nextjs/content/design-patterns/delegation

Sections


Related