侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 7年前   154

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

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面向对象之命令模式(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

您可能感兴趣的文章:

  • thinkPHP中钩子的两种配置调用方法详解
  • thinkPHP中钩子的使用方法实例分析
  • PHP钩子与简单分发方式实例分析
  • thinkPHP基于反射实现钩子的方法分析
  • php面向对象值单例模式
  • PHP面向对象程序设计组合模式与装饰模式详解
  • 老生常谈PHP面向对象之注册表模式
  • 老生常谈PHP面向对象之解释器模式
  • PHP面向对象之事务脚本模式(详解)
  • 浅谈PHP面向对象之访问者模式+组合模式
  • PHP钩子实现方法解析


  • 上一条:
    php 人员权限管理(RBAC)实例(推荐)
    下一条:
    php实现查询功能(数据访问)
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(95个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客