DDR爱好者之家 Design By 杰米
这个模式主要由 命令类、用户请求数据类、业务逻辑类、命令类工厂类及调用类构成,各个类的作用概括如下:
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面向对象之命令模式(必看篇)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2025年03月15日
2025年03月15日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]