The memorandum mode of Java and pattern

Source: Internet
Author: User

The memo mode is also called snapshot mode (Snapshot pattern) or token mode, which is the behavior pattern of the object.

A Memo object is an object that is used to store a snapshot of the internal state of another object. The purpose of the memo mode is to capture (capture) the state of an object without destroying the package, and to externally 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 in conjunction with Command mode and iterative sub-patterns.

Structure of the memo pattern

The structure diagram of the memo pattern is shown below

  

The memo pattern involves three roles: the Memo (Memento) role, the initiator (originator) role, the owner (caretaker) role .

Memo (Memento) role

The memorandum role is also responsible for the following:

(1) Store the Civil War state of the initiator (originator) object. A memo can determine the internal state of how many initiator (originator) objects are stored based on the decision of the initiator object.

(2) A memo can protect its content from being read by any object other than the initiator (originator) object.

The memo has two equivalent interfaces:

Narrow interface: the owner (caretaker) object (and any other object other than the initiator object) sees the narrow interface of the memo (narrow interface), which only allows it to pass the memo object to other objects.

Wide Interface: Contrary to the narrow interface seen by the owner object, the initiator object can see a wide interface (wide interface), which allows it to read all the data in order to recover the internal state of the initiator object based on that data.

Sponsor Originator) role

The initiator role has the following responsibilities:

(1) Create a Memo object that contains the current internal state.

(2) Use the memo object to store its internal state.

Head Caretaker) role

The responsible role has the following responsibilities:

(1) Responsible for saving the memorandum object.

(2) Do not check the contents of the memo object.

Implementation of the "white box" Memo mode

The memo role provides an interface to any object, that is, a wide interface, and the state stored inside the memo role is exposed to all objects. So this implementation is also called "white Box implementation".

The "white box" implementation stores the status of the initiator role in a place where everyone can see it, thus destroying encapsulation. But through the programmer self-discipline, the same can be achieved to a certain extent the mode of the intention. So the white box implementation still makes sense.

A schematic "white box implementation" is given below.

  

Source

The initiator role class, which uses a newly created Memo object to store its own internal state.

