Design Mode-memento)

Source: Internet
Author: User
Design Mode (21): memento)
Memento)
Definition
Capture the internal state of an object without compromising encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state.
Memento Structure Diagram
Inator class
Class Originator
{
Private   String State;
Public   String State
{
Get { Return State ;}
Set {State = Value ;}
}
Public Memento creatememento ()
{
Return ( New Memento (State ));
}
Public   Void Setmemento (memento)
{
State
= Memento. State;
}
Public   Void Show ()
{
Console. writeline (
" State = " + State );
}
}
Memento class
Class Memento
{
Private   String State;
Public Memento ( String State)
{
This . State = State;
}
Public   String State
{
Get { Return State ;}
}
}
Caretaker class
Class Caretaker
{
Private Memento memento
{
Get { Return Memento ;}
Set {Memento = Value ;}
}
}
Client Program
Static   Void Main ( String [] ARGs)
{
Originator o
=   New Originator ();
O. State
=   " On " ;
O. Show ();

Caretaker C =   New Caretaker ();
C. Memento
= O. creatememento ();
O. State
=   " Off " ;
O. Show ();
O. setmemento (C. Memento );
O. Show ();
}

Benefits of the Memorandum:
The details to be saved are encapsulated in memento. The client does not need to be affected when you change the details to be saved.
The mememto mode is suitable for classes with complex functions, but which need to maintain or record the property history. or, if the attributes to be saved are only a small part of many attributes, originator can be restored to the previous state based on the saved memento information.
If you need to revoke a command when using command mode in a system, you can use the memorandum mode to store the status of the Undo operation.
When the role's status changes, it is possible that this status is invalid. At this time, you can use a memorandum of temporary storage to restore the status.

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.