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


Posted in PHP onMay 24, 2017

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

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

PHP 相关文章推荐
php中$_SERVER[PHP_SELF] 和 $_SERVER[SCRIPT_NAME]之间的区别
Sep 05 PHP
PHP内核探索:变量概述
Jan 30 PHP
phpphp图片采集后按原路径保存图片示例
Feb 18 PHP
PHP对接微信公众平台消息接口开发流程教程
Mar 25 PHP
百度地图API应用之获取用户的具体位置
Jun 10 PHP
ThinkPHP入库出现两次反斜线转义及数据库类转义的解决方法
Nov 04 PHP
php接口数据加密、解密、验证签名
Mar 12 PHP
php计算税后工资的方法
Jul 28 PHP
PHP对象克隆clone用法示例
Sep 28 PHP
PHP关键特性之命名空间实例详解
May 06 PHP
PHP 的Opcache加速的使用方法
Dec 29 PHP
PHP简单实现记录网站访问量功能示例
Jun 06 PHP
php实现查询功能(数据访问)
May 23 #PHP
php批量删除操作(数据访问)
May 23 #PHP
[原创]PHP正则删除html代码中a标签并保留标签内容的方法
May 23 #PHP
php出租房数据管理及搜索页面
May 23 #PHP
Yii框架创建cronjob定时任务的方法分析
May 23 #PHP
php注册审核重点解析(数据访问)
May 23 #PHP
php + nginx项目中的权限详解
May 23 #PHP
You might like
PHP中的extract的作用分析
2008/04/09 PHP
PHP 批量更新网页内容实现代码
2010/01/05 PHP
Yii框架中jquery表单验证插件用法示例
2016/10/18 PHP
javascript 兼容FF的onmouseenter和onmouseleave的代码
2008/07/19 Javascript
js中的referrer返回上一页使用介绍
2013/09/26 Javascript
Javascript中的回调函数和匿名函数的回调示例介绍
2014/05/12 Javascript
使用jQuery将多条数据插入模态框的实现代码
2014/10/08 Javascript
node.js中的fs.unlinkSync方法使用说明
2014/12/15 Javascript
AngularJS基础知识笔记之过滤器
2015/05/10 Javascript
JavaScript数组的栈方法与队列方法详解
2016/05/26 Javascript
浅谈angular懒加载的一些坑
2016/08/20 Javascript
JQueryEasyUI框架下的combobox的取值和绑定的方法
2017/01/22 Javascript
webpack2.0配置postcss-loader的方法
2017/08/17 Javascript
使用Angular CLI进行单元测试和E2E测试的方法
2018/03/24 Javascript
微信小程序显示倒计时功能示例【测试可用】
2018/12/03 Javascript
详解vue中this.$emit()的返回值是什么
2019/04/07 Javascript
使用xampp将angular项目运行在web服务器的教程
2019/09/16 Javascript
[55:42]VG vs VGJ.T 2018国际邀请赛淘汰赛BO1 8.21
2018/08/22 DOTA
[31:47]夜魇凡尔赛茶话会 第三期01:选手知多少
2021/03/11 DOTA
python制作企业邮箱的爆破脚本
2016/10/05 Python
python difflib模块示例讲解
2017/09/13 Python
对python3 urllib包与http包的使用详解
2018/05/10 Python
python 3.6.4 安装配置方法图文教程
2018/09/18 Python
python3实现的zip格式压缩文件夹操作示例
2019/08/17 Python
wxpython绘制音频效果
2019/11/18 Python
Python importlib模块重载使用方法详解
2020/10/13 Python
2014年单位法制宣传日活动总结
2014/11/01 职场文书
敲诈同学钱财检讨书范文
2014/11/18 职场文书
加入学生会自荐书
2015/03/05 职场文书
2015年文员个人工作总结
2015/04/09 职场文书
旗帜观后感
2015/06/08 职场文书
交通安全学习心得体会
2016/01/18 职场文书
python删除csv文件的行列
2021/04/06 Python
Go语言基础知识点介绍
2021/07/04 Golang
SQL Server查询某个字段在哪些表中存在
2022/03/03 SQL Server
SQL解决未能删除约束问题drop constraint
2022/05/30 SQL Server