public class Originator {    private String state;    /**     * Factory method, returns a new Memo object *    /public Memento Creatememento () {        return new Memento (state);    /**     * Restores the initiator to the state recorded in the Memo object *    /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 status:" + This.state);    }    }

Memo role class, the memo object stores the state in which the initiator object is passed in.

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 owner role class, the owner role is responsible for saving the memo object, but never modifying (or even viewing) the contents of the memo object.

public class Caretaker {    private Memento Memento;    /**     * Memo Value Method *     /Public    Memento Retrievememento () {        return this.memento;    }    /**     * Memo Assignment method *     /public    void Savememento (Memento Memento) {        this.memento = Memento;    }}

Client Role Classes

public class Client {public    static void Main (string[] args) {                originator o = new originator ();        Caretaker C = new caretaker ();        Change the state of the owner object        o.setstate ("on");        Creates a memo object and stores the state of the initiator object        C.savememento (O.creatememento ());        Modify the initiator's status        o.setstate ("Off");        Restores the status of the initiator object        O.restorememento (C.retrievememento ());                System.out.println (O.getstate ());}    }

In this schematic client role above, first set the initiator object's state to "on" and create a Memo object to store it, then change the initiator object's state to "OFF", and finally restore the initiator object to the state stored by the memo object, that is, "on" state.

The timing diagram of the system is more reflective of the time sequence in which the various roles of the system are called. A sequence diagram that stores the state of the initiator object in a white-box memo object.

  

You can see that the timing of the system operation is this:

(1) Set the status of the initiator object to "on".

(2) Call the Creatememento () method of the initiator role to create a memo object to store the state.

(3) Store the memo object in the owner object.

  The timing diagram that restores the initiator object to the state recorded by the memo object is as follows:

  

  As you can see, when the initiator object is restored to the state recorded by the memo object, the system runs the timing:

(1) Set the initiator status to "Off".

(2) Remove the memo object from the owner object.

(3) Restores the initiator object to the state that the memo object is stored in, which is the "on" state.

Implementation of "black box" memo mode

The memo role provides a wide interface to the initiator (originator) Role object, while providing a narrow interface for other objects. Such implementations are called "Black-box implementations".

In the Java language, the way to implement a dual interface is to design the memo role Class as the internal member class of the initiator role class .

The memento is set to the inner class of the originator class, which encapsulates the memento object in originator, and externally provides an identity interface mementoif to caretaker and other objects. In this way, the originator class sees all the interfaces of the Menmento, and caretaker and other objects see only the interfaces exposed by the identity interface Mementoif.

The class diagram that uses the inner class to implement the memo pattern is shown below.

Source

An internal memento class is defined in the initiator role class originator. Since all interfaces of this memento class are private, only it itself and the initiating human can invoke.

Package memento.sample2;/** * @author Chen_dz * @date: 2012-6-2 a.m. 10:11:08 */public class Originator {    private String State;        Public String getState () {        return state;    }    public void SetState (String state) {        this.state = state;        System.out.println ("Assignment Status:" + state);    }    /**     * Factory method, return a new Memo object *    /public mementoif Creatememento () {        return to new Memento (state)    ;    /**     * Initiator reverts to Memo object record status */public    void Restorememento (Mementoif memento) {        this.setstate (( Memento) Memento). GetState ());    }        Private class Memento implements mementoif{                private String state;        /**         * Construction Method         *        /private Memento (String state) {            this.state = state;        }                Private String getState () {            return state;        }        private void SetState (String state) {            this.state = state;        }}    }

The narrow interface mementoif, which is an identity interface, so it does not define any method.

Public interface Mementoif {}

The owner role class caretaker can get the memo object is Mementoif interface, because this interface is only an identity interface, so the owner role cannot change the content of this memo object.

public class Caretaker {    private mementoif memento;    /**     * Memo Value Method *    /public mementoif Retrievememento () {        return memento;    }    /**     * Memo Assignment method *    /public void Savememento (Mementoif memento) {        this.memento = memento;    }}

Client Role Classes

public class Client {public    static void Main (string[] args) {        originator o = new originator ();        Caretaker C = new caretaker ();        Change the state of the owner object        o.setstate ("on");        Creates a memo object and stores the state of the initiator object        C.savememento (O.creatememento ());        Modifies the initiator object's state        o.setstate ("Off");        Restores the status of the initiator object        O.restorememento (C.retrievememento ());}    }

Client First

(1) Set the initiator object's state to "on".

(2) Call the Creatememento () method to create a memo object to store this state (at this point the Creatememento () method returns the apparent type of the Mementoif interface, the real type is the memento object inside the originator).

(3) Store the memo object in the owner object. Because the owner object gets only the Mementoif interface, it cannot read the state inside the memo object.

(4) Set the initiator object's state to "OFF".

(5) Call the Retrievememento () method of the owner object to remove the memo object. Note that only the Mementoif interface can be obtained at this time, so the internal state of this object cannot be read.

(6) invoking the Restorememento () method of the initiator object restores the state of the initiator object to the state stored by the memo object, which is the "on" state. Because the initiator object's inner class memento implements the Mementoif interface, this inner class is the true type of the incoming memo object, so the initiator object can read out the internal state of the object using the private interface of the inner class memento.

Multiple checkpoints

The schematic implementations of the white box and the black box given above are simple implementations that store only a single state, or they can be called only one checkpoint. Common systems often need to store more than one state, but need to store multiple states, or have multiple checkpoints.

Memo mode stores the state of the initiator object inside the memo object, which restores the initiator object to one of the checkpoints stored by the memo object. A schematic implementation of the memo pattern with multiple checkpoints is given below.

Source

Initiator Role Source Code

public class Originator {    private list<string> states;    Check Point index    private int index;    /**     * constructor */public    originator () {        states = new arraylist<string> ();        index = 0;    }    /**     * Factory method, return a new Memo object *    /public Memento Creatememento () {        return new Memento (states, index);    }    /**     * Restores the initiator to the state of the memo object record *    /public void Restorememento (Memento Memento) {        states = Memento.getstates ();        index = Memento.getindex ();    }    /**     * Status Assignment method */     public    void SetState (String state) {        states.add (state);        index++;    }    /**     * Auxiliary method, print all states */public    void Printstates () {for                (String state:states) {            System.out.println (state);}}}    

Memo role class, this implementation can store any number of states, the outside world can use the checkpoint index to remove the checkpoint state.

public class Memento {    private list<string> states;    private int index;    /**     * constructor *    /Public Memento (list<string> states, int index) {        this.states = new arraylist< String> (states);        This.index = index;    }    Public list<string> getstates () {        return states;    }    public int GetIndex () {        return index;    }    }

Owner Role Class

public class Caretaker {    private originator o;    Private list<memento> mementos = new arraylist<memento> ();    private int current;    /**     * constructor *    /public caretaker (originator O) {        this.o = O;        current = 0;    }    /**     * Create a new checkpoint */     public    int Creatememento () {        Memento Memento = O.creatememento ();        Mementos.add (memento);        return current++;    }    /**     * Restores the initiator to a checkpoint */public    void restorememento (int index) {        Memento Memento = mementos.get (index );        O.restorememento (memento);    }    /**     * Delete a checkpoint */     public    void Removememento (int index) {        mementos.remove (index);}    }

Client role Source code

public class Client {public    static void Main (string[] args) {                originator o = new originator ();        Caretaker C = new caretaker (o);        Change Status        o.setstate ("State 0");        Establish a checkpoint        C.creatememento ();        Change Status        o.setstate ("State 1");        Establish a checkpoint        C.creatememento ();        Change Status        o.setstate ("State 2");        Establish a checkpoint        C.creatememento ();        Change Status        o.setstate ("State 3");        Establish a checkpoint        C.creatememento ();        Print out all Checkpoint        o.printstates ();        System.out.println ("-----------------recovery checkpoint-----------------");        Revert to a second checkpoint        C.restorememento (2);        Print out all Checkpoint        o.printstates ();    }}

The results of the operation are as follows:

As you can see, the client role is constantly changing the status of the initiator's role and storing it in a memo. By indicating the checkpoint index, the initiator role can be restored to the corresponding state of the appropriate checkpoint.

The activity sequence diagram that stores the initiator's state to the memo object is as follows:

  

The timing of the system operation is this:

(1) Set the status of the initiator object to a valid state;

(2) The Creatememento () method that invokes the owner role, which is responsible for invoking the initiator role and the memo role, stores the state of the initiator object.

