在委託模式的示例裏,一個對象將它要執行的任務委派給與之關聯的幫助對象去執行。在示例中,「組長」聲明瞭 writeCode 方法並使用它,其實「組長」把 writeCode 委託給「菜鳥開發者」的 writeBadCode 方法作了。這種反轉責任的作法隱藏了其內部執行 writeBadCode 的細節。php
請閱讀 JuniorDeveloper.php,TeamLead.php 中的代碼,而後在 Usage.php 中結合在一塊兒。laravel
<?php namespace DesignPatterns\More\Delegation; class TeamLead { /** * @var JuniorDeveloper */ private $junior; /** * @param JuniorDeveloper $junior */ public function __construct(JuniorDeveloper $junior) { $this->junior = $junior; } public function writeCode(): string { return $this->junior->writeBadCode(); } }
<?php namespace DesignPatterns\More\Delegation; class JuniorDeveloper { public function writeBadCode(): string { return 'Some junior developer generated code...'; } }
<?php namespace DesignPatterns\More\Delegation\Tests; use DesignPatterns\More\Delegation; use PHPUnit\Framework\TestCase; class DelegationTest extends TestCase { public function testHowTeamLeadWriteCode() { $junior = new Delegation\JuniorDeveloper(); $teamLead = new Delegation\TeamLead($junior); $this->assertEquals($junior->writeBadCode(), $teamLead->writeCode()); } }
PHP 互聯網架構師 50K 成長指南+行業問題解決總綱(持續更新)shell
面試10家公司,收穫9個offer,2020年PHP 面試問題設計模式
★若是喜歡個人文章,想與更多資深開發者一塊兒交流學習的話,獲取更多大廠面試相關技術諮詢和指導,歡迎加入咱們的羣啊,暗號:phpzh服務器
內容不錯的話但願你們支持鼓勵下點個贊/喜歡,歡迎一塊兒來交流;另外若是有什麼問題 建議 想看的內容能夠在評論提出併發