mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-28 18:50:11 +02:00
Merge pull request #322 from carousel/master
enhance delegation example
This commit is contained in:
@@ -8,4 +8,12 @@ class JuniorDeveloper
|
|||||||
{
|
{
|
||||||
return 'Some junior developer generated code...';
|
return 'Some junior developer generated code...';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Junior is authorized to call method on TeamLead (real delegation)
|
||||||
|
*/
|
||||||
|
public function writeReallyBadCode(TeamLead $teamLead): string
|
||||||
|
{
|
||||||
|
return $teamLead->writeReallyBadCode();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -21,4 +21,18 @@ class TeamLead
|
|||||||
{
|
{
|
||||||
return $this->junior->writeBadCode();
|
return $this->junior->writeBadCode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function writeBadCode(): string
|
||||||
|
{
|
||||||
|
//note that we are passing $this from teamLead context
|
||||||
|
return $this->junior->writeReallyBadCode($this);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Junior can call this method
|
||||||
|
*/
|
||||||
|
public function writeReallyBadCode(): string
|
||||||
|
{
|
||||||
|
return 'Even team lead can write bad code...';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,11 +7,19 @@ use PHPUnit\Framework\TestCase;
|
|||||||
|
|
||||||
class DelegationTest extends TestCase
|
class DelegationTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testHowTeamLeadWriteCode()
|
public function testTeamLeadCanBlameJuniorForBadCode()
|
||||||
{
|
{
|
||||||
$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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testTeamLeadCanWriteBadCode()
|
||||||
|
{
|
||||||
|
$junior = new Delegation\JuniorDeveloper();
|
||||||
|
$teamLead = new Delegation\TeamLead($junior);
|
||||||
|
|
||||||
|
$this->assertEquals($junior->writeReallyBadCode($teamLead), $teamLead->writeBadCode());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user