PHP7 Delegation

This commit is contained in:
Dominik Liebler
2016-09-22 15:28:46 +02:00
parent df6fadf517
commit 2b1de13391
5 changed files with 9 additions and 31 deletions

View File

@@ -4,7 +4,7 @@ namespace DesignPatterns\More\Delegation;
class JuniorDeveloper class JuniorDeveloper
{ {
public function writeBadCode() public function writeBadCode(): string
{ {
return 'Some junior developer generated code...'; return 'Some junior developer generated code...';
} }

View File

@@ -23,12 +23,6 @@ Code
You can also find these code on `GitHub`_ You can also find these code on `GitHub`_
Usage.php
.. literalinclude:: Usage.php
:language: php
:linenos:
TeamLead.php TeamLead.php
.. literalinclude:: TeamLead.php .. literalinclude:: TeamLead.php

View File

@@ -4,26 +4,21 @@ namespace DesignPatterns\More\Delegation;
class TeamLead class TeamLead
{ {
/** @var JuniorDeveloper */ /**
protected $slave; * @var JuniorDeveloper
*/
private $junior;
/** /**
* Give junior developer into teamlead submission.
*
* @param JuniorDeveloper $junior * @param JuniorDeveloper $junior
*/ */
public function __construct(JuniorDeveloper $junior) public function __construct(JuniorDeveloper $junior)
{ {
$this->slave = $junior; $this->junior = $junior;
} }
/** public function writeCode(): string
* TeamLead drink coffee, junior work.
*
* @return mixed
*/
public function writeCode()
{ {
return $this->slave->writeBadCode(); return $this->junior->writeBadCode();
} }
} }

View File

@@ -4,15 +4,13 @@ namespace DesignPatterns\More\Delegation\Tests;
use DesignPatterns\More\Delegation; use DesignPatterns\More\Delegation;
/**
* DelegationTest tests the delegation pattern.
*/
class DelegationTest extends \PHPUnit_Framework_TestCase class DelegationTest extends \PHPUnit_Framework_TestCase
{ {
public function testHowTeamLeadWriteCode() public function testHowTeamLeadWriteCode()
{ {
$junior = new Delegation\JuniorDeveloper(); $junior = new Delegation\JuniorDeveloper();
$teamLead = new Delegation\TeamLead($junior); $teamLead = new Delegation\TeamLead($junior);
$this->assertEquals($junior->writeBadCode(), $teamLead->writeCode()); $this->assertEquals($junior->writeBadCode(), $teamLead->writeCode());
} }
} }

View File

@@ -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();