/**
* Command mode
*
* Encapsulate a request as an object so that you can parameterize the customer with different requests, exclude or log request logs, and support actions that can be canceled
*/
Interface Command
{
Public function execute ();
}
Class Invoker
{
Private $_command = Array ();
Public Function SetCommand ($command) {
$this->_command[] = $command;
}
Public Function ExecuteCommand ()
{
foreach ($this->_command as $command)
{
$command->execute ();
}
}
Public Function Removecommand ($command)
{
$key = Array_search ($command, $this->_command);
if ($key!== false)
{
unset ($this->_command[$key]);
}
}
}
Class Receiver
{
Private $_name = null;
Public function __construct ($name) {
$this->_name = $name;
}
Public Function Action ()
{
echo $this->_name. "Action
";
}
Public Function Action1 ()
{
echo $this->_name. "Action1
";
}
}
Class Concretecommand implements Command
{
Private $_receiver;
Public function __construct ($receiver)
{
$this->_receiver = $receiver;
}
Public Function Execute ()
{
$this->_receiver->action ();
}
}
Class ConcreteCommand1 implements Command
{
Private $_receiver;
Public function __construct ($receiver)
{
$this->_receiver = $receiver;
}
Public Function Execute ()
{
$this->_receiver->action1 ();
}
}
Class ConcreteCommand2 implements Command
{
Private $_receiver;
Public function __construct ($receiver)
{
$this->_receiver = $receiver;
}
Public Function Execute ()
{
$this->_receiver->action ();
$this->_receiver->action1 ();
}
}
$objRecevier = new Receiver ("the");
$objRecevier 1 = new Receiver ("No.2");
$objRecevier 2 = new Receiver ("No.3");
$objCommand = new Concretecommand ($objRecevier);
$objCommand 1 = new ConcreteCommand1 ($objRecevier);
$objCommand 2 = new Concretecommand ($objRecevier 1);
$objCommand 3 = new ConcreteCommand1 ($objRecevier 1);
$objCommand 4 = new ConcreteCommand2 ($objRecevier 2); Two ways to use Recevier
$objInvoker = new Invoker ();
$objInvoker->setcommand ($objCommand);
$objInvoker->setcommand ($objCommand 1);
$objInvoker->executecommand ();
$objInvoker->removecommand ($objCommand 1);
$objInvoker->executecommand ();
$objInvoker->setcommand ($objCommand 2);
$objInvoker->setcommand ($objCommand 3);
$objInvoker->setcommand ($objCommand 4);
$objInvoker->executecommand ();
[/code]
The above describes the come and get it PHP design mode command command mode, including come and get it content, want to be interested in PHP tutorial friends helpful.