《PHP編程:老生常談PHP面向對象之命令模式(必看篇)》要點:
本文介紹了PHP編程:老生常談PHP面向對象之命令模式(必看篇),希望對您有用。如果有疑問,可以聯系我們。
PHP編程這個模式主要由 命令類、用戶請求數據類、業務邏輯類、命令類工廠類及調用類構成,各個類的作用概括如下:
PHP編程1、命令類:調用用戶請求數據類和業務邏輯類;
PHP編程2、用戶請求數據類:獲取用戶請求數據及保存后臺處理后返回的結果;
PHP編程3、業務邏輯類:如以下的示例中驗證用戶登陸信息是否正確的功能等;
PHP編程4、命令工廠類(我自己取的名字,哈哈):生成命令類的實例,這個類第一次看的時候我覺得有點牛比豢戳思副榱嘶故薔醯煤 :);
PHP編程5、調用類:調用命令類,生成視圖;
PHP編程直接看代碼:
PHP編程
//命令類
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.
轉載請注明本頁網址:
http://www.snjht.com/jiaocheng/742.html