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 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. "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.
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. "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. class Rectangle(val width: Int, val height: Int) { fun area() = width * height } class Window(val bounds: Rectangle) { // Delegation fun area() = bounds.area() } 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 A more practical example is the following: interface Repository { fun save(data: String) fun load(id: String): String fun delete(id: String) } class FileRepository : Repository { override fun save(data: String) = println("saving $data") override fun load(id: String): String = "data:$id" override fun delete(id: String) = println("deleting $id") } class LoggingRepository( private val delegate: Repository ) : Repository by delegate { override fun save(data: String) { println("about to save") delegate.save(data) } } In this example, LoggingRepository implements the same interface as Repository, while wrapping another Repository instance. The save method is specialized to add logging, whereas the remaining methods are forwarded automatically to the delegate object. This avoids boilerplate and makes it possible to extend the behavior of a Repository without being tied to a particular concrete implementation such as FileRepository. By contrast, an inheritance-based solution would require subclassing a specific implementation: interface Repository { fun save(data: String) fun load(id: String): String fun delete(id: String) } open class FileRepository : Repository { override fun save(data: String) = println("saving $data") override fun load(id: String): String = "data:$id" override fun delete(id: String) = println("deleting $id") } class LoggingRepository : FileRepository() { override fun save(data: String) { println("about to save") super.save(data) } } This approach couples LoggingRepository to FileRepository, whereas interface delegation allows the same wrapper to be used with any `Repository` implementation. In Kotlin, interface delegation using by is implemented by the compiler through generated forwarding methods. For example, the previous Window class is equivalent to code such as: 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 { override fun area() = bounds.area() } When interface delegation is combined with manual overriding, only the members not explicitly overridden are forwarded automatically. For example: interface Greeter { fun greet() fun greetTwice() } class DefaultGreeter : Greeter { override fun greet() { println("hello") } override fun greetTwice() { greet() greet() } } class LoudGreeter( private val delegate: Greeter ) : Greeter by delegate { override fun greet() { println("HELLO") } } fun main() { val g = LoudGreeter(DefaultGreeter()) g.greet() g.greetTwice() } This program will print HELLO hello hello The result shows that greet is handled by the explicit override in LoudGreeter, whereas greetTwice is forwarded to DefaultGreeter. Inside DefaultGreeter.greetTwice, the receiver remains the delegate object itself, so the internal calls to greet() resolve to DefaultGreeter.greet() rather than LoudGreeter.greet(). For this reason, Kotlin's by feature is often described as delegation, but operationally it is closer to compiler-generated forwarding than to stronger forms of delegation in which the original receiver context is preserved.
References
No reference rows published for this article.
Cite this article
MLA
"Delegation pattern." encyclopegia.org, 2026, https://encyclopegia.org/wiki/delegation-pattern. Accessed 24 November 2026.
Chicago
"Delegation pattern." encyclopegia.org. Accessed November 24, 2026. https://encyclopegia.org/wiki/delegation-pattern.
APA
encyclopegia.org. (2026). Delegation pattern. Retrieved November 24, 2026, from https://encyclopegia.org/wiki/delegation-pattern
Accessed November 24, 2026 · CC BY-SA 4.0 · citation page · JSON