<?php
/**
* Command mode
*
* Encapsulate a request into an object so that you can parameterize clients 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<br/>";
}
Public Function Action1 ()
{
echo $this->_name. "Action1<br/>";
}
}
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 ("No.1");
$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
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