mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-03 21:47:25 +02:00
documentation (commiting now from Guadeloupe)
This commit is contained in:
@@ -9,13 +9,14 @@ namespace DesignPatterns\Visitor;
|
|||||||
* The Visitor Pattern lets you outsource operations on objects to other objects. The main reason to do this is to keep
|
* The Visitor Pattern lets you outsource operations on objects to other objects. The main reason to do this is to keep
|
||||||
* a seperation of concerns. But classes have to define an contract to allow visitors (the "accept" method in the example below).
|
* a seperation of concerns. But classes have to define an contract to allow visitors (the "accept" method in the example below).
|
||||||
*
|
*
|
||||||
* The contract is an abstract class but you can have also a clean interface
|
* The contract is an abstract class but you can have also a clean interface.
|
||||||
|
* In that case, each Visitee has to choose itself which method to invoke on the visitor.
|
||||||
*/
|
*/
|
||||||
abstract class Role
|
abstract class Role
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method handle a double dispatch based on the shortname of the Visitee
|
* This method handles a double dispatch based on the shortname of the Visitee
|
||||||
*
|
*
|
||||||
* Feel free to override it if your object must call another visiting behavior
|
* Feel free to override it if your object must call another visiting behavior
|
||||||
*
|
*
|
||||||
|
@@ -5,16 +5,22 @@ namespace DesignPatterns\Visitor;
|
|||||||
/**
|
/**
|
||||||
* Visitor Pattern
|
* Visitor Pattern
|
||||||
*
|
*
|
||||||
* An implementation of a Visitor
|
* An implementation of a concrete Visitor
|
||||||
*/
|
*/
|
||||||
class RolePrintVisitor implements RoleVisitor
|
class RolePrintVisitor implements RoleVisitor
|
||||||
{
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
public function visitGroup(Group $role)
|
public function visitGroup(Group $role)
|
||||||
{
|
{
|
||||||
echo "Role: " . $role->getName();
|
echo "Role: " . $role->getName();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritdoc
|
||||||
|
*/
|
||||||
public function visitUser(User $role)
|
public function visitUser(User $role)
|
||||||
{
|
{
|
||||||
echo "Role: " . $role->getName();
|
echo "Role: " . $role->getName();
|
||||||
|
@@ -5,13 +5,19 @@ namespace DesignPatterns\Visitor;
|
|||||||
/**
|
/**
|
||||||
* Visitor Pattern
|
* Visitor Pattern
|
||||||
*
|
*
|
||||||
* The contract for the visitor
|
* The contract for the visitor.
|
||||||
|
*
|
||||||
|
* Note 1 : in C++ or java, with method polymorphism based on type-hint, there are many
|
||||||
|
* methods visit() with different type for the 'role' parameter.
|
||||||
|
*
|
||||||
|
* Note 2 : the visitor must not choose itself which method to
|
||||||
|
* invoke, it is the Visitee that make this decision.
|
||||||
*/
|
*/
|
||||||
interface RoleVisitor
|
interface RoleVisitor
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Visit a user object
|
* Visit a User object
|
||||||
*
|
*
|
||||||
* @param \DesignPatterns\Visitor\User $role
|
* @param \DesignPatterns\Visitor\User $role
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user