php 宏命令模式

/*
 宏命令
新增复制命令和粘贴命令,然后添加到宏命令中去
*/

/**
 *命令角色
 *@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();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值