Java and model 26-22nd-Memorandum Mode

Source: Internet
Author: User
Document directory
  • Memento role
  • Source code
  • Source code
  • Source code
  • Source code

The memorandum mode is also called the Snapshot Pattern or token mode, and is the behavior mode of the object.

A memorandum object is an object used to store snapshots of another object's internal state. The purpose of the memorandum mode is to capture, externalize, and store the status of an object 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.

Structure of memorandum Mode

The structure of the memorandum mode is as follows:

The memorandum model involves three roles:Memento, originator, and caretaker roles.

Memento role

The role of the memorandum has the following responsibilities:

(1) stores the Civil War state of the initiator object. The memorandum determines 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.

The memorandum has two equivalent interfaces:

Narrow interface:The owner (caretaker) object (and any other object except the initiator object) sees the narrow interface of the memorandum (narrow interface ), this narrow interface 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 that allows it to read all data, to recover the internal status of the initiator object based on the data.

Initiator ( Originator) Role

The initiator role has the following responsibilities:

(1) create a memorandum object containing the current internal status.

(2) Use the memorandum object to store its internal status.

Owner ( Caretaker) Role

The owner role has the following responsibilities:

(1) saves the object of the memorandum.

(2) do not check the content of the memorandum object.

Implementation of the "white box" memorandum Model

A memorandum role provides an interface for any object, that is, a wide interface. The State stored in the role is disclosed to all objects. Therefore, this implementation is also called "white box implementation ".

The "white box" implements storing the status of the initiator role in a place that everyone can see, so it destroys encapsulation. But through the self-discipline of programmers, we can also achieve most of the intention of the model to a certain extent. Therefore, the white box implementation is still meaningful.

The following is a schematic "white box implementation ".

Source code

The initiator role uses a newly created memo object to store its internal status.

Package COM. bankht. memento;/*** @ Author: -AK47 * @ Creation Time: 04:12:20, January 2, ** @ Class description: The initiator's role class, the initiator role uses a newly created memo object to store its internal status. */Public class originator {private string state;/*** factory method, returns a new memo object */Public memento createmento () {return new memento (State );} /*** restore the initiator to the State recorded by the memorandum object */Public void restoremento (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 );}}

 

The description object stores the input state of the initiator object.

Package COM. bankht. memento;/*** @ Author: -AK47 * @ Creation Time: 04:12:40, January 2, ** @ Class description: Role class. The object stores the status 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 owner role is responsible for saving the object of the memorandum, but never modifying (or even not viewing) the content of the object.

Package COM. bankht. memento;/*** @ Author: -AK47 * @ Creation Time: 04:13:03, December 2, ** @ Class description: Owner role is responsible for saving the memorandum object, however, the content of the memorandum object is never modified (or even not viewed. */Public class caretaker {private memento;/*** memorandum Value Method */Public memento retrievemento () {return this. memento;}/*** memorandum Assignment Method */Public void savememento (memento) {This. memento = memento ;}}

 

Client role

