当前位置: 移动技术网 > IT编程>开发语言>PHP > 老生常谈PHP面向对象之命令模式(必看篇)

老生常谈PHP面向对象之命令模式(必看篇)

2017年12月12日  | 移动技术网IT编程  | 我要评论

这个模式主要由 命令类、用户请求数据类、业务逻辑类、命令类工厂类及调用类构成,各个类的作用概括如下:

1、命令类:调用用户请求数据类和业务逻辑类;

2、用户请求数据类:获取用户请求数据及保存后台处理后返回的结果;

3、业务逻辑类:如以下的示例中验证用户登陆信息是否正确的功能等;

4、命令工厂类(我自己取的名字,哈哈):生成命令类的实例,这个类第一次看的时候我觉得有点屌,当然看了几遍了还是觉得很屌 :);

5、调用类:调用命令类,生成视图;

直接看代码:

//命令类
abstract class command {
  abstract function execute(commandcontext $context);
}

class logincommand extends command{       //处理用户登陆信息的命令类
  function execute (commandcotext $context){    //commandcotext 是一个处理用户请求数据和后台回馈数据的类
    $manager = registry::getaccessmanager();  //原文代码中并没有具体的实现,但说明了这是一个处理用户登陆信息的业务逻辑类
    $user = $context->get('username');
    $pass = $context->get('pass');
    $user_obj = $manager->login($user,$pass);
    if(is_null($user_obj)){
      $context->seterror($manager->geterror);
      return false;
    }
    $context->addparam('user',$user_obj);
    return true;               //用户登陆成功返回true
  }
}

class feedbackcommand extends command{        //发送邮件的命令类
  function execute(commandcontext $context){
    $msgsystem = registry::getmessagesystem();
    $email = $context->get('email');
    $msg = $context->get('msg');
    $topic = $context->get('topci');
    $result = $msgsystem->send($email,$msg,$topic);
    if(!$result){
      $context->seterror($msgsystem->geterror());
      return false;
    }
    return true;
  }
}

//用户请求数据类  
class commandcontext {
  private $params = array();
  private $error = '';

  function __construct (){
  $this->params = $_request;
}

function addparam($key,$val){
  $this->params[$key] = $val;
}

function get($key){
  return $this->params[$key];
}

function seterror($error){
  $this->error = $error;
}

function geterror(){
  return $this->error;
}
}


//命令类工厂,这个类根据用户请求数据中的action来生成命令类
class commandnotfoundexception extends exception {}

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"; //directory_separator代表'/',这是一个命令类文件的路径
    if(!file_exists($file)){
      throw new commandnotfoundexception("could not find '$file'");
    }
    require_once($file);
    if(!class_exists($class)){
      throw new commandnotfoundexception("no '$class' class located");
    }
    $cmd = new $class();
    return $cmd;
  }
}

//调用者类,相当于一个司令部它统筹所有的资源
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(!$comd->execute($this->context)){                      
      //处理失败
    } else {
      //成功
      // 分发视图
    }
  }
}

// 客户端
$controller = new controller();
//伪造用户请求,真实的场景中这些参数应该是通过post或get的方式获取的,貌似又废话了:)
$context = $controller->getcontext();
$context->addparam('action','login');
$context->addparam('username','bob');
$context->addparam('pass','tiddles');
$controller->process();

以上这篇老生常谈php面向对象之命令模式(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网