php设计模式 Command

来源:爱站网时间:2019-01-04编辑:网友分享
本文主要介绍php设计模式 Command,它将一个请求封装为一个对象,这样就可以用不同的请求、队列或日志请求参数化,希望爱站技术频道小编提供的内容对你学习有所帮助!
本文主要介绍php设计模式 Command,它将一个请求封装为一个对象,这样就可以用不同的请求、队列或日志请求参数化,希望爱站技术频道小编提供的内容对你学习有所帮助!
php设计模式 Command
复制代码 代码如下:

/**
* 命令模式
*
* 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
*/
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("No.1");
$objRecevier1 = new Receiver("No.2");
$objRecevier2 = new Receiver("No.3");

$objCommand = new ConcreteCommand($objRecevier);
$objCommand1 = new ConcreteCommand1($objRecevier);
$objCommand2 = new ConcreteCommand($objRecevier1);
$objCommand3 = new ConcreteCommand1($objRecevier1);
$objCommand4 = new ConcreteCommand2($objRecevier2); // 使用 Recevier的两个方法

$objInvoker = new Invoker();
$objInvoker->setCommand($objCommand);
$objInvoker->setCommand($objCommand1);
$objInvoker->executeCommand();
$objInvoker->removeCommand($objCommand1);
$objInvoker->executeCommand();

$objInvoker->setCommand($objCommand2);
$objInvoker->setCommand($objCommand3);
$objInvoker->setCommand($objCommand4);
$objInvoker->executeCommand();
通过爱站技术频道小编介绍的php设计模式 Command,相信大家都有了一定的了解,想要了解更多的技术内容,请继续关注爱站技术频道吧!

上一篇:php中转义mysql语句的实现代码

下一篇:浅谈使用 Yii2 AssetBundle 中 $publishOptions 的正确姿势

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载