老生常談PHP面向?qū)ο笾钅J?必看篇)
這個模式主要由 命令類、用戶請求數(shù)據(jù)類、業(yè)務(wù)邏輯類、命令類工廠類及調(diào)用類構(gòu)成,各個類的作用概括如下:
1、命令類:調(diào)用用戶請求數(shù)據(jù)類和業(yè)務(wù)邏輯類;
2、用戶請求數(shù)據(jù)類:獲取用戶請求數(shù)據(jù)及保存后臺處理后返回的結(jié)果;
3、業(yè)務(wù)邏輯類:如以下的示例中驗證用戶登陸信息是否正確的功能等;
4、命令工廠類(我自己取的名字,哈哈):生成命令類的實例;
5、調(diào)用類:調(diào)用命令類,生成視圖;
直接看代碼:
//命令類
abstract class Command {
abstract function execute(CommandContext $context);
}
class LoginCommand extends Command{ //處理用戶登陸信息的命令類
function execute (CommandCotext $context){ //CommandCotext 是一個處理用戶請求數(shù)據(jù)和后臺回饋數(shù)據(jù)的類
$manager = Registry::getAccessManager(); //原文代碼中并沒有具體的實現(xiàn),但說明了這是一個處理用戶登陸信息的業(yè)務(wù)邏輯類
$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{ //發(fā)送郵件的命令類
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;
}
}
//用戶請求數(shù)據(jù)類
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;
}
}
//命令類工廠,這個類根據(jù)用戶請求數(shù)據(jù)中的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;
}
}
//調(diào)用者類,相當(dāng)于一個司令部它統(tǒng)籌所有的資源
class Controller{
private $context;
function __construct(){
$this->context = new CommandContext(); //用戶請求數(shù)據(jù)
}
function getContext(){
return $this->context;
}
function process(){
$cmd = CommandFactory::getCommand($this->context->get('action')); //通過命令工廠類來獲取命令類
if(!$comd->execute($this->context)){
//處理失敗
} else {
//成功
// 分發(fā)視圖
}
}
}
// 客戶端
$controller = new Controller();
//偽造用戶請求,真實的場景中這些參數(shù)應(yīng)該是通過post或get的方式獲取的,貌似又廢話了:)
$context = $controller->getContext();
$context->addParam('action','login');
$context->addParam('username','bob');
$context->addParam('pass','tiddles');
$controller->process();以上這篇老生常談PHP面向?qū)ο笾钅J?必看篇)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
PHP的new static和new self的區(qū)別與使用
這篇文章主要介紹了PHP的new static和new self的區(qū)別與使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
thinkPHP2.1自定義標(biāo)簽庫的導(dǎo)入方法詳解
這篇文章主要介紹了thinkPHP2.1自定義標(biāo)簽庫的導(dǎo)入方法,詳細(xì)分析了thinkPHP標(biāo)簽庫的定義、使用及自動導(dǎo)入相關(guān)技巧,需要的朋友可以參考下2016-07-07
destoon文章模塊調(diào)用企業(yè)會員資料的方法
這篇文章主要介紹了destoon文章模塊調(diào)用企業(yè)會員資料的方法,非常實用的一個技巧,需要的朋友可以參考下2014-08-08
在PHP中運(yùn)行Linux命令并啟動SSH服務(wù)的例子
這篇文章主要介紹了在PHP中運(yùn)行Linux命令并啟動SSH服務(wù)的例子,因為VPS的SSH服務(wù)掛了,導(dǎo)致無法進(jìn)入服務(wù)器,所以想了這么一個辦法,需要的朋友可以參考下2014-06-06
laravel 解決ajax異步提交數(shù)據(jù),并還回填充表格的問題
今天小編就為大家分享一篇laravel 解決ajax異步提交數(shù)據(jù),并還回填充表格的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10

