mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-30 19:50:12 +02:00
removed delegation as it is not a pattern but merely a concept in OOP
This commit is contained in:
@@ -1,19 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Delegation;
|
||||
|
||||
class JuniorDeveloper
|
||||
{
|
||||
public function writeBadCode(): string
|
||||
{
|
||||
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();
|
||||
}
|
||||
}
|
@@ -1,52 +0,0 @@
|
||||
`Delegation`__
|
||||
==============
|
||||
|
||||
Purpose
|
||||
-------
|
||||
|
||||
Demonstrate the Delegator pattern, where an object, instead of performing one
|
||||
of its stated tasks, delegates that task to an associated helper object. In
|
||||
this case TeamLead professes to writeCode and Usage uses this, while TeamLead
|
||||
delegates writeCode to JuniorDeveloper's writeBadCode function. This inverts
|
||||
the responsibility so that Usage is unknowingly executing writeBadCode.
|
||||
|
||||
Examples
|
||||
--------
|
||||
|
||||
Please review JuniorDeveloper.php, TeamLead.php, and then Usage.php to see it all tied together.
|
||||
|
||||
UML Diagram
|
||||
-----------
|
||||
|
||||
.. image:: uml/uml.png
|
||||
:alt: Alt Delegation UML Diagram
|
||||
:align: center
|
||||
|
||||
Code
|
||||
----
|
||||
|
||||
You can also find this code on `GitHub`_
|
||||
|
||||
TeamLead.php
|
||||
|
||||
.. literalinclude:: TeamLead.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
JuniorDeveloper.php
|
||||
|
||||
.. literalinclude:: JuniorDeveloper.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
Test
|
||||
----
|
||||
|
||||
Tests/DelegationTest.php
|
||||
|
||||
.. literalinclude:: Tests/DelegationTest.php
|
||||
:language: php
|
||||
:linenos:
|
||||
|
||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/More/Delegation
|
||||
.. __: http://en.wikipedia.org/wiki/Delegation_pattern
|
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Delegation;
|
||||
|
||||
class TeamLead
|
||||
{
|
||||
/**
|
||||
* @var JuniorDeveloper
|
||||
*/
|
||||
private $junior;
|
||||
|
||||
/**
|
||||
* @param JuniorDeveloper $junior
|
||||
*/
|
||||
public function __construct(JuniorDeveloper $junior)
|
||||
{
|
||||
$this->junior = $junior;
|
||||
}
|
||||
|
||||
public function writeCode(): string
|
||||
{
|
||||
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...';
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\More\Delegation\Tests;
|
||||
|
||||
use DesignPatterns\More\Delegation;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class DelegationTest extends TestCase
|
||||
{
|
||||
public function testTeamLeadCanBlameJuniorForBadCode()
|
||||
{
|
||||
$junior = new Delegation\JuniorDeveloper();
|
||||
$teamLead = new Delegation\TeamLead($junior);
|
||||
|
||||
$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());
|
||||
}
|
||||
}
|
@@ -1,21 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Diagram>
|
||||
<ID>PHP</ID>
|
||||
<OriginalElement>\DesignPatterns\More\Delegation\JuniorDeveloper</OriginalElement>
|
||||
<nodes>
|
||||
<node x="-8.0" y="134.0">\DesignPatterns\More\Delegation\JuniorDeveloper</node>
|
||||
<node x="0.0" y="0.0">\DesignPatterns\More\Delegation\TeamLead</node>
|
||||
</nodes>
|
||||
<notes />
|
||||
<edges />
|
||||
<settings layout="Hierarchic Group" zoom="1.0" x="68.5" y="90.5" />
|
||||
<SelectedNodes />
|
||||
<Categories>
|
||||
<Category>Fields</Category>
|
||||
<Category>Constants</Category>
|
||||
<Category>Constructors</Category>
|
||||
<Category>Methods</Category>
|
||||
</Categories>
|
||||
<VISIBILITY>private</VISIBILITY>
|
||||
</Diagram>
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 5.8 KiB |
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 28 KiB |
@@ -1,6 +1,5 @@
|
||||
# More
|
||||
|
||||
* [Delegation](Delegation) [:notebook:](http://en.wikipedia.org/wiki/Delegation_pattern)
|
||||
* [ServiceLocator](ServiceLocator) [:notebook:](http://en.wikipedia.org/wiki/Service_locator_pattern)
|
||||
* [Repository](Repository)
|
||||
* [EAV](EAV) [:notebook:](https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model)
|
||||
|
@@ -4,7 +4,6 @@ More
|
||||
.. toctree::
|
||||
:titlesonly:
|
||||
|
||||
Delegation/README
|
||||
ServiceLocator/README
|
||||
Repository/README
|
||||
EAV/README
|
||||
|
Reference in New Issue
Block a user