Design Mode-memento Mode

Source: Internet
Author: User
Design Mode ---- Memento ( Memorandum ) Mode  Gof :Capture the internal state of an object without damaging the encapsulation, and save the state outside the object. In this way, the object can be restored to the previously saved state. The memento mode uses a memento object to store the internal state of another object (the precondition is that the encapsulation is not damaged). It captures the state of an object and stores it externally, in this way, this object can be restored to the stored state when appropriate in the future. The memo mode is often used together with the command mode and iterator mode. In memento mode, the three roles are critical: memento (Memorandum): The Memorandum stores the internal status of the primary machine. The primary node determines the internal states of the primary Node Based on the need; it prevents access to other objects other than the primary node. Originator: Creates a memorandum to record its internal status at the current time, and uses it to restore its internal status. Caretaker (owner): saves the memorandum. The content of the memorandum cannot be operated or inspected. The following is an example code (from Java and mode): Package memento;

Public class memento
...{
Private string state;
/*** // *** Creates a new instance of memento */
Public memento (string state)
...{
This. State = State;
} // End memento (...)

Public String getstate ()
...{
Return state;
} // End getstate ()

Public void setstate (string state)
...{
This. State = State;
} // End setstate (...)

} // End class memento

Package memento;

Public class Originator
...{
Private string state;
/*** // *** Creates a new instance of originator */
Public originator ()...{
} // End originator ()

Public memento creatememento ()
...{
Return new memento (State );
} // End creatememento ()

Public void restorememento (memento)
...{
State = memento. getstate ();
} // End restorememento (...)

Public String getstate ()
...{
Return state;
} // End getstate ()

Public void setstate (string state)
...{
This. State = State;
System. Out. println ("current state =" + this. State );
} // End setstate (...)

}

Package memento;

Public class caretaker
...{
Private memento;

/*** // *** Creates a new instance of caretaker */
Public caretaker ()...{
} // End caretaker ()

Public memento retrievememento ()
...{
Return memento;
} // End retrievememento ()

Public void savemento (memento)
...{
This. Memento = memento;
} // End savemento (...)

} // End class caretaker

Let's see how to call it:
Package memento;

Public class mementopattern
...{
Private Static originator Ori = new originator ();
Private Static caretaker car = new caretaker ();

/*** // *** Creates a new instance of mementopattern */
Public mementopattern ()...{
} // End mementopattern ()

Public void showmementopattern ()
...{
Ori. setstate ("On ");
Car. savememento (ORI. createmento ());
Ori. setstate ("off ");
Ori. restorememento (car. retrievemento ());
} // End showmementopattern ()

Public static void main (string [] ARGs)
...{
Stringbuilder SSB = new stringbuilder ();
SSB. append ("the memento pattern! ");
System. Out. println (SSB. tostring ());
Mementopattern MP = new mementopattern ();
MP. showmementopattern ();
} // End main (...)

} // End class mementopattern
A uml diagram of the memento mode is provided:

Conclusion: The memento mode is easy to understand and detailed in gof's design patterns and Java and patterns. Especially in Java and patterns, we talked about the memento pattern variants. At present, we only need to learn about concepts and specific applications. In-depth content and other suitable scenarios are to be understood and used.

Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 1007600

 

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.