mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
start a restructure
This commit is contained in:
15
Behavioral/Command/CommandInterface.php
Normal file
15
Behavioral/Command/CommandInterface.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Command;
|
||||
|
||||
/**
|
||||
* class CommandInterface
|
||||
*/
|
||||
interface CommandInterface
|
||||
{
|
||||
/**
|
||||
* this is the most important method in the Command pattern,
|
||||
* The Receiver goes in the constructor.
|
||||
*/
|
||||
public function execute();
|
||||
}
|
36
Behavioral/Command/HelloCommand.php
Normal file
36
Behavioral/Command/HelloCommand.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Command;
|
||||
|
||||
/**
|
||||
* This concrete command calls "print" on the Receiver, but an external
|
||||
* invoker just know he can call "execute"
|
||||
*/
|
||||
class HelloCommand implements CommandInterface
|
||||
{
|
||||
/**
|
||||
* @var Receiver
|
||||
*/
|
||||
protected $output;
|
||||
|
||||
/**
|
||||
* Each concrete command is builded with different receivers.
|
||||
* Can be one, many, none or even other Command in parameters
|
||||
*
|
||||
* @param Receiver $console
|
||||
*/
|
||||
public function __construct(Receiver $console)
|
||||
{
|
||||
$this->output = $console;
|
||||
}
|
||||
|
||||
/**
|
||||
* execute and output "Hello World"
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
// sometimes, there is no receiver and this is the command which
|
||||
// does all the work
|
||||
$this->output->write('Hello World');
|
||||
}
|
||||
}
|
36
Behavioral/Command/Invoker.php
Normal file
36
Behavioral/Command/Invoker.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Command;
|
||||
|
||||
/**
|
||||
* Invoker is using the command given to it.
|
||||
* Example : an Application in SF2
|
||||
*/
|
||||
class Invoker
|
||||
{
|
||||
/**
|
||||
* @var CommandInterface
|
||||
*/
|
||||
protected $command;
|
||||
|
||||
/**
|
||||
* In the invoker we find this kind of method for subscribing the command.
|
||||
* There can be also a stack, a list, a fixed set...
|
||||
*
|
||||
* @param CommandInterface $cmd
|
||||
*/
|
||||
public function setCommand(CommandInterface $cmd)
|
||||
{
|
||||
$this->command = $cmd;
|
||||
}
|
||||
|
||||
/**
|
||||
* executes the command
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// here is a key feature of the invoker
|
||||
// the invoker is the same whatever is the command
|
||||
$this->command->execute();
|
||||
}
|
||||
}
|
17
Behavioral/Command/README.md
Normal file
17
Behavioral/Command/README.md
Normal file
@@ -0,0 +1,17 @@
|
||||
# Command
|
||||
|
||||
## Purpose
|
||||
|
||||
To encapsulate invocation and decoupling.
|
||||
|
||||
We have an Invoker and a Receiver. This pattern uses a "Command" to delegate the method call against the Receiver and presents the same method "execute".
|
||||
Therefore, the Invoker just knows to call "execute" to process the Command of the client. The Receiver is decoupled from the Invoker.
|
||||
|
||||
The second aspect of this pattern is the undo(), which undoes the method execute().
|
||||
Command can also be aggregated to combine more complex commands with minimum copy-paste and relying on composition over inheritance.
|
||||
|
||||
## Examples
|
||||
|
||||
* A text editor : all events are Command which can be undone, stacked and saved.
|
||||
* Symfony2: SF2 Commands that can be run from the CLI are built with just the Command pattern in mind
|
||||
* big CLI tools use subcommands to distribute various tasks and pack them in "modules", each of these can be implemented with the Command pattern (e.g. vagrant)
|
17
Behavioral/Command/Receiver.php
Normal file
17
Behavioral/Command/Receiver.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Command;
|
||||
|
||||
/**
|
||||
* Receiver is specific service with its own contract and can be only concrete
|
||||
*/
|
||||
class Receiver
|
||||
{
|
||||
/**
|
||||
* @param string $str
|
||||
*/
|
||||
public function write($str)
|
||||
{
|
||||
echo $str;
|
||||
}
|
||||
}
|
31
Behavioral/Command/Test/CommandTest.php
Normal file
31
Behavioral/Command/Test/CommandTest.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace DesignPatterns\Tests\Command;
|
||||
|
||||
use DesignPatterns\Command\Invoker;
|
||||
use DesignPatterns\Command\Receiver;
|
||||
use DesignPatterns\Command\HelloCommand;
|
||||
|
||||
/**
|
||||
* CommandTest has the role of the Client in the Command Pattern
|
||||
*/
|
||||
class CommandTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $invoker;
|
||||
protected $receiver;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
// this is the context of the application
|
||||
$this->invoker = new Invoker();
|
||||
$this->receiver = new Receiver();
|
||||
}
|
||||
|
||||
public function testInvocation()
|
||||
{
|
||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
||||
$this->expectOutputString('Hello World');
|
||||
$this->invoker->run();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user