docs: update delegation

This commit is contained in:
Ilkka Seppälä
2024-05-25 11:46:51 +03:00
parent 410c0c69a3
commit f9cb2b6ebe
+7 -5
View File
@@ -3,7 +3,9 @@ title: Delegation
category: Behavioral
language: en
tag:
- Decoupling
- Delegation
- Object composition
---
## Also known as
@@ -13,7 +15,7 @@ tag:
## Intent
The Delegation pattern in Java allows an object to delegate one or more tasks to a helper object. It is a technique where an object expresses certain behavior but actually delegates responsibility for implementing that behavior to an associated helper object.
To allow an object to delegate responsibility for a task to another helper object.
## Explanation
@@ -27,6 +29,8 @@ Wikipedia says
**Programmatic Example**
Let's consider a printing example.
We have an interface `Printer` and three implementations `CanonPrinter`, `EpsonPrinter` and `HpPrinter`.
```java
@@ -59,8 +63,7 @@ public class HpPrinter implements Printer {
}
```
The `PrinterController` can be used as a `Printer` by delegating any work handled by this
interface to an object implementing it.
The `PrinterController` can be used as a `Printer` by delegating any work handled by this interface to an object implementing it.
```java
public class PrinterController implements Printer {
@@ -78,8 +81,7 @@ public class PrinterController implements Printer {
}
```
Now on the client code printer controllers can print messages differently depending on the
object they're delegating that work to.
Now on the client code printer controllers can print messages differently depending on the object they're delegating that work to.
```java
public class App {