This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
The Memento Pattern stores a certain state of an object so that it can be restored at an appropriate time.
The memo pattern belongs to the behavior pattern.
introduce
describe | parsing |
---|---|
intentions | Capture the internal state of an object and store the state outside of the object without breaking encapsulation. |
Mainly to solve | The memo mode captures the internal state of an object without breaking encapsulation and stores the state outside of the object so that the object can be restored to its original state at a later date. |
When to use | Many times it is necessary to record the internal state of an object. The purpose of this method is to allow the user to cancel uncertain or incorrect operations and restore the original state so that the behavior can be rolled back. |
How to solve | There is a special class for storing object state: the Memo class. |
The key code | The customer is not coupled to the memo class, but to the memo management class. |
Examples of application | 1, regret medicine. 2. Save while playing the game. 3. Back in the browser. 4. Transaction management of database. |
advantages | 1. It provides a mechanism for the user to restore the state, so that the user can easily return to a historical state. 2. Information encapsulation is realized, so that users do not need to care about the details of state preservation. |
disadvantages | Consume resources. If a class has too many member variables, it will take up a lot of resources, and each save will consume a certain amount of memory. |
Usage scenarios | 1. State scenarios where data needs to be saved or recovered. 2. Provide a rollback operation. |
Matters needing attention | 1. To comply with Demeter’s principle, add a management memo class. 2. To save memory, use prototype mode + memo mode. |
Implementation example
The example contains four classes
Memento contains the state of the object to be restored (Memento).
Originator creates and stores state in Memento objects.
The Caretaker object restores the state of the object from the Memento.
4, MementoPatternDemo scenario class, demo memo mode.
The class diagram is as follows:
Step 1- Create the memo class
Create the Memento class:
// Memento.java public class Memento { private String state; public Memento(String state){ this.state = state; } public String getState(){ return state; }}Copy the code
Step 2- Create the class to record the memo
Creating an Originator class:
// Originator.java public class Originator { private String state; public void setState(String state){ this.state = state; } public String getState(){ return state; } public Memento saveStateToMemento(){ return new Memento(state); } public void getStateFromMemento(Memento Memento){ state = Memento.getState(); }}Copy the code
Step 3- Create a memo management class
Create CareTaker class:
// CareTaker.java import java.util.ArrayList; import java.util.List; public class CareTaker { private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento state){ mementoList.add(state); } public Memento get(int index){ return mementoList.get(index); }}Copy the code
Step 4- Create the scene class
Use the CareTaker and Originator object.
// MementoPatternDemo.java public class MementoPatternDemo { public static void main(String[] args) { Originator originator = new Originator(); CareTaker careTaker = new CareTaker(); originator.setState("State #1"); originator.setState("State #2"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #3"); careTaker.add(originator.saveStateToMemento()); originator.setState("State #4"); System.out.println("Current State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(0)); System.out.println("First saved State: " + originator.getState()); originator.getStateFromMemento(careTaker.get(1)); System.out.println("Second saved State: " + originator.getState()); }}Copy the code
Step 5- Output print
Current State: State #4
First saved State: State #2
Second saved State: State #3
Copy the code