Design pattern (Behavioral) Memo mode (Memento pattern)

Source: Internet
Author: User

PS One sentence: Eventually choose Csdn to organize the publication of the knowledge points of these years, the article parallel migration to CSDN. Because CSDN also support markdown grammar, Ah!

"Craftsman Joshui Http://blog.csdn.net/yanbober" read the previous "pattern of design (behavioral) intermediary (mediator pattern)" http://blog.csdn.net/yanbober/ article/details/45533335

Overview

Memo mode provides an implementation mechanism for state recovery that allows users to easily return to a specific historical step, and when the new state is invalid or problematic, the state can be restored using a temporarily stored memo, and many software now offer undo (undo) operations, in which a memo pattern is used.

Core

concept: captures the internal state of an object without destroying the package, and saves the state outside of the object, so that the object can be restored to its previously saved state at a later time. It is an object-oriented pattern with the alias token.

Memo mode structure important core modules:

Originator (original generator)

It is a generic class that can create a memo and store its current internal state, or it can use a memo to restore its internal state, and generally will need to save the internal state of the class design as the original.

Memento (Memo)

Stores the internal state of the original, depending on the origin of the generator to decide which internal state to save. The design of a memo can generally refer to the design of the original, and determine the attributes in the memo class according to the actual needs. It is important to note that, in addition to the sender itself and the responsible human, the memo object cannot be used directly by other classes, the design of the original is different in the implementation mechanism of the programming language.

Caretaker (person in charge)

The person in charge, also known as the manager, is responsible for saving the memo, but cannot manipulate or check the contents of the memo. In the owner class, you can store one or more Memo objects, which are only responsible for storing objects, not modifying objects, and need not know the implementation details of the object.

Precautions:

Because the intermediate state of the sender is stored in the memo, it is necessary to prevent other objects from accessing the memo, especially if other objects are not allowed to modify the memo. Therefore, in the design of the memo class to consider its encapsulation, in addition to the originator class, do not allow other classes to invoke the memento of the memo class constructor and related methods, if not considered encapsulation, allowing other classes to call, will result in the memo saved in the history state changes, The state restored by the undo operation is no longer a true historical state, and the memo pattern loses its meaning.

Usage Scenarios

Saves the entire state or part of an object at a certain point in time, so that it can revert back to its previous state when needed, enabling the undo operation.

Prevents external objects from destroying the encapsulation of an object's historical state.

Program Ape Instance

Here is an example of a memo pattern (simulating an Android robot sitting on a two-dimensional planar map with the punctuation moving, can be back one step), strict adherence to the pattern of several core templates, not much explanation:

 PackageYanbober.github.io;//originator (original generator)Class Robotposition {PrivateString Mrobotname;Private intMcurxpos;Private intMcurypos; Public robotposition(String Mrobotname,intMcurxpos,intMcurypos) { This. mrobotname = Mrobotname; This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } Public void SetPos(intMcurxpos,intMcurypos) { This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } Public void Drawscreen() {System.out.println ("#RobotPosition #"+mrobotname+": "+mcurxpos+", "+mcurypos); } PublicRobotpositionmementoSave() {return NewRobotpositionmemento (Mrobotname, Mcurxpos, Mcurypos); } Public void Restore(Robotpositionmemento Memento) { This. Mrobotname = Memento.getmrobotname (); This. Mcurxpos = Memento.getmcurxpos (); This. Mcurypos = Memento.getmcurypos (); }}//memento (Memo)Class Robotpositionmemento {PrivateString Mrobotname;Private intMcurxpos;Private intMcurypos; Public Robotpositionmemento(String Mrobotname,intMcurxpos,intMcurypos) { This. mrobotname = Mrobotname; This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } PublicStringGetmrobotname() {returnMrobotname; } Public int Getmcurxpos() {returnMcurxpos; } Public int Getmcurypos() {returnMcurypos; }}//caretaker (person in charge)Class Robotpositioncaretaker {PrivateRobotpositionmemento Memento; PublicRobotpositionmementoGetmemento() {returnMemento } Public void Setmemento(Robotpositionmemento Memento) { This. Memento = Memento; }}//Client Public  class Main {     Public Static void Main(string[] args) {Robotpositioncaretaker caretaker =NewRobotpositioncaretaker (); Robotposition robot =NewRobotposition ("Android",0,0);        Robot.drawscreen ();        Caretaker.setmemento (Robot.save ()); Robot.setpos (0,1);        Robot.drawscreen ();        Caretaker.setmemento (Robot.save ()); Robot.setpos (5,5); Robot.drawscreen ();//back on StepRobot.restore (Caretaker.getmemento ());        Robot.drawscreen ();    Caretaker.setmemento (Robot.save ()); }}

