Http://www.35java.com/zhibo/forum.php? MoD = viewthread & tid = 284 & extra = Page % 3d1
You want a restoration mechanism in your program. If you directly create a restoration mechanism in the program, this will increase the responsibilities of the object and reduce the reusability of the object.
Instead of building a recovery mechanism in an object, you can remove it from the object. In this case, you can use the memento mode to achieve this function.
Memento mode retains a memento member in originator. This MEMENTO can include information about the members of originator. If it is external, memento can be maintained by a caretaker. Each action is performed on originator, caretaker retains the member status before the originator action. If you want to restore the object status in the future, you only need to retrieve the memento object from the caretaker and restore the originator state.
The UML class structure of the memento mode is as follows:
The caretaker in the figure is used to retain the memorandum object created by the original object for future recovery. The State indicates an internal state. When there are many internal states, it can also be organized into a category, the memento maintained by caretaker can be multiple to implement redo and undo functions multiple times.
The following provides a simple implementation to see how to implement the memento mode:
Public class originator {
Private string name;
Private string phone;
Public originator (string name, string phone ){
This. Name = Name;
This. Phone = phone;
}
// Some operations make state changed
Public void someoperation (){
Name = "noboby ";
Phone = "911-911 ";
}
// Recover object's state
Public void setmemento (memento m ){
This. Name = M. getname ();
This. Phone = M. getphone ();
}
Public memento creatememento (){
Return new memento (name, phone );
}
Public void showinfo (){
System. Out. println ("name:" + name +
"\ Nphone:" + phone + "\ n ");
}
}
Public class memento {
Private string name;
Private string phone;
Public memento (string name, string phone ){
This. Name = Name;
This. Phone = phone;
}
Public String getname (){
Return name;
}
Public String getphone (){
Return phone;
}
Public void setname (string name ){
This. Name = Name;
}
Public void setphone (string phone ){
This. Phone = phone;
}
}
Public class caretaker {
Private memento;
Public void setmemento (memento ){
This. Memento = memento;
}
Public memento getmemento (){
Return memento;
}
}
\
Public class main {
Public static void main (string [] ARGs ){
Originator originator =
New originator ("Justin", "888-8888 ");
Caretaker caretaker = new caretaker ();
// Save object's memento
Caretaker. setmemento (originator. createmento ());
Originator. showinfo ();
// Some operations make the object's state changed
Originator. someoperation ();
Originator. showinfo ();
// Use memento to recover object's state
Originator. setmemento (caretaker. getmemento ());
Originator. showinfo ();
}
}
Can be combinedCommand modeTo implement the redo/Undo function, record the object status before and after the operation, and record the commands used. When Undo/Redo is to be implemented, you only need to retrieve the memento object to restore the object state.