Objective
This time to introduce the memorandum model, is also a mode of behavior. Now people's smartphones will have memos such a function, we will also use, is to remember something, to prevent later forget. So what does the memo pattern look like? Is it the same as the memo on the phone? Here's a look at this.
Memo Mode Concept Introduction
A memo pattern is a capture of the internal state of an object without compromising encapsulation, and the state is saved outside that object. The object can then be restored to its previously saved state. Simple to say is usually play the customs clearance game, the first step forward 5 steps, the second time stepping on the poo, and then to return to the progress of the previous. For example structure:
Example
We still use a small example to briefly introduce the memo mode, when playing a step game, each step when the number of steps are added one, encountered props can quickly walk a few steps, or a few steps. Using the idea of Memo mode to implement this logic is the following code.
Game class
/*** Game*/@Data Public classGame {/*** Number of steps the player walks*/ Private intPlayerstep; /*** Backup Game *@return */ PublicGamememento Creategamememento () {return NewGamememento (Playerstep); } /*** Start playing games*/ Public voidPlay () {Playerstep= 0; } /*** Restore Backup *@paramGamememento*/ Public voidRestore (Gamememento gamememento) { This. Playerstep =gamememento.getplayersteps (); }}
Backup
/*** Backup*/@Getter Public classGamememento {/**Number of steps*/ Private intplayersteps; /*** Number of steps to backup *@paramplayersteps*/ PublicGamememento (intplayersteps) { This. Playersteps =playersteps; }}
Backup Information Management Classes
/*** Backup Information management class*/ Public classCaretaker {/**Backup*/ PrivateGamememento Gamememento; /**Restore Backup*/ PublicGamememento Retrievememento () {return This. Gamememento; } /**Save Backup*/ Public voidSavememento (Gamememento gamememento) { This. Gamememento =Gamememento; }}
Test
Public classPlayer { Public Static voidMain (string[] args) {Game game=NewGame (); System.out.println ("The game starts, picks up the skateboard, advances 10 steps"); Game.setplayerstep (10); //back up the current stateSYSTEM.OUT.PRINTLN ("Backup Current state"); Gamememento Gamememento=Game.creategamememento (); Caretaker caretaker=Newcaretaker (); Caretaker.savememento (Gamememento); System.out.println ("Backup Complete"); Game.play (); System.out.println ("Stepping on the stool, the current number of steps is:" +game.getplayerstep ()); System.out.println ("Revert to previous step"); Game.restore (Caretaker.retrievememento ()); System.out.println ("Recovery complete, current player steps are:" +game.getplayerstep ()); }}
Run results
The game starts, picks up the skateboard, advances 10 steps back up the current state backup to complete stepping on the poo, the current number of steps is:0 Restore to the previous step recovery completed, the current player steps are:10
The above example uses the annotations of the Lombok plugin for example: @Getterhe @data so the getter and setter methods are less written. After reading this small example, we should probably know what the logic of the memo pattern is, something similar to what we normally know about the rollback mechanism of a transaction. The following is an analysis of what the memo pattern consists of.
Memo Pattern Structure
The structure diagram is as follows
The composition role of the memo pattern is shown below.
Primitive Class (originator): Creates a Memo object that uses a memo to store its internal state.
responsible for human (caretaker): Responsible for keeping the memo object, cannot check or manipulate the contents of the memo.
Memo Class (Memento): Stores the internal state of the original, which, as needed, determines which internal states the memo stores as the original.
Summarize the benefits
1, it provides a state recovery implementation mechanism, so that users can easily return to a specific historical step, when the new state is invalid or there is a problem, you can use the temporarily stored memo to restore the state.
2, the memo implements the encapsulation of information, a memo object is a representation of the state of the original sender object, and will not be changed 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.
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.
Applicable scenarios
1. Save the whole state or part state of an object at a certain point in time, so that it can revert to the previous state when needed, and implement the undo operation.
2. Prevent external objects from destroying the encapsulation of an object's historical state, and avoid exposing the implementation details of the object's historical state to the outside object.
To learn more about design patterns, see the Java Design Patterns Learning record-gof design pattern Overview.