catalogue
Game character status recovery problem
Traditional solution for game character recovery
Traditional way of problem analysis
Basic introduction to memo mode
Schematic class diagram of memo mode
Game character restore state instance
Notes and details of memo mode
Memo mode
Game character status recovery problem
The game characters have attack power and defense power. They save their own state (attack power and defense power) before fighting the Boss. When fighting the Boss, their attack power and defense power decrease and recover from the memo object to the state before the war
Traditional solution for game character recovery
Traditional way of problem analysis
1) An object corresponds to an object that saves the object state. In this way, when there are many objects in our game, it is not conducive to management and costs a lot
2) The traditional method is to simply make a backup, create another object, and then put the data to be backed up into the new object, but this exposes the internal details of the object
3) Solution: = > memo mode
Basic introduction to memo mode
Basic introduction
1) Memo pattern captures the internal state of an object without destroying encapsulation, and saves the state outside the object. This will restore the object to its original saved state later
2) The memo mode can be understood here: in real life, the memo is used to record something to be done, or to record something that has been agreed, so as not to forget. At the software level, the memo mode has the same meaning. The memo object is mainly used to record a certain state or some data of an object. When fallback is required, the original data can be obtained from the memo object for recovery
3) Memo mode belongs to behavior mode
Schematic class diagram of memo mode
description of schematic class diagram - i.e. (roles and responsibilities of memo mode)
1) originator: object (object whose state needs to be saved)
2) Memento: memo object, which is responsible for keeping records, that is, the internal status of the Originator
3) Caretaker: Guardian object, which is responsible for saving multiple memo objects. It uses collection management to improve efficiency
4) Note: if you want to save the status of multiple originator objects at different times, you can just need HashMap < string, set >
Code implementation
theory.zip
import java.util.ArrayList; import java.util.List; public class Caretaker { //There will be many memo objects in the List collection private List<Memento> mementoList = new ArrayList<Memento>(); public void add(Memento memento) { mementoList.add(memento); } //Get the memo object of the index Originator (i.e. saved state) public Memento get(int index) { return mementoList.get(index); } } import java.util.ArrayList; import java.util.HashMap; public class Client { public static void main(String[] args) { // TODOAuto-generated method stub Originator originator = new Originator(); Caretaker caretaker = new Caretaker(); originator.setState(" state#1) attack power (100 "); //Saved the current state caretaker.add(originator.saveStateMemento()); originator.setState(" state#2. Attack power (80 "); caretaker.add(originator.saveStateMemento()); originator.setState(" state#3. Attack power (50 "); caretaker.add(originator.saveStateMemento()); System.out.println("The current status is =" + originator.getState()); //To get state 1, restore the originator to state 1 originator.getStateFromMemento(caretaker.get(0)); System.out.println("Restore to state 1 , The current status is"); System.out.println("The current status is =" + originator.getState()); } } public class Memento { private String state; //constructor public Memento(String state) { super(); this.state = state; } public String getState() { return state; } } public class Originator { private String state;//status information public String getState() { return state; } public void setState(String state) { this.state = state; } //Write a method to save a state object Memento //So write a method that returns Memento public Memento saveStateMemento() { return new Memento(state); } //Restore state through memo object public void getStateFromMemento(Memento memento) { state = memento.getState(); } }
Game character restore state instance
1) Application example requirements
The game characters have attack power and defense power. They save their own state (attack power and defense power) before fighting the Boss. When fighting the Boss, their attack power and defense power decrease and recover from the memo object to the state before the war
2) Train of thought analysis and illustration (class diagram)
3) Code implementation
game.zip
import java.util.ArrayList; import java.util.HashMap; //Guardian object to save the state of the game character public class Caretaker { //If the status is saved only once private Memento memento; //Save state for GameRole multiple times //private ArrayList<Memento> mementos; //Save multiple states for multiple game characters //private HashMap<String, ArrayList<Memento>> rolesMementos; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } } public class Client { public static void main(String[] args) { // TODOAuto-generated method stub //Create game characters GameRole gameRole = new GameRole(); gameRole.setVit(100); gameRole.setDef(100); System.out.println("and boss Pre war state"); gameRole.display(); //Save the current state to caretaker Caretaker caretaker = new Caretaker(); caretaker.setMemento(gameRole.createMemento()); System.out.println("and boss War~~~"); gameRole.setDef(30); gameRole.setVit(30); gameRole.display(); System.out.println("After the war, use the memo object to restore to the front of the station"); gameRole.recoverGameRoleFromMemento(caretaker.getMemento()); System.out.println("Status after recovery"); gameRole.display(); } } public class GameRole { private int vit; private int def; //Create Memento, that is, get Memento according to the current status public Memento createMemento() { return new Memento(vit, def); } //Restore the state of GameRole from the memo object public void recoverGameRoleFromMemento(Memento memento) { this.vit = memento.getVit(); this.def = memento.getDef(); } //Displays the status of the current game character public void display() { System.out.println("Current attack power of game character:" + this.vit + " Defensive power: " + this.def); } public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } } public class Memento { //aggressivity private int vit; //Defensive power private int def; public Memento(int vit, int def) { super(); this.vit = vit; this.def = def; } public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } }
Notes and details of memo mode
1) It provides users with a mechanism to restore the state, which can make it easier for users to return to a historical state
2) The encapsulation of information is realized, so that users do not need to care about the details of state preservation
3) If there are too many member variables of a class, it is bound to occupy a large amount of resources, and each save will consume a certain amount of memory. This should be noted
4) Applicable application scenarios: 1. Regret medicine. 2. Archive when playing games. 3. ctri + z in Windows. 4. Backward in IE. 4. Transaction management of database
5) To save memory, memo mode can be used in conjunction with prototype mode