zoukankan      html  css  js  c++  java
  • 执行及描述任务-------命令模式

    定义

    命令模式:将请求封装成对象,以便使用不同的请求、日志、队列等参数化其他对象。命令模式也支持撤销操作

    uml

    代码实现

    abstract class Command{
        abstract function execute(CommandContext $context);
    }
    //FeedBackCommand.php
    class FeedbackCommand extends Command{
        function execute(CommandContext $context){
            $msgSystem = Register::ferMessageSystem();
    
            $email = $context->get('email');
            $msg = $context->get('msg');
            $topic = $context->get('topic');
    
            $result = $msgSystem->send($email,$msg,$topic);
            if(!$result){
                $context->setError($msgSystem->getError());
            }else{
                return true;
            }
        }
    }
    abstract class Command{
        abstract function execute(CommandContext $context);
    }
    //命令对象
    class LoginCommand extends Command{
        function execute(CommandContext $context){
            $manger = Register::getAccessMananger();//虚拟类,处理用户登录系统的具体细节
            
            $user = $context->get('username');
            $pass = $context->get('pass');
    
            $user_obj = $manger->login($user,$pass);
            if(is_null($user_obj)){
                $context->setError($manger->getError());
                return false;
            }
            $context->addParam('user',$user_obj);
            return true;
        }
    }
    <?php
    //command.php命令模式
    class CommandContext {
        private $params = array();
        private $error='';
        function __construct(){
            $this->params = $_REQUEST;
        }
        function addParam($key,$value){
            $this->params[$key] = $value;
        }
    
        function get($key){
            return $this->params[$key];
        }
    
        function setError($error){
            $this->error = $error;
        }
    
        function getError(){
            return $this->error;
        }
    }
    
    
    class CommandNotFoundException extends Exception{}
    
    //client,实例化命令对象的客户端
    class CommandFactory{
        private static $dir = 'commands';
        static function getCommand($action='Default'){
            if(preg_match('/W/', $action)){
                throw new Exception("illegal characters in action");
            }
            $class = UCFirst(strtolower($action))."Command";
            $file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php";
            if(!file_exists($file)){
                throw new CommandNotFoundException("could not find $file");
            }
            require_once($file);
            if(!class_exists($class)){
                throw new CommandNotFoundException("not $class class located");
            }
            $cmd = new $class();
            return $cmd;
        }
    }
    
    
    //invoker,使用命令对象的类
    class Controller{
        private $context;
        function __construct(){
            $this->context = new CommandContext();
        }
    
        function getContext(){
            return $this->context;
        }
    
        function process(){
            $cmd = CommandFactory::getCommand($this->context->get('action'));
            if(!$cmd->execute($this->context)){
                //处理失败
            }else{
                //成功
                //分发视图
            }
        }
    }
    
    
    $controller = new Controller();
    //模拟用户请求
    $context = $controller->getContext();
    $context->addParam('action','login');
    $context->addParam('username','bob');
    $context->addParam('pass','tiddlies');
    $controller->process();
    ?>
  • 相关阅读:
    性能优化之无阻塞加载脚步方法比较
    谈谈JS中的函数节流
    JS继承类相关试题
    JS继承之寄生类继承
    JS继承之借用构造函数继承和组合继承
    JS继承之原型继承
    谈谈JS的观察者模式(自定义事件)
    JS图片上传预览插件制作(兼容到IE6)
    前端HTML5几种存储方式的总结
    angularJS实用的开发技巧
  • 原文地址:https://www.cnblogs.com/rcjtom/p/6073686.html
Copyright © 2011-2022 走看看