Java design pattern-Memento)

Source: Internet
Author: User

Java design pattern-Memento)

Also known as snapshot mode or token Mode
A memorandum object is an object used to store snapshots of another object. The purpose of the memorandum mode is to capture the status of the next object and store it externally without damaging the encapsulation, in this way, this object can be restored to the stored state when appropriate in the future. The memorandum mode is often used together with the command mode and iteration submode.
Roles involved in the memorandum mode: Memorandum role, initiator role, and owner role
Memento ):
1. Store the internal status of the initiator object. The memorandum can determine the number of internal states of the initiator object to be stored based on the judgment of the initiator object.
2. A memorandum can protect its content from being read by any object other than the initiator object.
Two equivalent Interfaces
Narrow interface: the narrow interface of the memorandum seen by the owner (Caretaker) object. This narrow interface only allows the owner to pass the memorandum object to other objects.
Wide interface: opposite to the narrow interface seen by the owner object, the initiator can see a wide interface that allows him to read all data, this allows you to recover the internal status of the initiator object based on the data.
Inator ):
1. Create a memorandum object containing the current internal status
2. Use the memorandum object to store its internal status
Caretaker ):
1. Saves the object of the memorandum.
2. Do not check the content of the memorandum object
I. Implementation of the memorandum mode in white box
As shown in, the schematic "white box implementation"

The initiator uses the newly created memo object to store its internal status. The Code is as follows:

Public class Originator {private String state;/*** factory method, return a new memo object * factory method, return a new Memento Object * @ author Fu yuwei * @ time 07:10:13 * @ return */public Memento createMemento () {return new Memento ();} /*** restore the initiator to the State recorded in the memorandum object * retrieve originator to status of storing in Memento * @ author Fu yuwei * @ time 07:11:45 * @ param memento */ public void restoreMemento (Memento memento) {this. state = memento. getState () ;}public String getState () {return state;} public void setState (String state) {this. state = state; System. out. println (current state is: + this. state );}}

The source code of the role is as follows. The object stores the initiator object in the state. The role determines the number of internal states of the initiator based on the judgment class of the initiator object.

public class Memento { private String state; public Memento(String state){  this.state = state; } public String getState() {  return state; } public void setState(String state) {  this.state = state; }}

The following is the source code of the owner role:

Public class Caretaker {private Memento memento; /*** method of memo value * get value of Memento * @ author Fu yuwei * @ time 07:21:34 * @ return */public Memento retrievemento () {return this. memento;}/*** set value of Memento * @ author Fu yuwei * @ time 07:22:41 * @ param memento */public void savemento (Memento memento) {this. memento = memento ;}}

The following is a schematic client code:

public class Client { private static Originator o = new Originator(); private static  Caretaker c = new Caretaker(); public static void main(String[] args) {  // change status of originator  o.setState(On);  // create Memento Object and save status Of originator  c.saveMemento(o.createMemento());  // change status of originator  o.setState(Off);  // retrieve status of originator  o.restoreMemento(c.retrieveMemento()); }}

The sequence diagram of white box implementation:

The sequence of system running is as follows:
1. Set the initiator object status to on
2. Call the createMemento method of the initiator role to create a memorandum object and store the state.
3. Store the memorandum object to the owner object
The sequence diagram of restoring the initiator object to the state recorded by the memorandum object is as follows:

Recover the initiator object to the sequence of running the state system recorded by the memorandum object:
1. Set the initiator object status to off.
2. Retrieve the memorandum object from the owner object
3. Restore the initiator object to the state stored by the memorandum object, that is, on
Advantages and disadvantages of white box implementation:
Advantage: simple
Disadvantage: destroys the encapsulation of the initiator status.
II. Implementation of black box in memorandum Mode
The class diagram that uses internal classes to implement the black box mode is as follows:

An internal Memento class is defined in the Originator class. Since all interfaces of the Memento class are private, only the Memento class and its initiator can call it. The source code of the Caretaker class of the owner role can be seen that the owner role can obtain the memorandum object using MementoIF as the interface. Because this interface is only a identification interface, the owner role cannot change the content of this memorandum object.
The source code is as follows:

Public class Originator {private String state; public Originator () {} public MementoIF createMemento () {return new Memento (this. state);}/*** restore the initiator to the state of the memorandum record * @ author Fu yuwei * @ time 09:59:02 * @ param memento */public void resotreMemento (MementoIF memento) {Memento aMemento = (Memento) memento; this. setState (aMemento. getState ();} public String getState () {return state;} public void setState (String state) {this. state = state ;}
protected class Memento implements MementoIF{        private String savedState;        Memento(String someState){            this.savedState = someState;        }        public String getState() {            return savedState;        }        public void setState(String someState) {            this.savedState = someState;        }    }} 
Public class Caretaker {private MementoIF memento;/*** method of memorandum value * @ author Fu yuwei * @ time 10:04:51 * @ return */public MementoIF retrievemento () {return this. memento;}/*** memorandum Assignment Method * @ author Fu yuwei * @ time 10:05:25 * @ param memento */public void saveMemento (MementoIF memento) {this. memento = memento ;}}
public interface MementoIF {}
Public class Client {/*** @ author Fu yuwei * @ time 10:05:49 * @ param args */public static void main (String [] args) {Originator o = new Originator (); Caretaker c = new Caretaker (); // change the owner state o. setState (on); // create a memorandum object and store the initiator state. savemento (o. createMemento (); // modify the state o of the initiator object. setState (off); // restores the initiator's state o. resotreMemento (c. retrieveMemento (); System. out. println (o. getState ());}}

Application of memorandum Mode
1. JDBC and database
When designing the architecture, you must note that the business logic and storage logic are separated for the following reasons:
A. the two can be separated by different designers to facilitate system maintenance.
B. Changing business logic the storage logic is not necessarily changed
C. The storage logic is changing. The business logic is not necessarily changing.
2. Application of memorandum mode in J2EE framework
Cookie, URL rewriting, HTML hiding table, etc.
3. System Configuration File

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.