php 命令链模式

来源:互联网 发布:赛捷软件 编辑:程序博客网 时间:2024/06/03 08:08
<?phpheader("content-type:text/html;charset=utf-8");// ==================php :命令链模式 =============================interface ICommand{    function onCommand($name,$args);}class CommandChain{    private $_commands = array();    public function addCommand($cmd){        $this->_commands[] = $cmd;    }    public function runCommand($name,$args){        foreach ($this->_commands as $cmd) {            if($cmd->onCommand($name,$args) ) return;        }    }}class UserCommand Implements ICommand{    public function onCommand($name,$args){        if($name == 'addUser'){            echo "运行命令 addUser";        }else{            echo "运行错误!";            return;        }    }}$a = new CommandChain();$a->addCommand(new UserCommand());$a->runCommand('addUser',null);

0 0