  The activity sequence diagram that restores the initiator object to a checkpoint of a memo object is as follows:

  Because the function of the responsible role is enhanced, the timing of the system operation is simplified when the initiator object is restored to the state recorded by the memo object:

(1) Call the Restorememento () method of the owner role to restore the initiator to a checkpoint.

"Readme History" mode

The so-called "Readme History" pattern (history-on-self pattern) is actually a variant of the memo pattern. In memo mode, the initiator (originator) role, the owner (caretaker) role, and the memo (Memento) role are separate roles. While the implementation of the memo class can be an internal member class that initiates human beings, the memo class still retains its independent meaning as a role. In the "Readme History" mode, the initiator role is the role of the responsible person.

The class diagram for the "Readme history" pattern is as follows:

  

  The memorandum role has the following responsibilities:

(1) Store the internal state of the initiator (originator) object.

(2) A memo can protect its content from being read by any object other than the initiator (originator) object.

 The initiator role has the following responsibilities:

(1) Create a Memo object that contains its current internal state.

(2) Use the memo object to store its internal state.

The client role has responsibility for saving the memo object.

Source

The narrow interface mementoif, which is an identity interface, so it does not define any method.

Public interface Mementoif {}

The initiator role also acts as the responsible person, which means that it is responsible for keeping its own memo object.

  

public class Originator {public    String state;    /**     * Change Status     *    /public void Changestate (String state) {        this.state = state;        System.out.println ("Status changed to:" + state);    }    /**     * Factory method, return a new Memo object */public    Memento Creatememento () {        return to new Memento (this);    }    /**     * Restores the initiator to the state recorded by the memo object     *    /public void Restorememento (Mementoif memento) {        memento m = (Memento) Memento;        Changestate (m.state);    }        Private class Memento implements mementoif{                private String state;        /**         * Construction Method         *        /private Memento (originator o) {            this.state = o.state;        }        Private String getState () {            return state;}}            }

Client Role Classes

public class Client {public    static void Main (string[] args) {        originator o = new originator ();        Modify        the status o.changestate ("state 0");        Create Memo        Mementoif memento = O.creatememento ();        Modify        the status O.changestate ("State 1");        Restore the state of the object according to the memo        O.restorememento (Memento);}    }

Since the "Readme history" as a special form of implementation of a memo pattern is very simple and understandable, it is probably the most popular form of implementation of the memo pattern.

The memorandum mode of Java and pattern

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.