时间:2021-07-01 10:21:17 帮助过:14人阅读
<?php
// 命令模式
interface Command
{
public function execute();
}
/**
* concrete command, 具体的命令
*/
class ConcreteCommand implements Command
{
private $receiver;
public function construct(Receiver $r) {
$this->receiver = $r;
}
public function execute() {
$this->receiver->doAction();
}
}
/**
* 接收者, 命令的执行者
*/
class Receiver
{
public function doAction() {
echo 'Action has been taken!<br/>';
}
}
/**
* 请求者, 命令的请求者
*/
class Invoker
{
private $cmd;
public function construct(Command $cmd) {
$this->cmd = $cmd;
}
/**
* call command execute
*/
public function action() {
$this->cmd->execute();
}
}
// test code
$r = new Receiver();
$cmd = new ConcreteCommand($r);
$invoker = new Invoker($cmd);
$invoker->action();以上就是PHP命令模式实现简单的代码示例的详细内容,更多请关注Gxl网其它相关文章!