当前位置:Gxlcms > php框架 > 老生常谈PHP面向对象之命令模式(必看篇)

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

时间:2021-07-01 10:21:17 帮助过:29人阅读

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

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

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

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

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

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

直接看代码:

  1. //命令类
  2. abstract class Command {
  3.   abstract function execute(CommandContext $context);
  4. }
  5. class LoginCommand extends Command{       //处理用户登陆信息的命令类
  6.   function execute (CommandCotext $context){    //CommandCotext 是一个处理用户请求数据和后台回馈数据的类
  7.     $manager = Registry::getAccessManager();  //原文代码中并没有具体的实现,但说明了这是一个处理用户登陆信息的业务逻辑类
  8.     $user = $context->get('username');
  9.     $pass = $context->get('pass');
  10.     $user_obj = $manager->login($user,$pass);
  11.     if(is_null($user_obj)){
  12.       $context->setError($manager->getError);
  13.       return false;
  14.     }
  15.     $context->addParam('user',$user_obj);
  16.     return true;               //用户登陆成功返回true
  17.   }
  18. }
  19. class FeedbackCommand extends Command{        //发送邮件的命令类
  20.   function execute(CommandContext $context){
  21.     $msgSystem = Registry::getMessageSystem();
  22.     $email = $context->get('email');
  23.     $msg = $context->get('msg');
  24.     $topic = $context->get('topci');
  25.     $result = $msgSystem->send($email,$msg,$topic);
  26.     if(!$result){
  27.       $context->setError($msgSystem->getError());
  28.       return false;
  29.     }
  30.     return true;
  31.   }
  32. }
  33. //用户请求数据类
  34. class CommandContext {
  35.   private $params = array();
  36.   private $error = '';
  37.   function __construct (){
  38.   $this->params = $_REQUEST;
  39. }
  40. function addParam($key,$val){
  41.   $this->params[$key] = $val;
  42. }
  43. function get($key){
  44.   return $this->params[$key];
  45. }
  46. function setError($error){
  47.   $this->error = $error;
  48. }
  49. function getError(){
  50.   return $this->error;
  51. }
  52. }
  53. //命令类工厂,这个类根据用户请求数据中的action来生成命令类
  54. class CommandNotFoundException extends Exception {}
  55. class CommandFactory {
  56.   private static $dir = 'commands';
  57.   static function getCommand($action='Default'){
  58.     if(preg_match('/\w',$action)){
  59.       throw new Exception("illegal characters in action");
  60.     }
  61.     $class = UCFirst(strtolower($action))."Command";
  62.     $file = self::$dir.DIRECTORY_SEPARATOR."{$class}.php"; //DIRECTORY_SEPARATOR代表'/',这是一个命令类文件的路径
  63.     if(!file_exists($file)){
  64.       throw new CommandNotFoundException("could not find '$file'");
  65.     }
  66.     require_once($file);
  67.     if(!class_exists($class)){
  68.       throw new CommandNotFoundException("no '$class' class located");
  69.     }
  70.     $cmd = new $class();
  71.     return $cmd;
  72.   }
  73. }
  74. //调用者类,相当于一个司令部它统筹所有的资源
  75. class Controller{
  76.   private $context;
  77.   function __construct(){
  78.     $this->context = new CommandContext(); //用户请求数据
  79.   }
  80.   function getContext(){
  81.     return $this->context;
  82.   }
  83.   function process(){
  84.     $cmd = CommandFactory::getCommand($this->context->get('action'));    //通过命令工厂类来获取命令类
  85.     if(!$comd->execute($this->context)){                      
  86.       //处理失败
  87.     } else {
  88.       //成功
  89.       // 分发视图
  90.     }
  91.   }
  92. }
  93. // 客户端
  94. $controller = new Controller();
  95. //伪造用户请求,真实的场景中这些参数应该是通过post或get的方式获取的,貌似又废话了:)
  96. $context = $controller->getContext();
  97. $context->addParam('action','login');
  98. $context->addParam('username','bob');
  99. $context->addParam('pass','tiddles');
  100. $controller->process();

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

人气教程排行