mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-06 06:25:48 +02:00
enhance delegation example
This commit is contained in:
parent
0ea1593c1d
commit
b4599ec67c
@ -8,4 +8,11 @@ class JuniorDeveloper
|
||||
{
|
||||
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,16 @@ class TeamLead
|
||||
{
|
||||
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,24 @@ use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DelegationTest extends TestCase
|
||||
{
|
||||
public function testHowTeamLeadWriteCode()
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function teamLeadCanBlameJuniorForBadCode()
|
||||
{
|
||||
$junior = new Delegation\JuniorDeveloper();
|
||||
$teamLead = new Delegation\TeamLead($junior);
|
||||
|
||||
$this->assertEquals($junior->writeBadCode(), $teamLead->writeCode());
|
||||
}
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function teamLeadCanWriteBadCode()
|
||||
{
|
||||
$junior = new Delegation\JuniorDeveloper();
|
||||
$teamLead = new Delegation\TeamLead($junior);
|
||||
|
||||
$this->assertEquals($junior->writeReallyBadCode($teamLead), $teamLead->writeBadCode());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user