PHP物件導向之命令模式

來源:互聯網
上載者:User

標籤:檔案的   directory   檔案   cti   request   span   character   topic   視圖   

這個模式主要由 命令類、使用者請求資料類、商務邏輯類、命令類工廠類及調用類構成,各個類的作用概括如下:

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物件導向之命令模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.