Original address: http://www.work100.net/training/monolithic-architecture-design-patterns-memento-pattern.html
More tutorials: Beam cloud - free course
Memo mode
Serial number | Chapter in text | video |
---|---|---|
1 | Summary | |
2 | Realization |
Please refer to the navigation above for reading
1. overview
Memo pattern saves a state of an object to restore it when appropriate. Memo mode belongs to behavioral mode.
Intention:
Without destroying encapsulation, capture the internal state of an object and save the state outside the object.
Main solutions:
The memo mode is to capture the internal state of an object and save the state outside the object without destroying the encapsulation, so that the object can be restored to the original saved state in the future.
When to use:
In many cases, we always need to record the internal state of an object. The purpose of this is to allow the user to cancel uncertain or wrong operations, recover to his original state, and make him have "regret medicine" to take.
How to solve:
The object state is stored exclusively through a memo class.
Key code:
Customer is not coupled with memo class, but with memo management class.
Application example:
- Regret.
- Save when playing the game.
- ctrl + z in Windows.
- Back in IE.
- Transaction management of database.
Advantage:
- It provides a mechanism for users to recover the state, which makes it easier for users to return to a certain historical state.
- The encapsulation of information is realized, so that users do not need to care about the details of state preservation.
Disadvantages:
Consume resources. If there are too many member variables in a class, it will take up a large amount of resources, and every save will consume a certain amount of memory.
Usage scenario:
- The relevant state scenarios of data need to be saved / recovered.
- Provides an operation that can be rolled back.
matters needing attention:
- In order to conform to Dimiter principle, we need to add a management memo class.
- To save memory, use prototype mode + memo mode.
2. implementation
Memo mode uses three classes, Memento, Originator, and CareTaker.
Memento contains the state of the object to be recovered.
The Originator creates and stores the state in the Memento object.
The Caretaker object is responsible for recovering the state of the object from Memento.
MementoPatternDemo, our demo class uses CareTaker and Originator objects to display the state recovery of objects.
Step 1
Create a Memento class.
Memento.java, the code is as follows:
public class Memento { private String state; public Memento(String state){ this.state = state; } public String getState(){ return state; } }
Step 2
Create the Originator class.
Originator.java, the code is as follows:
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(); } }
Step 3
Create the CareTaker class.
CareTaker.java, the code is as follows:
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); } }
Step 4
Use CareTaker and Originator objects.
The code of MementoPatternDemo.java is as follows:
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()); } }
Step 5
Execute the program and output the result:
Current State: State #4 First saved State: State #2 Second saved State: State #3
Last article: Intermediary model
Next article: Observer mode