mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-29 03:00:15 +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();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user