Come and get it PHP design mode command Command mode

Source: Internet
Author: User
/**
* 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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.