在委托模式的示例里,一个对象将它要执行的任务委派给与之关联的帮助对象去执行。在示例中,「组长」声明了 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服务器
内容不错的话但愿你们支持鼓励下点个赞/喜欢,欢迎一块儿来交流;另外若是有什么问题 建议 想看的内容能够在评论提出并发