Design Pattern: memento Mode

Source: Internet
Author: User

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:

    • Originator. Java

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 ");
}
}

    • Memento. Java

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;
}
}

    • Caretaker. Java

Public class caretaker {
Private memento;

Public void setmemento (memento ){
This. Memento = memento;
}

Public memento getmemento (){
Return memento;
}
}
\

    • Main. Java

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.