Catalogue of series articles
Design pattern - design principle
Create mode - singleton mode (I)
Creation mode - factory mode (II)
Creation mode - prototype mode (III)
Creative mode - builder mode (4)
Structural mode - adapter mode (I)
Structural mode - bridge mode (II)
Structural mode - decorator mode (III)
Structural mode - combined mode (IV)
Structural mode - appearance mode (V)
Structural mode - Xiangyuan mode (6)
Structural mode - agent mode (7)
Behavioral mode - template method mode (I)
Behavioral mode - command mode (II)
Behavioral model visitor model (III)
Behavioral mode iterator mode (4)
Behavioral model observer model (V)
Behavioral model - intermediary model (6)
Behavioral model - memorandum model (7)
Behavioral mode interpreter mode (8)
Behavioral mode - state mode (IX)
Behavioral model - Strategic Model (10)
Behavioral model - responsibility chain model (11)
preface
1, Memo mode
1.1 introduction to memo mode
- Memo mode:
Also known as snapshot mode, it captures the internal state of an object without destroying the encapsulation, and saves the state outside the object, so that the object can be restored to the original saved state when necessary in the future;
1.2 memorandum mode structure
- Originator role:
- It is also called the initiator role. It records the internal status information at the current time, provides the function of creating memos and restoring memo data, and realizes other business functions. It can access all the information in memos;
- Memo role:
- Complex storage of the initiator's internal status, providing these supplementary status to the initiator when necessary;
- Caretaker role:
- Manage the memo and provide the function of saving and lake memo, but it can't access and modify the forgotten content;
2, Realize
example:
- Zhang San is playing a game. He is playing. Suddenly, the child cries and wants to eat milk. You save the game and make milk for the child...
2.1 realization of memorandum
package com.dozezz.designpattern.memento; import java.util.Random; /** * Initiator */ public class Originator { /** * Remaining gold coins */ private Integer coin; /** * Blood volume */ private Integer hp; /** * Blue quantity */ private Integer mp; /** * Grade */ private Integer level; public Memento createMemento(){ return new Memento(this.coin,this.hp,this.mp,this.level); } public void recovery(Memento memento){ this.setCoin( memento.getCoin()); this.setHp(memento.getHp()); this.setMp(memento.getMp()); this.setLevel(memento.getLevel()); } public void playGame(){ System.out.println("Start playing games.."); int i = new Random().nextInt(100); this.coin = i ; this.hp = i ; this.mp = i ; this.level = i ; } public void display(){ System.out.println(String.format("Current role: gold coin: %d Blood volume:%d Blue amount:%d Grade:%d", this.coin,this.hp,this.mp,this.level)); } public Integer getCoin() { return coin; } public void setCoin(Integer coin) { this.coin = coin; } public Integer getHp() { return hp; } public void setHp(Integer hp) { this.hp = hp; } public Integer getMp() { return mp; } public void setMp(Integer mp) { this.mp = mp; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } }
package com.dozezz.designpattern.memento; /** * memorandum */ public class Memento { /** * Remaining gold coins */ private Integer coin; /** * Blood volume */ private Integer hp; /** * Blue quantity */ private Integer mp; /** * Grade */ private Integer level; public Memento(Integer coin, Integer hp, Integer mp, Integer level) { this.coin = coin; this.hp = hp; this.mp = mp; this.level = level; } public Integer getCoin() { return coin; } public void setCoin(Integer coin) { this.coin = coin; } public Integer getHp() { return hp; } public void setHp(Integer hp) { this.hp = hp; } public Integer getMp() { return mp; } public void setMp(Integer mp) { this.mp = mp; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } }
package com.dozezz.designpattern.memento; import java.util.ArrayList; import java.util.HashMap; import java.util.List; /** * controller */ public class CareTaker { /** * Save memo object only once */ // private Memento memento; /** * Save many memo objects */ private List<Memento> mementoList = new ArrayList<>(); /** * Save multiple memo objects for multiple roles */ // private HashMap<String ,List<Memento>> listHashMap = new HashMap<>(); public void addMemento(Memento memento) { if (!mementoList.contains(memento)) { mementoList.add(memento); } } /** * Get the memo object of the index Originator (i.e. saved state) * @param index * @return */ public Memento getMemento(int index) { return mementoList.get(index); } }
package com.dozezz.designpattern.memento; /** * Main test class */ public class ClientTest { public static void main(String[] args) { CareTaker careTaker = new CareTaker(); Originator originator = new Originator(); originator.playGame(); Memento memento = originator.createMemento(); careTaker.addMemento(memento); originator.display(); originator.playGame(); Memento memento1 = originator.createMemento(); careTaker.addMemento(memento1); originator.display(); Memento memento2 = careTaker.getMemento(0); originator.recovery(memento2); originator.display(); } }
3, Memo mode summary
3.1 application scenario of memo
- regret;
- Playing games and archiving;
- Transaction management of database;
- ...
3.2 differences between memo mode and command mode
- In the command mode, the command behavior of "method call", or request, is further abstracted and encapsulated as an object, so that the function of request recording can be achieved, and then the revocation operation can be realized;
- The memo mode is also used to record the internal state of the object and do the basic work for the cancellation operation;
- The command mode focuses on the log of request history, records the trajectory of the operation, and calls the receiver's revocation method. The revocation method basically needs the internal state of the object, and the memo mode is the state change of the example object.
3.3 advantages and disadvantages of memo mode
- Provides a mechanism to restore status. When users need it, they can easily restore the data to a historical state;
- The encapsulation of internal state is realized
4, References
- http://c.biancheng.net/view/1390.html
- https://www.cnblogs.com/noteless/p/10178477.html
- https://www.bilibili.com/video/BV1G4411c7N4?p=54&spm_id_from=pageDriver