Upgrade Loading force:

What is the idea that a robot can only go back one step back? Too weak, and then how to support multi-step back AH. The above changes are as follows (support multi-step fallback, do not explain too much):

 PackageYanbober.github.io;ImportJava.util.ArrayList;ImportJava.util.List;//originator (original generator)Class Robotposition {PrivateString Mrobotname;Private intMcurxpos;Private intMcurypos; Public robotposition(String Mrobotname,intMcurxpos,intMcurypos) { This. mrobotname = Mrobotname; This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } Public void SetPos(intMcurxpos,intMcurypos) { This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } Public void Drawscreen() {System.out.println ("#RobotPosition #"+mrobotname+": "+mcurxpos+", "+mcurypos); } PublicRobotpositionmementoSave() {return NewRobotpositionmemento (Mrobotname, Mcurxpos, Mcurypos); } Public void Restore(Robotpositionmemento Memento) { This. Mrobotname = Memento.getmrobotname (); This. Mcurxpos = Memento.getmcurxpos (); This. Mcurypos = Memento.getmcurypos (); }}//memento (Memo)Class Robotpositionmemento {PrivateString Mrobotname;Private intMcurxpos;Private intMcurypos; Public Robotpositionmemento(String Mrobotname,intMcurxpos,intMcurypos) { This. mrobotname = Mrobotname; This. Mcurxpos = Mcurxpos; This. Mcurypos = Mcurypos; } PublicStringGetmrobotname() {returnMrobotname; } Public int Getmcurxpos() {returnMcurxpos; } Public int Getmcurypos() {returnMcurypos; }}//caretaker (person in charge)Class Robotpositioncaretaker {PrivateList<robotpositionmemento> mementolist =NewArraylist<> (); PublicRobotpositionmementoGetmemento(intSETP) {returnMementolist.get (SETP); } Public void Setmemento(Robotpositionmemento Memento) { This. Mementolist.add (Memento); }}//Client Public  class Main {     Public Static void Main(string[] args) {Robotpositioncaretaker caretaker =NewRobotpositioncaretaker (); Robotposition robot =NewRobotposition ("Android",0,0);        Robot.drawscreen ();        Caretaker.setmemento (Robot.save ()); Robot.setpos (0,1);        Robot.drawscreen ();        Caretaker.setmemento (Robot.save ()); Robot.setpos (5,5); Robot.drawscreen ();//back to step 1Robot.restore (Caretaker.getmemento (0));        Robot.drawscreen ();    Caretaker.setmemento (Robot.save ()); }}

Operation Result:

robotposition#android:0, 0
robotposition#android:0, 1
Robotposition#android:5, 5
robotposition#android:0, 0

sum up a

Memo Mode Advantages:

Provides an implementation mechanism for state recovery that allows the user to easily return to a specific historical step and, when the new state is invalid or problematic, can be restored using a memo that is temporarily stored.

The encapsulation of information is implemented, and a memo object is a representation of the state of the sender object and is not altered by other code. The memo holds the status of the original, and storing the memo object with a collection of lists, stacks, and so on can enable multiple undo operations.

Memo Mode Disadvantages:

Resource consumption is too large, if you need to save the original class member variable too many, it is inevitable to occupy a large amount of storage space, each time the state of the object is saved need to consume some system resources.

"Craftsman Joshui Http://blog.csdn.net/yanbober" continues to read the "design pattern (behavioral) Memo mode (Memento pattern)" http://blog.csdn.net/yanbober/article/ details/45535821

Design pattern (Behavioral) Memo mode (Memento 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.