Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb42e71

Browse files
author
Dominik Liebler
authored
Merge pull request DesignPatternsPHP#322 from carousel/master
enhance delegation example
2 parents 305078f + bb22ef0 commit fb42e71

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

More/Delegation/JuniorDeveloper.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@ public function writeBadCode(): string
88
{
99
return 'Some junior developer generated code...';
1010
}
11+
12+
/**
13+
* Junior is authorized to call method on TeamLead (real delegation)
14+
*/
15+
public function writeReallyBadCode(TeamLead $teamLead): string
16+
{
17+
return $teamLead->writeReallyBadCode();
18+
}
1119
}

More/Delegation/TeamLead.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,18 @@ public function writeCode(): string
2121
{
2222
return $this->junior->writeBadCode();
2323
}
24+
25+
public function writeBadCode(): string
26+
{
27+
//note that we are passing $this from teamLead context
28+
return $this->junior->writeReallyBadCode($this);
29+
}
30+
31+
/**
32+
* Junior can call this method
33+
*/
34+
public function writeReallyBadCode(): string
35+
{
36+
return 'Even team lead can write bad code...';
37+
}
2438
}

More/Delegation/Tests/DelegationTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,19 @@
77

88
class DelegationTest extends TestCase
99
{
10-
public function testHowTeamLeadWriteCode()
10+
public function testTeamLeadCanBlameJuniorForBadCode()
1111
{
1212
$junior = new Delegation\JuniorDeveloper();
1313
$teamLead = new Delegation\TeamLead($junior);
1414

1515
$this->assertEquals($junior->writeBadCode(), $teamLead->writeCode());
1616
}
17+
18+
public function testTeamLeadCanWriteBadCode()
19+
{
20+
$junior = new Delegation\JuniorDeveloper();
21+
$teamLead = new Delegation\TeamLead($junior);
22+
23+
$this->assertEquals($junior->writeReallyBadCode($teamLead), $teamLead->writeBadCode());
24+
}
1725
}

0 commit comments

Comments
 (0)