Skip to content

Instantly share code, notes, and snippets.

@UtmostCreator
Last active October 30, 2025 17:03
Show Gist options
  • Select an option

  • Save UtmostCreator/9e488a91b7c9fd5ff60047a8b5fbb567 to your computer and use it in GitHub Desktop.

Select an option

Save UtmostCreator/9e488a91b7c9fd5ff60047a8b5fbb567 to your computer and use it in GitHub Desktop.
<?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