php设计模式 Command(命令模式)
2015-01-24信息快讯网
将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
<?php/**
* 命令模式
*
* 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
*/
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");
$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();
[/code]
PHP gbk环境下json_dencode传送来的汉字
域名和cookie问题(域名后缀)
Linux下CoreSeek及PHP扩展模块的安装
PHP 之Section与Cookie使用总结
PHP中防止直接访问或查看或下载config.php文件的方法
php设计模式 Command(命令模式)
php开发过程中关于继承的使用方法分享
php学习笔记 面向对象中[接口]与[多态性]的应用
php自动获取字符串编码函数mb_detect_encoding
php file_put_contents()功能函数(集成了fopen、fwrite、fclose)
php利用cookie实现访问次数统计代码
php异常:Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE eval()'d code error
php cookies中删除的一般赋值方法