mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-08-02 21:17:29 +02:00
cs Command
This commit is contained in:
@@ -23,7 +23,7 @@ namespace DesignPatterns\Command;
|
|||||||
* can be implemented with the Command pattern (e.g. vagrant)
|
* can be implemented with the Command pattern (e.g. vagrant)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
interface Command
|
interface CommandInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* this is the most important method in the Command pattern,
|
* this is the most important method in the Command pattern,
|
||||||
|
@@ -6,25 +6,31 @@ namespace DesignPatterns\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 know he can call "execute"
|
* invoker just know he can call "execute"
|
||||||
*/
|
*/
|
||||||
class HelloCommand implements Command
|
class HelloCommand implements CommandInterface
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var Receiver
|
||||||
|
*/
|
||||||
protected $output;
|
protected $output;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Each concrete command is builded with different receivers.
|
* Each concrete command is builded with different receivers.
|
||||||
* Can be one, many, none or even other Command in parameters
|
* Can be one, many, none or even other Command in parameters
|
||||||
|
*
|
||||||
|
* @param Receiver $console
|
||||||
*/
|
*/
|
||||||
public function __construct(Receiver $console)
|
public function __construct(Receiver $console)
|
||||||
{
|
{
|
||||||
$this->output = $console;
|
$this->output = $console;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* execute and output "Hello World"
|
||||||
|
*/
|
||||||
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');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@@ -12,23 +12,29 @@ namespace DesignPatterns\Command;
|
|||||||
*/
|
*/
|
||||||
class Invoker
|
class Invoker
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var CommandInterface
|
||||||
|
*/
|
||||||
protected $command;
|
protected $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
|
||||||
*/
|
*/
|
||||||
public function setCommand(Command $cmd)
|
public function setCommand(CommandInterface $cmd)
|
||||||
{
|
{
|
||||||
$this->command = $cmd;
|
$this->command = $cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* executes the command
|
||||||
|
*/
|
||||||
public function run()
|
public function run()
|
||||||
{
|
{
|
||||||
// here is a key feature of the invoker
|
// here is a key feature of the invoker
|
||||||
// the invoker is the same whatever is the command
|
// the invoker is the same whatever is the command
|
||||||
$this->command->execute();
|
$this->command->execute();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
@@ -11,10 +11,11 @@ namespace DesignPatterns\Command;
|
|||||||
*/
|
*/
|
||||||
class Receiver
|
class Receiver
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @param string $str
|
||||||
|
*/
|
||||||
public function write($str)
|
public function write($str)
|
||||||
{
|
{
|
||||||
echo $str;
|
echo $str;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
|
||||||
|
Reference in New Issue
Block a user