使用PHP實現命令模式

來源:互聯網
上載者:User

標籤:echo   on()   main   提交   logs   狀態   角色   cti   ons   

<?php/** * 命令模式 2010-08-21 sz * @author phppan.p#gmail.com  http://www.phppan.com                                                        * 哥學社成員(http://www.blog-brother.com/) * @package design pattern */ /** * 命令角色 */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(); ?>

 

【命令模式協作】
1、Client建立一個ConcreteCommand對象並指定它的Receiver對象
2、某InvokerObject Storage Service該ConcreteCommand對象
3、該Invoker通過調用Command對象的execute操作來提交一個請求。若該命令是可撤消的,ConcreteCommand就在執行execute操作之前儲存目前狀態以用於取消命令。
4、ConcreteCommand對象對調用它的Receiver的一些操作以執行該請求。

 

轉自:

http://www.phppan.com/2010/08/php-design-pattern-15-comman/

使用PHP實現命令模式(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.