Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Design Pattern & UML  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 [原创] 如何使用Memento模式重构现有的Undo模块
tier3





发贴: 228
积分: 120
于 2004-06-08 11:21 user profilesend a private message to usersend email to tier3search all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1.  Intent
Memento模式主要用于GUI应用程序中UNDO的操作,还可以用于保存Transction方面保存旧的数据库操作,本文主要讨论如何把一个现有的GUI应用程序中的UNDO模块重构为使用Memento模式,增加可复用性和可扩展性。

2.  Memento introduction
Suppose you would like to save the internal state of an object so you can restore it later. Ideally, it should be possible to save and restore this state without making the object itself take care of this task, and without violating encapsulation. This is the purpose of the Memento pattern.
Objects frequently expose only some of their internal state using public methods, but you would still like to be able to save the entire state of an object because you might need to restore it later. In some cases, you could obtain enough information from the public interfaces (such as the drawing position of graphical objects) to save and restore that data. In other cases, the color, shading, angle and connection relationship to other graphical objects need to be saved and this information is not readily available. This sort of information saving and restoration is common in systems that need to support Undo commands.
If all of the information describing an object is available in public variables, it is not that difficult to save them in some external store. However, making these data public makes the entire system vulnerable to change by external program code, when we usually expect data inside an object to be private and encapsulated from the outside world.
The Memento pattern attempts to solve this problem by having privileged access to the state of the object you want to save. Other objects have only a more restricted access to the object, thus preserving their encapsulation. This pattern defines three roles for objects:
1.  The Originator is the object whose state we want to save.
2.  The Memento is another object that saves the state of the Originator.
3.  The Caretaker manages the timing of the saving of the state, saves Memento and, if needed, uses the Memento to restore the state of the Originator.

3. Memento模式实现方法:
3.1.  Implement it along with command pattern;
  We define a command interface class, and there are two methods in it: execute(), restore(), execute() method is for the command pattern, and restore() method is for the memento pattern, it mainly used for the menu button groups or tool bar button groups.
3.2.  Implement it as an inner memento class for the originator;
  We define an inner memento class for the originator class, and there are setter and getter method in the originator class for the memento, but it only support one level undo operation.
3.3.  Implement is as an independent class to support multiple level mementos;
  We define an independent class as the memento class, and a memento container class to contains all previous mementos. It is the way we currently used in.

4. Memento模式实现流程:
1.  Define which operations in the GUI application will be kept, not all the operations should be kept, we should     define the opertaion scope we cared;
2.  Adopt a best way from the above three ways for your real application.
3.  Design and implement the undo()/ restore() method for the memento class.

附件是我们实现的一个类图


tier3 edited on 2004-06-08 11:32


flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923