/*
宏命令
新增复制命令和粘贴命令,然后添加到宏命令中去
*/
/**
*命令角色
*@author li.yonghuan
*@version 2014.01.28
*/
interface Command {
/**
*执行方法
*/
public function execute();
}
/**
*宏命令接口
*@author li.yonghuan
*@version 2014.01.28
*/
interface MacroCommand extends Command {
/**
*添加成员命令
*@param $command Command
*/
public function add( Command $command );
/**
*移除一个成员命令
*@param $command Command
*/
public function remove( Command $command );
}
/**
*宏命令
*@author li.yonghuan
*@version 2014.01.28
*/
class DemoMacroCommand implements MacroCommand {
/**
*成员命令列表
*@var array
*/
private $commandList;
/**
*构造方法
*/
public function __construct() {
$this->commandList = array();
}
/**
*添加成员命令
*@param $command Command
*
*/
public function add( Command $command ) {
return array_push( $this->commandList, $command );
}
/**
*移除成员命令
*@param $command Command
*/
public function remove( Command $command ) {
$key = array_search( $command, $this->commandList );
if($key === false) {
return false;
}
unset( $this->commandList[$key] );
return true;
}
/**
*执行请求
*
*/
public function execute() {
foreach( $this->commandList as $command ) {
$command->execute();
}
}
}
/**
*具体拷贝命令角色
*@author li.yonghuan
*@version 2014.01.28
*/
class CopyCommand implements Command {
/**
*接收者
*@var Receiver
*/
private $receiver;
/**
*构造方法
*@param $receiver Receiver
*/
public function __construct( Receiver $receiver ) {
$this->receiver = $receiver;
}
/**
*执行命令
*
*/
public function execute() {
$this->receiver->copy();
}
}
/**
*粘贴命令
*@author li.yonghuan
*version 2014.01.28
*/
class PasteCommand implements Command {
/**
*接收者
*@var Receiver
*/
private $receiver;
/**
*构造方法
*@param $receiver Receiver
*/
public function __construct( Receiver $receiver ) {
$this->receiver = $receiver;
}
/**
*执行命令
*
*/
public function execute() {
$this->receiver->paste();
}
}
/**
*接收者
*@author li.yonghuan
*@version 2014.01.28
*/
class Receiver {
/**
*名称
*@var string
*/
private $name;
/**
*构造方法
*@param $name string
*/
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>';
}
}
/**
*请求者
*@author li.yonghuan
*@version 2014.01.28
*/
class Invoker {
/**
*命令对象
*@var Command
*/
private $command;
/**
*构造方法
*@param $command 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('liyh');
//具体的命令
$copyCommand = new CopyCommand( $receiver );
$pasteCommand = new PasteCommand( $receiver );
//添加到宏命令
$demoMacroCommand = new DemoMacroCommand();
$demoMacroCommand->add( $copyCommand );
$demoMacroCommand->add( $pasteCommand );
//移除复制命令对象
$demoMacroCommand->remove( $copyCommand );
//请求者
$invoker = new Invoker( $demoMacroCommand );
$invoker->action();
}
}
//测试
Client::main();
php 宏命令模式
最新推荐文章于 2021-11-16 08:53:54 发布