系統框圖:
命令分發控制器主要有四個任務:快取
1 命令分發控制器主要有四個任務:快取
可以使用序列化機制,把資料快取而不用每次去讀取文件,加快存取效率。
2.根據前端請求,收集參數產生一個請求(Request)。
3.把請求對應到特定業務邏輯的命令模組(Command)。 4.執行操作並將結果回傳給前端視圖。
業務邏輯層根據傳入的context物件可以取得執行參數,執行完畢後還可以將執行結果透過context物件回傳給上一層。
指令分發控制器的實作:
class Controller{ private function __construct() {} static function run(){ $instance = new Controller(); $instance->init(); $instance->handleRequest(); } function init(){ $application = \base\ApplicationHelper::instance(); $application->system_init(); } function handleRequest(){ $request = new \controller\Request(); $cmd_r = new \command\CommandResolver(); $cmd = $cmd_r->get_command($request); $cmd->execute($request); } }
對於類似PHP這樣的解釋型的語言,要實現undeo/redo機制,必須用到一些緩存機制(session)來保存命令執行的歷史記錄。這裡的session模組主要負責維護一個命令歷史記錄,其實現如下:
namespace base; require_once('session_registry.php'); class SessionMementoTaker extends SessionRegistry{ const COMMAND_COUNT = 5; private $persent = 0; private $cmd_stack = array(); static public function instance(){ return parent::instance(); } public function push_command(Command $cmd){ $this->cmd_stack = self::instance()->get('cmd_stack'); if(!empty($this->cmd_stack)){ if(count($this->cmd_stack) >self::COMMAND_COUNT){ array_shift($this->cmd_stack); reset($this->cmd_stack); } } array_push($this->cmd_stack, $cmd); $this->persent = count($this->cmd_stack) + 1; self::instance()->set('cmd_stack', $this->cmd_stack); self::instance()->set('cmd_persent', $this->persent); } public function get_undo_command(){ $this->persent = self::instance()->get('cmd_persent'); $this->cmd_stack = self::instance()->get('cmd_stack'); if(!empty($this->cmd_stack) && $this->persent > 0){ $command = $this->cmd_stack[--$this->persent]; self::instance()->set('cmd_persent', $this->persent); return $command; } return null; } public function get_redo_command(){ $this->persent = self::instance()->get('cmd_persent'); $this->cmd_stack = self::instance()->get('cmd_stack'); if(!empty($this->cmd_stack) && $this->persent < count($this->cmd_stack)){ $command = $this->cmd_stack[$this->persent++]; self::instance()->set('cmd_persent', $this->persent); return $command; } return null; } }
command基類實現:
namespace woo\command; require_once('../memento/state.php'); require_once('../memento/memento.php'); abstract class Command { protected $state; final function __construct(){ $this->state = new \woo\memento\State(); } function execute(\woo\controller\Request $request) { $this->state->set('request', $request); $this->do_execute($request); } abstract function do_execute(\woo\controller\Request $request); function do_unexecute(\woo\controller\Request $request) {} public function get_state(){ return $this->state; } public function set_state(State $state){ $this->state = $state; } public function get_request(){ if(isset($this->state)){ return $this->state->get('request'); } return null; } public function set_request(\woo\controller\Request $request){ if(isset($this->state)){ return $this->state->set('request', $request); } } public function create_memento(){ \woo\base\SessionMementoTaker::push_command($this); $mem = new \woo\memento\Memento(); $mem->set_state($this->state); return $mem; } public function set_memento(Memento $mem){ $this->state = $mem->get_state(); } }
保存指令狀態的物件:
class State{ private $values = array(); function __construct(){ } public function set($key, $value){ $this->values[$key] = $value; } public function get($key){ if(isset($this->values[$key])) { return $this->values[$key]; } return null; } }
最後是要實現的undo-redo,在這裡我把undo/redo也看成是一次普通的命令請求,而不需要在控制器做額外的分發處理。
撤銷指令:
namespace woo\command; require_once('request.php'); require_once('command.php'); require_once('../base/registry.php'); require_once('../file_manager.php'); require_once('../base/session_memento.php'); class CopyCommand extends Command { function do_execute(\controller\Request $request) { $src_path = $request->get_property('src'); $dst_path = $request->get_property('dst'); $this->state->set('src_path', $src_path); $this->state->set('dst_path', $dst_path); $this->create_memento(); $file_manager = \base\Registry::file_manager(); $ret = $file_manager->copy($src_path, $dst_path); $request->add_feedback($ret); //... } }
重做指令:
namespace woo\command; require_once('request.php'); require_once('command.php'); require_once('../base/registry.php'); require_once('../base/session_memento.php'); class UndoCommand extends Command{ public function do_execute(\controller\Request $request){ $command = \base\SessionMementoTaker::get_undo_command(); if(isset($command)){ $old_req = $command->get_request(); $command->do_unexecute($old_req); $request->set_feedback($old_req->get_feedback()); } else{ $request->add_feedback('undo command not fount'); } return; } }
以上就介紹了重做(redo)和撤銷(undo)的完整實現,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。