mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 13:07:27 +02:00
Delegation pattern
This commit is contained in:
14
Delegation/JuniorDeveloper.php
Normal file
14
Delegation/JuniorDeveloper.php
Normal 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
32
Delegation/TeamLead.php
Normal 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
9
Delegation/Usage.php
Normal 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();
|
19
Tests/Delegation/DelegationTest.php
Normal file
19
Tests/Delegation/DelegationTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user