<?php
/**
* 命令角色
*/
interface Command {
/**
* 执行方法
*/
public function execute();
}
/**
* 具体命令角色
*/
class ConcreteCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
/**
* 执行方法
*/
public function execute() {
$this->_receiver->action();
}
}
/**
* 接收者角色
*/
class Receiver {
/* 接收者名称 */
private $_name;
public function __construct($name) {
$this->_name = $name;
}
/**
* 行动方法
*/
public function action() {
echo $this->_name, ' do action.<br />';
}
}
/**
* 请求者角色
*/
class Invoker {
private $_command;
public function __construct(Command $command) {
$this->_command = $command;
}
public function action() {
$this->_command->execute();
}
}
/**
* 客户端
*/
class Client {
/**
* Main program.
*/
public static function main() {
$receiver = new Receiver('phpppan');
$command = new ConcreteCommand($receiver);
$invoker = new Invoker($command);
$invoker->action();
}
}
Client::main();
?>
<?php
/**
* 命令角色
*/
interface Command {
/**
* 执行方法
*/
public function execute();
}
/**
* 宏命令接口
*/
interface MacroCommand extends Command {
/**
* 宏命令聚集的管理方法,可以删除一个成员命令
* @param Command $command
*/
public function remove(Command $command);
/**
* 宏命令聚集的管理方法,可以增加一个成员命令
* @param Command $command
*/
public function add(Command $command);
}
class DemoMacroCommand implements MacroCommand {
private $_commandList;
public function __construct() {
$this->_commandList = array();
}
public function remove(Command $command) {
$key = array_search($command, $this->_commandList);
if ($key === FALSE) {
return FALSE;
}
unset($this->_commandList[$key]);
return TRUE;
}
public function add(Command $command) {
return array_push($this->_commandList, $command);
}
public function execute() {
foreach ($this->_commandList as $command) {
$command->execute();
}
}
}
/**
* 具体拷贝命令角色
*/
class CopyCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
/**
* 执行方法
*/
public function execute() {
$this->_receiver->copy();
}
}
/**
* 具体拷贝命令角色
*/
class PasteCommand implements Command {
private $_receiver;
public function __construct(Receiver $receiver) {
$this->_receiver = $receiver;
}
/**
* 执行方法
*/
public function execute() {
$this->_receiver->paste();
}
}
/**
* 接收者角色
*/
class Receiver {
/* 接收者名称 */
private $_name;
public function __construct($name) {
$this->_name = $name;
}
/**
* 复制方法
*/
public function copy() {
echo $this->_name, ' do copy action.<br />';
}
/**
* 粘贴方法
*/
public function paste() {
echo $this->_name, ' do paste action.<br />';
}
}
/**
* 请求者角色
*/
class Invoker {
private $_command;
public function __construct(Command $command) {
$this->_command = $command;
}
public function action() {
$this->_command->execute();
}
}
/**
* 客户端
*/
class Client {
/**
* Main program.
*/
public static function main() {
$receiver = new Receiver('phpppan');
$pasteCommand = new PasteCommand($receiver);
$copyCommand = new CopyCommand($receiver);
$macroCommand = new DemoMacroCommand();
$macroCommand->add($copyCommand);
$macroCommand->add($pasteCommand);
$invoker = new Invoker($macroCommand);
$invoker->action();
$macroCommand->remove($copyCommand);
$invoker = new Invoker($macroCommand);
$invoker->action();
}
}
Client::main();
?>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有