mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-13 19:36:22 +02:00
remove Interface-Suffix
This commit is contained in:
@ -20,7 +20,7 @@ class Group implements Role
|
|||||||
return sprintf('Group: %s', $this->name);
|
return sprintf('Group: %s', $this->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function accept(RoleVisitorInterface $visitor)
|
public function accept(RoleVisitor $visitor)
|
||||||
{
|
{
|
||||||
$visitor->visitGroup($this);
|
$visitor->visitGroup($this);
|
||||||
}
|
}
|
||||||
|
@ -25,18 +25,18 @@ Code
|
|||||||
|
|
||||||
You can also find this code on `GitHub`_
|
You can also find this code on `GitHub`_
|
||||||
|
|
||||||
RoleVisitorInterface.php
|
|
||||||
|
|
||||||
.. literalinclude:: RoleVisitorInterface.php
|
|
||||||
:language: php
|
|
||||||
:linenos:
|
|
||||||
|
|
||||||
RoleVisitor.php
|
RoleVisitor.php
|
||||||
|
|
||||||
.. literalinclude:: RoleVisitor.php
|
.. literalinclude:: RoleVisitor.php
|
||||||
:language: php
|
:language: php
|
||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
|
RecordingVisitor.php
|
||||||
|
|
||||||
|
.. literalinclude:: RecordingVisitor.php
|
||||||
|
:language: php
|
||||||
|
:linenos:
|
||||||
|
|
||||||
Role.php
|
Role.php
|
||||||
|
|
||||||
.. literalinclude:: Role.php
|
.. literalinclude:: Role.php
|
||||||
@ -65,4 +65,4 @@ Tests/VisitorTest.php
|
|||||||
:linenos:
|
:linenos:
|
||||||
|
|
||||||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Visitor
|
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/master/Behavioral/Visitor
|
||||||
.. __: http://en.wikipedia.org/wiki/Visitor_pattern
|
.. __: http://en.wikipedia.org/wiki/Visitor_pattern
|
||||||
|
30
Behavioral/Visitor/RecordingVisitor.php
Normal file
30
Behavioral/Visitor/RecordingVisitor.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Visitor;
|
||||||
|
|
||||||
|
class RecordingVisitor implements RoleVisitor
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Role[]
|
||||||
|
*/
|
||||||
|
private $visited = [];
|
||||||
|
|
||||||
|
public function visitGroup(Group $role)
|
||||||
|
{
|
||||||
|
$this->visited[] = $role;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function visitUser(User $role)
|
||||||
|
{
|
||||||
|
$this->visited[] = $role;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Role[]
|
||||||
|
*/
|
||||||
|
public function getVisited(): array
|
||||||
|
{
|
||||||
|
return $this->visited;
|
||||||
|
}
|
||||||
|
}
|
@ -5,5 +5,5 @@ namespace DesignPatterns\Behavioral\Visitor;
|
|||||||
|
|
||||||
interface Role
|
interface Role
|
||||||
{
|
{
|
||||||
public function accept(RoleVisitorInterface $visitor);
|
public function accept(RoleVisitor $visitor);
|
||||||
}
|
}
|
||||||
|
@ -3,28 +3,13 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Visitor;
|
namespace DesignPatterns\Behavioral\Visitor;
|
||||||
|
|
||||||
class RoleVisitor implements RoleVisitorInterface
|
/**
|
||||||
|
* Note: the visitor must not choose itself which method to
|
||||||
|
* invoke, it is the Visitee that make this decision
|
||||||
|
*/
|
||||||
|
interface RoleVisitor
|
||||||
{
|
{
|
||||||
/**
|
public function visitUser(User $role);
|
||||||
* @var Role[]
|
|
||||||
*/
|
|
||||||
private $visited = [];
|
|
||||||
|
|
||||||
public function visitGroup(Group $role)
|
public function visitGroup(Group $role);
|
||||||
{
|
|
||||||
$this->visited[] = $role;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function visitUser(User $role)
|
|
||||||
{
|
|
||||||
$this->visited[] = $role;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Role[]
|
|
||||||
*/
|
|
||||||
public function getVisited(): array
|
|
||||||
{
|
|
||||||
return $this->visited;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Visitor;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Note: the visitor must not choose itself which method to
|
|
||||||
* invoke, it is the Visitee that make this decision
|
|
||||||
*/
|
|
||||||
interface RoleVisitorInterface
|
|
||||||
{
|
|
||||||
public function visitUser(User $role);
|
|
||||||
|
|
||||||
public function visitGroup(Group $role);
|
|
||||||
}
|
|
@ -9,13 +9,13 @@ use PHPUnit\Framework\TestCase;
|
|||||||
class VisitorTest extends TestCase
|
class VisitorTest extends TestCase
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Visitor\RoleVisitor
|
* @var Visitor\RecordingVisitor
|
||||||
*/
|
*/
|
||||||
private $visitor;
|
private $visitor;
|
||||||
|
|
||||||
protected function setUp(): void
|
protected function setUp(): void
|
||||||
{
|
{
|
||||||
$this->visitor = new Visitor\RoleVisitor();
|
$this->visitor = new Visitor\RecordingVisitor();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function provideRoles()
|
public function provideRoles()
|
||||||
|
@ -20,7 +20,7 @@ class User implements Role
|
|||||||
return sprintf('User %s', $this->name);
|
return sprintf('User %s', $this->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function accept(RoleVisitorInterface $visitor)
|
public function accept(RoleVisitor $visitor)
|
||||||
{
|
{
|
||||||
$visitor->visitUser($this);
|
$visitor->visitUser($this);
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 57 KiB |
Reference in New Issue
Block a user