Last active
October 30, 2025 17:03
-
-
Save UtmostCreator/9e488a91b7c9fd5ff60047a8b5fbb567 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| Delegation Pattern | |
| ------------------ | |
| Intent: An object passes (delegates) a task to a helper object | |
| instead of doing it itself — but keeps the same interface. | |
| Difference from Strategy: | |
| - Strategy = interchangeable algorithms. | |
| - Delegation = same responsibility, but helper *acts on behalf of* the main object. | |
| */ | |
| // Common interface | |
| interface Printer { | |
| public function print(string $text): void; | |
| } | |
| // Real worker | |
| final class RealPrinter implements Printer { | |
| public function print(string $text): void { | |
| echo "🖨️ Printing: {$text}\n"; | |
| } | |
| } | |
| // Delegating class | |
| final class Manager implements Printer { | |
| public function __construct(private Printer $printer) {} | |
| public function print(string $text): void { | |
| // Manager delegates to the RealPrinter | |
| $this->printer->print($text); | |
| } | |
| } | |
| // Client code | |
| $mgr = new Manager(new RealPrinter()); | |
| $mgr->print("Monthly Report"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment