Design mode-Memo mode (Memento)

Source: Internet
Author: User

What is Memo mode? captures the internal state of an object without compromising encapsulation, and saves that state outside of that object, so that later object state can be restored to the previously saved state.      When we are programming, we often need to save the middle state of the object, and when necessary, we can revert to this state. For example, when we are programming with eclipse, if we write a mistake (such as accidentally accidentally deleting a few lines of code), we want to return to the state before deletion, we can use CTRL + Z to return. At this point we can use the memo mode to achieve.       code Example:The code shows an example of a single-state single-backup, which is very simple: the originator in the class has to be backed up in order to recover when needed; in the memento class, there is also a The state variable, which is used to store the temporary states of the status variable in the originator class, and the caretaker class is used to manage the memo class to write state to or retrieve state from the memo object.
   Initiator class Originator {private String state = "";     Public String GetState () {return state;     public void SetState (String state) {this.state = state;     } public Memento Creatememento () {return new Memento (this.state);     } public void Restorememento (Memento Memento) {this.setstate (Memento.getstate ());     }}//Memo 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;     }}//management of Memos class caretaker {private Memento Memento;     Public Memento Getmemento () {return Memento;     } public void Setmemento (Memento Memento) {this.memento = Memento;          }}public class Client {public static void main (string[] args) {Originator originator = new originator (); Originator.setstate ("Status 1");          SYSTEM.OUT.PRINTLN ("Initial state:" +originator.getstate ());          Caretaker caretaker = new Caretaker ();          Caretaker.setmemento (Originator.creatememento ());          Originator.setstate ("State 2");          SYSTEM.OUT.PRINTLN ("changed state:" +originator.getstate ());          Originator.restorememento (Caretaker.getmemento ());     System.out.println ("Post-Restore Status:" +originator.getstate ()); }}<strong></strong>

The advantages and disadvantages of Memo mode and the applicable scenarios

The advantages of the memo pattern are:

    • When the status of the initiator role changes, it is possible that this is a wrong change, we can use the memo mode to restore the error.
    • The status of the backup is saved outside the initiator role, so that the initiator role does not need to manage the status of each backup.

Cons of Memo mode:

    • In practical applications, the memo mode is multi-state and multi-backup, the status of the initiator role needs to be stored in the memo object, the consumption of resources is more serious.

If there is a need to provide rollback operations, the use of Memo mode is ideal, such as JDBC transaction operations, text editor CTRL + Z recovery, and so on.


Other design modes:

Design mode-Adapter mode (Adapter)

Design mode-agent mode (proxy)

Design pattern-Decorative mode (Decorator)

Design pattern--Dimitri Law (LOD/LKP)

Design Patterns-Dependency reversal principle

Design mode-open/Close principle (OCP)

Design Patterns-Single Responsibility principle (SRP)




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.