php 命令链模式

<?php
header("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);

你可能感兴趣的:(php 命令链模式)