preface

The memo pattern is rarely used, but it is also a behavioral design pattern.

directory

A, definitions,

Capture the internal state of an object without breaking encapsulation and save that state outside of the object so that the object can later be restored to its previously saved state.

Two, mode principle analysis

The memo pattern contains two key actors

  • Originator: In addition to creating its own properties and business logic, it provides methods create() and restore(memento) to save and restore a copy of the object

  • Memento: Used to hold the state of all the properties of the original object for future undo operations

The following is the single state memoranda pattern, and the Map set is needed for multiple states.

Public 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()); }} 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; } // Caretaker public 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){// Define Originator Originator = new Originator(); // Define the Caretaker Caretaker Caretaker = new Caretaker(); // Create a memotive.setmemento (originator.creatememento ()); / / return a memo originator. RestoreMemento (caretaker. GetMemento ()); }}Copy the code

Iii. Usage Scenarios

  • Need to save the state of an object at a certain point in time

  • Provide a rollback operation, such as CTRL + Z in Word

  • Copy scenarios that need to be monitored. For example, if you want to monitor the properties of an object, but the monitoring should not be called as the main business of the system, consider backing up a copy and analyzing it in a child thread

  • Database connection transaction management is used in the memo mode

Four advantages,

  • Ability to quickly undo changes to object state

  • Increases the extensibility of your code. Instead of adding new state records to the original object, the memos mode saves the state of the original object by adding external objects, which can be easily cancelled when the state of the object is no longer needed. By the same token, adding memo objects is easy

  • Helps cache historical object state. For example, in the customer service conversation, for some important conversations (return, exchange, price guarantee, etc.), we will record the object data, the traditional approach is to call the service interface once, once the service failure is easy to cause the chat reply speed is slow; The use of memorandum mode can record these important data information (order data provided by the user), without the need to repeatedly query the interface, so as to improve the efficiency of replying to customers

Five, the disadvantages

  • The life cycle of memos, proactively manage and release them

  • The performance of memos is affected by too much backup