mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 10:40:17 +02:00
PHP7 Delegation
This commit is contained in:
@@ -4,7 +4,7 @@ namespace DesignPatterns\More\Delegation;
|
||||
|
||||
class JuniorDeveloper
|
||||
{
|
||||
public function writeBadCode()
|
||||
public function writeBadCode(): string
|
||||
{
|
||||
return 'Some junior developer generated code...';
|
||||
}
|
||||
|
@@ -23,12 +23,6 @@ Code
|
||||
|
||||
You can also find these code on `GitHub`_
|
||||
|
||||
Usage.php
|
||||
|
||||
.. literalinclude:: Usage.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
TeamLead.php
|
||||
|
||||
.. literalinclude:: TeamLead.php
|
||||
|
@@ -4,26 +4,21 @@ namespace DesignPatterns\More\Delegation;
|
||||
|
||||
class TeamLead
|
||||
{
|
||||
/** @var JuniorDeveloper */
|
||||
protected $slave;
|
||||
/**
|
||||
* @var JuniorDeveloper
|
||||
*/
|
||||
private $junior;
|
||||
|
||||
/**
|
||||
* Give junior developer into teamlead submission.
|
||||
*
|
||||
* @param JuniorDeveloper $junior
|
||||
*/
|
||||
public function __construct(JuniorDeveloper $junior)
|
||||
{
|
||||
$this->slave = $junior;
|
||||
$this->junior = $junior;
|
||||
}
|
||||
|
||||
/**
|
||||
* TeamLead drink coffee, junior work.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function writeCode()
|
||||
public function writeCode(): string
|
||||
{
|
||||
return $this->slave->writeBadCode();
|
||||
return $this->junior->writeBadCode();
|
||||
}
|
||||
}
|
||||
|
@@ -4,15 +4,13 @@ namespace DesignPatterns\More\Delegation\Tests;
|
||||
|
||||
use DesignPatterns\More\Delegation;
|
||||
|
||||
/**
|
||||
* DelegationTest tests the delegation pattern.
|
||||
*/
|
||||
class DelegationTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testHowTeamLeadWriteCode()
|
||||
{
|
||||
$junior = new Delegation\JuniorDeveloper();
|
||||
$teamLead = new Delegation\TeamLead($junior);
|
||||
|
||||
$this->assertEquals($junior->writeBadCode(), $teamLead->writeCode());
|
||||
}
|
||||
}
|
||||
|
@@ -1,9 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Delegation;
|
||||
|
||||
// instantiate TeamLead and appoint to assistants JuniorDeveloper
|
||||
$teamLead = new TeamLead(new JuniorDeveloper());
|
||||
|
||||
// team lead delegate write code to junior developer
|
||||
echo $teamLead->writeCode();
|
Reference in New Issue
Block a user