mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 20:20:15 +02:00
PHP7 Command
This commit is contained in:
@@ -4,14 +4,14 @@ namespace DesignPatterns\Behavioral\Command;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This concrete command tweaks receiver to add current date to messages
|
* This concrete command tweaks receiver to add current date to messages
|
||||||
* invoker just knows that it can call "execute".
|
* invoker just knows that it can call "execute"
|
||||||
*/
|
*/
|
||||||
class AddMessageDateCommand implements UndoableCommandInterface
|
class AddMessageDateCommand implements UndoableCommandInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Receiver
|
* @var Receiver
|
||||||
*/
|
*/
|
||||||
protected $output;
|
private $output;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each concrete command is built with different receivers.
|
* Each concrete command is built with different receivers.
|
||||||
|
@@ -2,9 +2,6 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Command;
|
namespace DesignPatterns\Behavioral\Command;
|
||||||
|
|
||||||
/**
|
|
||||||
* class CommandInterface.
|
|
||||||
*/
|
|
||||||
interface CommandInterface
|
interface CommandInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
|
@@ -4,18 +4,18 @@ namespace DesignPatterns\Behavioral\Command;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This concrete command calls "print" on the Receiver, but an external
|
* This concrete command calls "print" on the Receiver, but an external
|
||||||
* invoker just knows that it can call "execute".
|
* invoker just knows that it can call "execute"
|
||||||
*/
|
*/
|
||||||
class HelloCommand implements CommandInterface
|
class HelloCommand implements CommandInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var Receiver
|
* @var Receiver
|
||||||
*/
|
*/
|
||||||
protected $output;
|
private $output;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each concrete command is built with different receivers.
|
* Each concrete command is built with different receivers.
|
||||||
* There can be one, many or completely no receivers, but there can be other commands in the parameters.
|
* There can be one, many or completely no receivers, but there can be other commands in the parameters
|
||||||
*
|
*
|
||||||
* @param Receiver $console
|
* @param Receiver $console
|
||||||
*/
|
*/
|
||||||
@@ -29,8 +29,7 @@ class HelloCommand implements CommandInterface
|
|||||||
*/
|
*/
|
||||||
public function execute()
|
public function execute()
|
||||||
{
|
{
|
||||||
// sometimes, there is no receiver and this is the command which
|
// sometimes, there is no receiver and this is the command which does all the work
|
||||||
// does all the work
|
|
||||||
$this->output->write('Hello World');
|
$this->output->write('Hello World');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -11,11 +11,11 @@ class Invoker
|
|||||||
/**
|
/**
|
||||||
* @var CommandInterface
|
* @var CommandInterface
|
||||||
*/
|
*/
|
||||||
protected $command;
|
private $command;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In the invoker we find this kind of method for subscribing the 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...
|
* There can be also a stack, a list, a fixed set ...
|
||||||
*
|
*
|
||||||
* @param CommandInterface $cmd
|
* @param CommandInterface $cmd
|
||||||
*/
|
*/
|
||||||
@@ -25,12 +25,10 @@ class Invoker
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* executes the command.
|
* executes the command; the invoker is the same whatever is the command
|
||||||
*/
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
// here is a key feature of the invoker
|
|
||||||
// the invoker is the same whatever is the command
|
|
||||||
$this->command->execute();
|
$this->command->execute();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -7,14 +7,20 @@ namespace DesignPatterns\Behavioral\Command;
|
|||||||
*/
|
*/
|
||||||
class Receiver
|
class Receiver
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
private $enableDate = false;
|
private $enableDate = false;
|
||||||
|
|
||||||
private $output = array();
|
/**
|
||||||
|
* @var string[]
|
||||||
|
*/
|
||||||
|
private $output = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $str
|
* @param string $str
|
||||||
*/
|
*/
|
||||||
public function write($str)
|
public function write(string $str)
|
||||||
{
|
{
|
||||||
if ($this->enableDate) {
|
if ($this->enableDate) {
|
||||||
$str .= ' ['.date('Y-m-d').']';
|
$str .= ' ['.date('Y-m-d').']';
|
||||||
@@ -23,13 +29,13 @@ class Receiver
|
|||||||
$this->output[] = $str;
|
$this->output[] = $str;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getOutput()
|
public function getOutput(): string
|
||||||
{
|
{
|
||||||
return implode("\n", $this->output);
|
return join("\n", $this->output);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Enable receiver to display message date.
|
* Enable receiver to display message date
|
||||||
*/
|
*/
|
||||||
public function enableDate()
|
public function enableDate()
|
||||||
{
|
{
|
||||||
@@ -37,7 +43,7 @@ class Receiver
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disable receiver to display message date.
|
* Disable receiver to display message date
|
||||||
*/
|
*/
|
||||||
public function disableDate()
|
public function disableDate()
|
||||||
{
|
{
|
||||||
|
@@ -6,31 +6,15 @@ use DesignPatterns\Behavioral\Command\HelloCommand;
|
|||||||
use DesignPatterns\Behavioral\Command\Invoker;
|
use DesignPatterns\Behavioral\Command\Invoker;
|
||||||
use DesignPatterns\Behavioral\Command\Receiver;
|
use DesignPatterns\Behavioral\Command\Receiver;
|
||||||
|
|
||||||
/**
|
|
||||||
* CommandTest has the role of the Client in the Command Pattern.
|
|
||||||
*/
|
|
||||||
class CommandTest extends \PHPUnit_Framework_TestCase
|
class CommandTest extends \PHPUnit_Framework_TestCase
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var Invoker
|
|
||||||
*/
|
|
||||||
protected $invoker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Receiver
|
|
||||||
*/
|
|
||||||
protected $receiver;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
|
||||||
$this->invoker = new Invoker();
|
|
||||||
$this->receiver = new Receiver();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testInvocation()
|
public function testInvocation()
|
||||||
{
|
{
|
||||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
$invoker = new Invoker();
|
||||||
$this->invoker->run();
|
$receiver = new Receiver();
|
||||||
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
|
||||||
|
$invoker->setCommand(new HelloCommand($receiver));
|
||||||
|
$invoker->run();
|
||||||
|
$this->assertEquals($receiver->getOutput(), 'Hello World');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,44 +6,27 @@ use DesignPatterns\Behavioral\Command\AddMessageDateCommand;
|
|||||||
use DesignPatterns\Behavioral\Command\HelloCommand;
|
use DesignPatterns\Behavioral\Command\HelloCommand;
|
||||||
use DesignPatterns\Behavioral\Command\Invoker;
|
use DesignPatterns\Behavioral\Command\Invoker;
|
||||||
use DesignPatterns\Behavioral\Command\Receiver;
|
use DesignPatterns\Behavioral\Command\Receiver;
|
||||||
use PHPUnit_Framework_TestCase;
|
|
||||||
|
|
||||||
/**
|
class UndoableCommandTest extends \PHPUnit_Framework_TestCase
|
||||||
* UndoableCommandTest has the role of the Client in the Command Pattern.
|
|
||||||
*/
|
|
||||||
class UndoableCommandTest extends PHPUnit_Framework_TestCase
|
|
||||||
{
|
{
|
||||||
/**
|
|
||||||
* @var Invoker
|
|
||||||
*/
|
|
||||||
protected $invoker;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Receiver
|
|
||||||
*/
|
|
||||||
protected $receiver;
|
|
||||||
|
|
||||||
protected function setUp()
|
|
||||||
{
|
|
||||||
$this->invoker = new Invoker();
|
|
||||||
$this->receiver = new Receiver();
|
|
||||||
}
|
|
||||||
|
|
||||||
public function testInvocation()
|
public function testInvocation()
|
||||||
{
|
{
|
||||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
$invoker = new Invoker();
|
||||||
$this->invoker->run();
|
$receiver = new Receiver();
|
||||||
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
|
||||||
|
|
||||||
$messageDateCommand = new AddMessageDateCommand($this->receiver);
|
$invoker->setCommand(new HelloCommand($receiver));
|
||||||
|
$invoker->run();
|
||||||
|
$this->assertEquals($receiver->getOutput(), 'Hello World');
|
||||||
|
|
||||||
|
$messageDateCommand = new AddMessageDateCommand($receiver);
|
||||||
$messageDateCommand->execute();
|
$messageDateCommand->execute();
|
||||||
|
|
||||||
$this->invoker->run();
|
$invoker->run();
|
||||||
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']');
|
$this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']');
|
||||||
|
|
||||||
$messageDateCommand->undo();
|
$messageDateCommand->undo();
|
||||||
|
|
||||||
$this->invoker->run();
|
$invoker->run();
|
||||||
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World");
|
$this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,14 +2,10 @@
|
|||||||
|
|
||||||
namespace DesignPatterns\Behavioral\Command;
|
namespace DesignPatterns\Behavioral\Command;
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface UndoableCommandInterface.
|
|
||||||
*/
|
|
||||||
interface UndoableCommandInterface extends CommandInterface
|
interface UndoableCommandInterface extends CommandInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* This method is used to undo change made by command execution
|
* This method is used to undo change made by command execution
|
||||||
* The Receiver goes in the constructor.
|
|
||||||
*/
|
*/
|
||||||
public function undo();
|
public function undo();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user