<?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]
/**
* 命令模式
*
* 将一个请求封装为一个对象从而使你可用不同的请求对客户进行参数化,对请求排除或记录请求日志,以及支持可取消的操作
*/
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]
标签:
设计模式
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“php设计模式 Command(命令模式)”评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?