mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
docs: update delegation
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user