Java Design mode-Memo mode (Memento)

Source: Internet
Author: User

aka Snapshot mode or token mode
A Memo object is an object that is used to store a snapshot (snapshot) of the internal state of another object. The memo mode is intended to capture (capture) the state of the next object without destroying the package, and to externalize and store it so that it can be restored to a stored state at the appropriate time in the future. Memo patterns are often used with command patterns and iterative sub-patterns.
Roles involved in memo mode: Memo role, initiator role, owner role
Memo Role (Memento):
1. Store the internal state of the initiator (originator) object. Memos can determine the internal state of how many initiator objects are stored based on the decision of the initiator object
2. Memos can protect their content from being read by any object other than the initiator object.
Two equivalent interfaces
Narrow interface: The owner (caretaker) object sees the narrow interface of the memo, this narrow interface only allows him to pass the memo object to other objects
Wide interface: In contrast to the narrow interface seen by the owner object, the initiator can see a wide interface, which allows him to read all the data in order to recover the internal state of the initiator object based on the data.
Initiator Role (originator):
1. Create a Memo object containing the current internal state
2. Use Memo object to store its internal state
responsible Role (caretaker):
1, responsible for saving the memorandum object
2, do not check the contents of the Memo object
A white box implementation of Memo mode
As shown, the schematic "white box implementation"

The initiator uses the newly created Memo object to store its own internal state, with the following code:

 Public  class originator { PrivateString State;/** * Factory method, return a new Memo object * Factory Method,return a new Memento object * @author Shu Yuwei * @time 2015-5 -9 pm 07:10:13 * @return * *  PublicMementoCreatememento(){return NewMemento (); }/** * Restores the initiator to the status recorded in the Memo object * Retrieve originator to status of storing in Memento * @author Shu Yuwei * @time /c2> 2015-5-9 pm 07:11:45 * @param Memento * *  Public void Restorememento(Memento Memento) { This. state = Memento.getstate (); } PublicStringgetState() {returnState } Public void setState(String State) { This. state = State; System.out.println ("Current state is :"+ This. State); }}

The following is the source code for the memo role, which stores the initiator object's incoming state. The memo role determines how much of the initiator's internal state is stored, based on the judging class of the initiator object.

publicclassprivatepublicMemento(String state){  thispublicgetState() {  returnpublicvoidsetState(String state) {  this.state = state; }}

Below is the source of the responsible role

 Public  class caretaker { PrivateMemento Memento;/** * Memo Value Method * Get value of Memento * @author Shu Yuwei * @time 2015-5-9 pm 07:21:34 * @return 
       * *  PublicMementoRetrievememento(){return  This. Memento; }/** * Memo Assignment Method * Set Value of Memento * @author Shu Yuwei * @time 2015-5-9 pm 07:22:41 * @param Memento * *  Public void Savememento(Memento Memento) { This. Memento = Memento; }}

A schematic client code is given below:

 Public classClient {Private StaticOriginator o =NewOriginator ();Private StaticCaretaker C =NewCaretaker (); Public Static void Main(string[] args) {//Change status of originatorO.setstate ("on");//Create Memento Object and save status of originatorC.savememento (O.creatememento ());//Change status of originatorO.setstate ("Off");//Retrieve Status of originatorO.restorememento (C.retrievememento ()); }}

Time series diagram implemented in white box:

The timing of the system operation is this:
1. Set the initiator object state to On
2. Call the Creatememento method of the initiator role to create a memo object that stores the state.
3. Store the memo object in the owner object
The sequence diagram that restores the initiator object to the state recorded by the memo object is as follows:

To restore the initiator object to the state system that is logged by the memo object:
1. Set the initiator object state to Off
2. Remove the memo object from the owner object
3. Restores the initiator object to the state stored by the memo object, that is, on
advantages and disadvantages of white box implementation:
Advantages: Simple
Cons: Destroying the encapsulation of the initiator state
Second, the Black-box realization of Memo Mode
The class diagram for implementing a black-box pattern using an inner class is as follows:

An internal memento class is defined in the originator class, and since all interfaces of the Memento class are private, only himself and the initiator can invoke it. Owner role The source code of the caretaker class can be seen, the owner role can get the memo object is Mementoif interface, because this interface is only an identity interface, so the owner role can not change the content of this memo object.
The source code is as follows:

 Public  class originator {    PrivateString State; Public Originator(){} PublicMementoifCreatememento(){return NewMemento ( This. State); }/** * Restore the initiator to the status of the memo record * @author Shu Yuwei * @time 2015-5-12 pm 09:59:02 * @param m Emento * *     Public void Resotrememento(Mementoif Memento) {Memento Amemento = (Memento) Memento; This. SetState (Amemento.getstate ()); } PublicStringgetState() {returnState } Public void setState(String State) { This. state = State; }
protectedclass Memento implements MementoIF{        private String savedState;        Memento(String someState){            this.savedState = someState;        }        publicgetState() {            return savedState;        }        publicvoidsetState(String someState) {            this.savedState = someState;        }    
 Public  class caretaker {    PrivateMementoif Memento;/** * Memo Value method * @author Shu Yuwei * @time 2015-5-12 pm 10:04:51 * @return */< /c4>     PublicMementoifRetrievememento(){return  This. Memento; }/** * Memo Assignment method * @author Shu Yuwei * @time 2015-5-12 pm 10:05:25 * @param Memento */     Public void Savememento(Mementoif Memento) { This. Memento = Memento; }}
publicinterface MementoIF {}
 Public  class Client {    /** * @author Shu Yuwei * @time 2015-5-12 pm 10:05:49 * @param args */     Public Static void Main(string[] args) {Originator o =NewOriginator (); Caretaker C =NewCaretaker ();//Change owner statusO.setstate ("on");//Create a Memo object and store the initiator stateC.savememento (O.creatememento ());//Modify the status of the initiator objectO.setstate ("Off");//Restore the status of the initiatorO.resotrememento (C.retrievememento ());    System.out.println (O.getstate ()); }}

Application of Memo mode
1. JDBC and database
In architecture design, it is important to be aware of the reasons for separating business logic from storage logic:
A. Separation of the two can be given to different designers for easy system maintenance
B. Changes in business logic storage logic does not necessarily change
C. Storage logic changes business logic does not necessarily change
2. Application of Memo mode in the framework of Java EE
Cookies, url rewriting, HTML hidden tables, etc.
3. System Configuration File

Java Design mode-Memo mode (Memento)

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.