Design Pattern Notes 20 - Memento Mode (memento)

Game character state recovery problem

Game characters have attack power and defense power, save their own state (attack power and defense power) before the battle against the boss, and after the battle against the boss, the attack power and defense power will drop, and the memo object will return to the state before the battle

 

Traditional way of problem analysis

1) An object corresponds to an object that saves the state of the object, so when there are many objects in our game, it is not conducive to management, and the overhead is also very high.

2) The traditional way is to simply make a backup, new out another object, and then put the data that needs to be backed up into this new object, but this exposes the internal details of the object

3) Solution: => memo mode

 

basic introduction

1) Memento Pattern (Memento Pattern) Captures the internal state of an object and saves this state outside the object without breaking the encapsulation. This will later restore the object to its previously saved state

2) You can understand the memo mode here: memos in real life are used to record certain things to be done, or to record things that have reached a common opinion, in case they are forgotten. At the software level, the memo mode has the same meaning. The memo object is mainly used to record a certain state of an object, or some data. When a rollback is required, the original data can be obtained from the memo object for recovery operations.

3) Memo mode is a behavioral mode

 

 

Principle class diagram of the memo pattern

 

Explanation of the principle class diagram - that is (roles and responsibilities of the memo mode)

1) originator: object (object that needs to save state)

2) Memento: Memento object, responsible for keeping good records, that is, the internal state of Originator

3) Caretaker: Guardian object, responsible for saving multiple memo objects, using collection management to improve efficiency

4) Note: If you want to save the status of multiple originator objects at different times, you can also, just need HashMap <String, collection>

 

 

Core code:

        //Save the current state of caretaker
        Caretaker caretaker = new Caretaker();
        caretaker.setMemento(gameRole.createMemento());
 

Among them, the caretaker maintains a Memento object. Once it needs to be restored, the original state can be taken out of the Memento object:

gameRole.recoverGameRoleFromMemento(caretaker.getMemento());

 

There is also another way of writing, Caretaker can keep a Memento queue.

 

Notes and Details of the Memento Pattern

1) Provide the user with a mechanism to restore the state, allowing the user to return to a certain historical state more conveniently

2) Encapsulation of information is achieved, so that users do not need to care about the details of state preservation

3) If there are too many member variables of the class, it will inevitably occupy relatively large resources, and each save will consume a certain amount of memory, which requires attention

4) Applicable application scenarios: 1. Regret medicine. 2. Archive when playing games. 3. ctri + z in Windows. 4. Back in IE. 4. Database transaction management

5) In order to save memory, the memo mode can be used in conjunction with the prototype mode

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_22059611/article/details/103297765