mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-11 02:16:28 +02:00
88 - Added undo example for command pattern
This commit is contained in:
46
Behavioral/Command/AddMessageDateCommand.php
Normal file
46
Behavioral/Command/AddMessageDateCommand.php
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This concrete command tweaks receiver to add current date to messages
|
||||||
|
* invoker just knows that it can call "execute".
|
||||||
|
*/
|
||||||
|
class AddMessageDateCommand implements CommandInterface, UndoableCommandInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @var Receiver
|
||||||
|
*/
|
||||||
|
protected $output;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
||||||
|
*
|
||||||
|
* @param Receiver $console
|
||||||
|
*/
|
||||||
|
public function __construct(Receiver $console)
|
||||||
|
{
|
||||||
|
$this->output = $console;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute and make receiver to enable displaying messages date.
|
||||||
|
*/
|
||||||
|
public function execute()
|
||||||
|
{
|
||||||
|
// sometimes, there is no receiver and this is the command which
|
||||||
|
// does all the work
|
||||||
|
$this->output->enableDate();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Undo the command and make receiver to disable displaying messages date.
|
||||||
|
*/
|
||||||
|
public function undo()
|
||||||
|
{
|
||||||
|
// sometimes, there is no receiver and this is the command which
|
||||||
|
// does all the work
|
||||||
|
$this->output->disableDate();
|
||||||
|
}
|
||||||
|
}
|
@ -7,11 +7,40 @@ namespace DesignPatterns\Behavioral\Command;
|
|||||||
*/
|
*/
|
||||||
class Receiver
|
class Receiver
|
||||||
{
|
{
|
||||||
|
private $enableDate = false;
|
||||||
|
|
||||||
|
private $output = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $str
|
* @param string $str
|
||||||
*/
|
*/
|
||||||
public function write($str)
|
public function write($str)
|
||||||
{
|
{
|
||||||
echo $str;
|
if ($this->enableDate) {
|
||||||
|
$str .= ' ['.date('Y-m-d').']';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->output[] = $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getOutput()
|
||||||
|
{
|
||||||
|
return implode("\n", $this->output);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enable receiver to display message date.
|
||||||
|
*/
|
||||||
|
public function enableDate()
|
||||||
|
{
|
||||||
|
$this->enableDate = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disable receiver to display message date.
|
||||||
|
*/
|
||||||
|
public function disableDate()
|
||||||
|
{
|
||||||
|
$this->enableDate = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,7 @@ class CommandTest extends \PHPUnit_Framework_TestCase
|
|||||||
public function testInvocation()
|
public function testInvocation()
|
||||||
{
|
{
|
||||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
||||||
$this->expectOutputString('Hello World');
|
|
||||||
$this->invoker->run();
|
$this->invoker->run();
|
||||||
|
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
49
Behavioral/Command/Tests/UndoableCommandTest.php
Normal file
49
Behavioral/Command/Tests/UndoableCommandTest.php
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Command\Tests;
|
||||||
|
|
||||||
|
use DesignPatterns\Behavioral\Command\AddMessageDateCommand;
|
||||||
|
use DesignPatterns\Behavioral\Command\HelloCommand;
|
||||||
|
use DesignPatterns\Behavioral\Command\Invoker;
|
||||||
|
use DesignPatterns\Behavioral\Command\Receiver;
|
||||||
|
use 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()
|
||||||
|
{
|
||||||
|
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
||||||
|
$this->invoker->run();
|
||||||
|
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
||||||
|
|
||||||
|
$messageDateCommand = new AddMessageDateCommand($this->receiver);
|
||||||
|
$messageDateCommand->execute();
|
||||||
|
|
||||||
|
$this->invoker->run();
|
||||||
|
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']');
|
||||||
|
|
||||||
|
$messageDateCommand->undo();
|
||||||
|
|
||||||
|
$this->invoker->run();
|
||||||
|
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World");
|
||||||
|
}
|
||||||
|
}
|
15
Behavioral/Command/UndoableCommandInterface.php
Normal file
15
Behavioral/Command/UndoableCommandInterface.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace DesignPatterns\Behavioral\Command;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface UndoableCommandInterface.
|
||||||
|
*/
|
||||||
|
interface UndoableCommandInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* This method is used to undo change made by command execution
|
||||||
|
* The Receiver goes in the constructor.
|
||||||
|
*/
|
||||||
|
public function undo();
|
||||||
|
}
|
Reference in New Issue
Block a user