Package COM. bankht. memento;/*** @ Author: -AK47 * @ Creation Time: 04:13:24, January 2, ** @ Class description: client Angle class */public class client {public static void main (string [] ARGs) {originator o = new originator (); caretaker c = new caretaker (); // change the state of the owner object. setstate ("On"); // creates a memorandum object and stores the state of the initiator object. savemento (O. creatememento (); // modify the initiator's status o. setstate ("off"); // restores the state o of the initiator object. restorememento (C. retrievememento (); system. out. println (O. getstate ());}}

 

In the above schematic client role, first set the state of the initiator object to "on" and create a memorandum object to store the state; then, the state of the initiator object is changed to "off", and the initiator object is restored to the State stored by the memorandum object, that is, the "on" state.

The system sequence diagram reflects the time sequence of each role in the system. If you store the status of the initiator object to the time sequence diagram of the white box memo object.

We can see that the time sequence of system operation is as follows:

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

(2) Call the creatememento () method of the initiator role and create a memorandum object to 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:

  It can be seen that when the initiator object is restored to the State recorded by the memorandum object, the running sequence of the system is as follows:

(1) set the initiator status to "off ".

(2) retrieve the memorandum object from the owner object.

(3) restore the initiator object to the on State stored by the memorandum object.

Implementation of the "black box" memorandum Model

The memorandum role provides a wide interface for the initiator role object, while a narrow interface for other objects. This implementation is called "Black Box implementation ".

In Java, the method to implement the dual interface isBadgeDesignedInitiator role.

Set memento to the internal class of the originator class to encapsulate the memento object in originator. Provide an identification interface mementoif externally to caretaker and other objects. In this way, the originator class sees all the menmento interfaces, while caretaker and other objects only see the interfaces exposed by the mementoif interface.

The following figure shows the classes that use the internal classes to implement the memorandum mode.

Source code

The initiator angular class originator defines an internal memento class. Because all interfaces of this memento class are private, only the memento class can be called by itself and the initiator.

Package COM. bankht. memento. sample2;/*** @ Author: -AK47 * @ Creation Time: 04:33:55, January 2, ** @ Class description *: an internal memento class is defined in the initiator originator. * Because all interfaces of the memento class are private, only the memento class can be called by itself and the initiator. */Public class originator {private string state; Public String getstate () {return state;} public void setstate (string state) {This. state = State; system. out. println ("value assignment status:" + state);}/*** factory method, return a new memo object */Public mementoif creatememento () {return new memento (State);}/*** the initiator restores the state of the memorandum Object Record */Public void restorememento (mementoif memento) {This. setstate (memento ). getstate ();} private class memento implements mementoif {private string state;/*** constructor */private memento (string state) {This. state = State;} private string getstate () {return state;} private void setstate (string state) {This. state = State ;}}}

 

Narrow interface mementoif, which is an identifier interface, does not define any methods.

Package COM. bankht. memento. sample2;/*** @ Author: -AK47 * @ Creation Time: 04:34:25, January 2, ** @ Class description: narrow interface mementoif, which is an identifier interface, therefore, it does not define any methods. */Public interface mementoif {}

 

The owner's role-based caretaker can obtain a memorandum object that uses mementoif as an interface. Because this interface is only an identity interface, it is impossible for the owner role to change the content of this memorandum object.

Package COM. bankht. memento. sample2;/*** @ Author: -AK47 * @ Creation Time: 04:35:20, January 2, ** @ Class description: * The owner's role-based caretaker can obtain a memorandum object using the mementoif interface. Because this interface is only an interface for identification, * the owner role cannot change the content of this memorandum object. */Public class caretaker {private mementoif memento;/*** memorandum Value Method */Public mementoif retrievememento () {return memento ;} /*** memorandum Assignment Method */Public void savemento (mementoif memento) {This. memento = memento ;}}

 

Client role

Package COM. bankht. memento. sample2;/*** @ Author: -AK47 * @ Creation Time: 04:36:01, January 2, ** @ Class description: client Angle class */public class client {public static void main (string [] ARGs) {originator o = new originator (); caretaker c = new caretaker (); // change the state of the owner object. setstate ("On"); // creates a memorandum object and stores the state of the initiator object. savemento (O. creatememento (); // modify the state o of the initiator object. setstate ("off"); // restores the state o of the initiator object. restorememento (C. retrievememento ());}}

 

Client first

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

(2) Call the creatememento () method and create a memorandum object to store the State (the obvious type returned by the creatememento () method is the mementoif interface, the actual type is the memento object inside originator ).

(3) store the memorandum object to the owner object. Because the owner object only obtains the mementoif interface, the internal status of the object cannot be read.

(4) set the status of the initiator object to "off ".

(5) Call the retrievememento () method of the owner object to retrieve the memorandum object. Note that only the mementoif interface can be obtained at this time, so the internal status of this object cannot be read.

(6) Call the restorememento () method of the initiator object to restore the state of the initiator object to the state stored by the memorandum object, that is, the "on" state. Because the internal class memento of the initiator object implements the mementoif interface, this internal class is the actual type of the input memo object, therefore, the initiator object can read the internal state of the object using the private interface of the internal class memento.

Multiple checkpoints

The schematic Implementation of the preceding white box and black box is a simple implementation that only stores one State, or it can be called only one checkpoint. A common system usually needs to store more than one State, but multiple states, or multiple checkpoints.

The memorandum mode stores the state of the initiator object in the memorandum object, and the memorandum mode restores the initiator object to a Check Point stored by the memorandum object. The following provides a schematic implementation of the memorandum mode with multiple checkpoints.

Source code

Source code of the initiator role

Package COM. bankht. memento. multiple; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 04:55:00, January 2, ** @ Class description: source code of the initiator role */public class originator {private list <string> states; // check point index private int index;/*** inator */Public originator () {States = new arraylist <string> (); Index = 0;}/*** factory method, return a new memo object */Public memento createmento () {return new memento (States, index);}/*** restore the initiator to the State recorded by the memorandum object */Public void restorememento (memento) {States = memento. getstates (); Index = memento. getindex ();}/*** status Assignment Method */Public void setstate (string state) {states. add (State); index ++;}/*** auxiliary method to print all States */Public void printstates () {system. out. println ("Checkpoint Total:" + states. size () + ""); For (string state: States) {system. out. println (state );}}}

The description class, which can store any number of states. You can use the checkpoint index to retrieve the status of the checkpoint.

Package COM. bankht. memento. multiple; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 04:55:23, January 2, ** @ Class description: The Role class of the memo. This implementation can store any number of States, you can use the checkpoint index to retrieve the status of the checkpoint. */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

Package COM. bankht. memento. multiple; import Java. util. arraylist; import Java. util. list;/*** @ Author: -AK47 * @ Creation Time: 04:56:08, December 2, ** @ Class description: 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 = O. creatememento (); mementos. add (memento); Return Current ++;}/*** restore the initiator to a checkpoint */Public void restorememento (INT index) {memento = mementos. get (INDEX); O. restorememento (memento);}/*** delete a checkpoint */Public void removememento (INT index) {mementos. remove (INDEX );}}

Client role source code

Package COM. bankht. memento. multiple;/*** @ Author: -AK47 * @ Creation Time: 04:56:31, January 2, ** @ Class description: client role source code */public class client {public static void main (string [] ARGs) {originator o = new originator (); caretaker c = new caretaker (O ); // change the status o. setstate ("state 0"); // creates a checkpoint C. creatememento (); // Changes the status o. setstate ("state 1"); // creates a checkpoint C. creatememento (); // Changes the status o. setstate ("state 2"); // creates a checkpoint C. creatememento (); // Changes the status o. setstate ("State 3"); // creates a checkpoint C. creatememento (); // print all checkpoints O. printstates (); system. out. println ("----------------- restore checkpoint ---------------"); // restore to the second checkpoint C. restorememento (2); // print all checkpoints O. printstates ();}}

The running result is as follows:

Check Point Total: 4 states 0 state 1 State 2 State 3 ----------------- restore checkpoint --------------- check point total: 3 States 0 state 1 State 2

 

It can be seen that the client role constantly changes the status of the initiator role and stores it in the memorandum. By specifying the checkpoint index, you can restore the initiator role to the status corresponding to the corresponding checkpoint.

The following figure shows the activity sequence in which the initiator state is stored in the memo object:

The sequence of system running is as follows:

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

(2) Call the creatememento () method of the owner role. The owner role is responsible for calling the initiator role and the memorandum role, and storing the status of the initiator object.

  The activity sequence diagram of restoring the initiator object to the checkpoint of a memorandum object is as follows:

 

  As the role owner function is enhanced, the sequence of 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.

"Report history" Mode

The so-called "history-on-self pattern" mode is actually a variant of the memorandum mode. In memorandum mode, the initiator, caretaker, and memento roles are independent roles. Although in implementation, the memorandum class can be an internal Member class that initiates humans, the memorandum class remains independent as a role. In the "Review history" mode, the initiator role is also the owner role.

The class diagram of the "Report history" mode is as follows:

  The role of the memorandum has the following responsibilities:

(1) stores the internal status of the initiator object.

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

 The initiator role has the following responsibilities:

(1) create a memorandum object containing its current internal status.

(2) Use the memorandum object to store its internal status.

The client role is responsible for saving the memorandum object.

Source code

Narrow interface mementoif, which is an identifier interface, does not define any methods.

Package COM. bankht. memento. historyonself;/*** @ Author: -AK47 * @ Creation Time: 05:02:31, January 2, ** @ Class description: narrow interface mementoif, which is an ID interface, therefore, it does not define any methods. */Public interface mementoif {}

The initiator role also acts as the owner, that is, it maintains its own memorandum object.

Package COM. bankht. memento. historyonself;/*** @ Author: -AK47 * @ Creation Time: 05:02:57, January 2, ** @ Class description: The initiator role also acts as the owner role, in other words, it maintains its own memorandum object. */Public class originator {Public String state;/*** change state */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 new memento (this);}/*** restores the initiator to the State recorded by the memorandum object */Public void restorememento (mementoif memento) {memento M = (memento) memento; changestate (M. state);} private class memento implements mementoif {private string state;/*** constructor */private memento (originator O) {This. state = O. state;} private string getstate () {return state ;}}}

Client role

Package COM. bankht. memento. historyonself;/*** @ Author: -AK47 * @ Creation Time: 05:03:29, January 2, ** @ Class description: client Angle class */public class client {public static void main (string [] ARGs) {originator o = new originator (); // modify the status o. changestate ("state 0"); // creates a memorandum mementoif memento = O. creatememento (); // modify the O. changestate ("state 1"); // follow the memorandum to restore the object state o. restorememento (memento );}}

 

Because the "self-report history" is a special implementation form of the memorandum mode, it may be the most popular implementation form of the memorandum mode.

 

Related Article

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.