重做(redo)和撤销(undo)的完整实现
来源:互联网 发布:高速下载软件 编辑:程序博客网 时间:2024/05/22 13:34
undo-redo需要备忘录模式和命令模式做支撑,之前有学习过了command模式和memento模式的一些基本知识。这里要结合两个模式实现一个undo-redo操作的模块,巩固所学的知识。
SessionMementoTaker的实现基于之前实现的一个会话(注册表模式)。根据cookies里面存储的会话ID恢复不同的对象数据,可以达到同一用户多次请求访问同一对象数据的目的。SessionMementoTaker额外提供了三个接口,push_command操作添加命令到历史命令列表。历史命令列表最大长度为5个,超过5个把最开始的命令移除。另外,push_command相当于添加一个新的命令,要把命令指针(persent)移动到最新的位置。舍弃之前的状态。get_undo_command获取最后一次执行的历史命令并更新指针,get_redo_command同理。
历史命令列表: command1---command2---command3---* 星号表示persent,指向最新要执行的命令。
一次undo操作:command1---command2-*--command3--- 回滚之后persent指针往后移动。
一次undo操作:command1--*command2----command3--- 回滚之后persent指针往后移动。
一次redo操作:command1---command2-*--command3--- 重做之后persent指针往前移动。
push_command:command1---command2---command3---command4---* persent更新到最前端
在这里把单个command对象看成是一个原发器(Originator)。根据需要主动创建一个备忘录(memento)保存此刻它的内部状态,并把command对象放入历史命令记录列表。
命令任务在执行开始的时候保存请求命令的参数,在命令执行过程中还可以保存其他必要的参数。由于有些命令不支持撤销操作所以在父类实现里一个空的unexecute;
一个支持undo-redo的复制文件的命令:
重做命令:
The end.
系统框图:
命令分发控制器主要有四个任务:
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);}}通过把构造函数声明为private,controller为一个单例。
对于类似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;}}
SessionMementoTaker的实现基于之前实现的一个会话(注册表模式)。根据cookies里面存储的会话ID恢复不同的对象数据,可以达到同一用户多次请求访问同一对象数据的目的。SessionMementoTaker额外提供了三个接口,push_command操作添加命令到历史命令列表。历史命令列表最大长度为5个,超过5个把最开始的命令移除。另外,push_command相当于添加一个新的命令,要把命令指针(persent)移动到最新的位置。舍弃之前的状态。get_undo_command获取最后一次执行的历史命令并更新指针,get_redo_command同理。
历史命令列表: command1---command2---command3---* 星号表示persent,指向最新要执行的命令。
一次undo操作:command1---command2-*--command3--- 回滚之后persent指针往后移动。
一次undo操作:command1--*command2----command3--- 回滚之后persent指针往后移动。
一次redo操作:command1---command2-*--command3--- 重做之后persent指针往前移动。
push_command:command1---command2---command3---command4---* persent更新到最前端
在这里把单个command对象看成是一个原发器(Originator)。根据需要主动创建一个备忘录(memento)保存此刻它的内部状态,并把command对象放入历史命令记录列表。
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();}}
命令任务在执行开始的时候保存请求命令的参数,在命令执行过程中还可以保存其他必要的参数。由于有些命令不支持撤销操作所以在父类实现里一个空的unexecute;
保存命令状态的对象:
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的复制文件的命令:
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);//...}}命令对象要做的工作比较单一:获取参数(校验参数),保存必要的状态信息,把控制权交给具体的业务逻辑对象。添加执行结果并返回。不同的命令需要不同的请求参数,一些命令根本不需要也不支持撤销操作,所以可以选择性的执行create_memento操作。
最后是要实现的undo-redo,在这里我把undo/redo也看成是一次普通的命令请求,而不需要在控制器做额外的分发处理。
撤销命令:
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;}}
重做命令:
namespace woo\command;require_once('request.php');require_once('command.php');require_once('../base/registry.php');class RedoCommand extends Command {public function do_execute(\woo\controller\Request $request){$command = \woo\base\SessionMementoTaker::get_redo_command();if(isset($command)){$old_req = $command->get_request();$command->do_execute($old_req);$request->set_feedback($old_req->get_feedback());} else{$request->add_feedback('undo command not fount');}return;}}
The end.
0 0
- 重做(redo)和撤销(undo)的完整实现
- 撤销重做(Undo/Redo)
- 撤销和重做(Undo和Redo)(2)
- Command模式实现撤销重做(Undo/Redo)
- Command模式实现撤销重做(Undo/Redo)
- 撤销(undo)和重做(redo)的C++完美实现
- qt的redo和undo undo撤销(后退),redo取消撤销(前进)
- Simple Undo/redo library for C#/.NET(简单的C#.Net撤销、重做库)
- iOS: 为画板App增加 Undo/Redo(撤销/重做)操作
- iOS: 为画板App增加 Undo/Redo(撤销/重做)操作
- vim撤销undo与反撤销redo
- vim撤销undo与反撤销redo
- oracle 的redo和undo
- oracle 的redo和undo
- Oracle的REDO和UNDO
- redo和undo的区别
- Oracle的REDO和UNDO
- redo和undo的区别
- virtual box上网设置
- LeetCode:Linked List Cycle && Linked List Cycle II
- 算法与数据结构第十一次作业 Sins of a Solar Empire P 5
- dijkstra算法JAVA实现
- asp.net登录时生成验证码的方法
- 重做(redo)和撤销(undo)的完整实现
- mysql sqlserver 类似oracle rownum
- [hdoj]2005
- clang
- VS项目属性的一些配置项总结
- ReportStudio进阶教程(三十) - 地图开发(四)调色板
- “M”划分区域(计算几何欧拉公式推导规律)
- IOS设计模式第四篇之装饰设计模式的类别设计模式
- 顿号分割字符串