Delegation pattern

This commit is contained in:
maximkou
2014-03-02 21:54:04 +04:00
parent 912bd0c003
commit adec443d9c
4 changed files with 74 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
<?php
namespace DesignPatterns\Delegation;
/**
* Class JuniorDeveloper
* @package DesignPatterns\Delegation
*/
class JuniorDeveloper {
public function writeBadCode()
{
return "Some junior developer generated code...";
}
}

32
Delegation/TeamLead.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace DesignPatterns\Delegation;
/**
* Class TeamLead
* @package DesignPatterns\Delegation
* The `TeamLead` class, he delegate work to `JuniorDeveloper`
*/
class TeamLead
{
/** @var JuniorDeveloper */
protected $slave;
/**
* Give junior developer into teamlead submission
* @param JuniorDeveloper $junior
*/
public function __construct(JuniorDeveloper $junior)
{
$this->slave = $junior;
}
/**
* TeamLead drink coffee, junior work
* @return mixed
*/
public function writeCode()
{
return $this->slave->writeBadCode();
}
}

9
Delegation/Usage.php Normal file
View File

@@ -0,0 +1,9 @@
<?php
namespace DesignPatterns\Delegation;
// instantiate TeamLead and appoint to assistants JuniorDeveloper
$teamLead = new TeamLead(new JuniorDeveloper());
// team lead delegate write code to junior developer
echo $teamLead->writeCode();

View File

@@ -0,0 +1,19 @@
<?php
namespace DesignPatterns\Tests\Delegation;
use DesignPatterns\Delegation;
/**
* DelegationTest tests the delegation pattern
*/
class DelegationTest extends \PHPUnit_Framework_TestCase
{
public function testHowTeamLeadWriteCode()
{
$teamLead = new Delegation\TeamLead(
new Delegation\JuniorDeveloper()
);
$this->assertEquals("Some junior developer generated code...", $teamLead->writeCode());
}
}