[Python design mode] 18th Chapter game role Backup--Memo mode

Source: Internet
Author: User

GitHub Address: Https://github.com/cheesezh/python_design_patterns

Topic

Using code to simulate the following scenarios, a game character has vitality, attack, defense and other data, before and after the boss of the data value will change, if the player challenges boss failure, we allow the player to restore the game data to the boss before the duel state.

Base version
Class Gamerole (): Def __init__ (self): self.vitality = 0 Self.attack = 0 self.defense = 0        def state_display (self): print ("Current role Status:") print ("Stamina:", self.vitality) print ("Attack:", Self.attack)        Print ("Defense:", Self.defense) def get_init_state (self): "" "loads the initial state from the server or local disk" " self.vitality = Self.attack = Self.defense = + def fight (self): "" "with boss    Duel, data loss is 0 "" "self.vitality = 0 Self.attack = 0 self.defense = 0 def main ():  Mario = Gamerole () mario.get_init_state () # Before battle boss, get initial role status Mario.state_display () backup = Gamerole () # Save Game Progress backup.vitality = mario.vitality Backup.attack = mario.attack Backup.defense = mario.defense Mario . Fight () # War boss Mario.state_display () mario.vitality = backup.vitality # Read progress, re-visited Mario.attack = backup. Attack Mario.defense= Backup.defense Mario.state_display () main () 
当前角色状态:体力: 100攻击: 100防御: 100当前角色状态:体力: 0攻击: 0防御: 0当前角色状态:体力: 100攻击: 100防御: 100
Comments

The main problem is client invocation:

    1. Game role details exposed to the client, the client needs to know the game character Vitality, attack, defense and other details;
    2. The client also needs to back up the role of the game;
    3. If you later add "magic value" or modify the existing data, you have to change the client;
    4. The process of recovering a role also has the same problem;
Memo Mode

Memo mode captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state. Mainly consists of three kinds:

    • Initiator (originator): Responsible for creating a memo memento that records its internal state at the current moment and can use the memo to restore its internal state. Originator can determine the internal state of memento storage originator as needed;
    • Memo (Memento): is responsible for storing the internal state of the originator object and prevents objects other than originator from accessing the memo Memento. The memo has two interfaces, and caretaker can only see the narrow interface of the memo, which can only pass the memo to other objects. Originator can see a wide interface, allowing it to access all data returned to the previous state;
    • Manager (Caretaker): Responsible for the preservation of the memorandum Memento, the contents of the memo can not be manipulated or checked.

In the scene of the topic, the game role class is equivalent to originator, using the same "game role" instance "backup" to make memos, which can be considered when it is necessary to save all the information, and using clone to achieve memento state preservation may be a better method, However, this is equivalent to opening up all the interfaces of the originator to the upper application, which is sometimes inappropriate for saving backups.

So what do we need to do when we don't need to save all the information for use? When we need to save not all the information, but only part of the information, then there should be a separate memo class Memento, it only has the information needed to save the properties.

Improved version-Memo mode
Class Gamerole (): Def __init__ (self): self.vitality = 0 Self.attack = 0 self.defense = 0        def state_display (self): print ("Current role Status:") print ("Stamina:", self.vitality) print ("Attack:", Self.attack)        Print ("Defense:", Self.defense) def get_init_state (self): "" "loads the initial state from the server or local disk" " self.vitality = Self.attack = Self.defense = + def fight (self): "" "with boss Duel, data loss is 0 "" "self.vitality = 0 Self.attack = 0 self.defense = 0 def save_state (sel        f): "" "Add the" Save state "Method" "" Return Rolestatememento (Self.vitality, Self.attack, Self.defense)        def recovery_state (self, Memento): "" "New Method" "" "," "" Self.vitality = memento.vitality Self.attack = Memento.attack Self.defense = Memento.defense class Rolestatememento (): "" "Game role State store Box "" "Def __init__(self, vitality, attack, defense): self.vitality = Vitality Self.attack = Attack Self.defense = Defen Se class Rolestatecaretaker (): "" "Role Status Manager Class" "Def __init__ (self): Self.memento = None D EF Main (): Mario = Gamerole () mario.get_init_state () mario.state_display () State_admin = Rolestatecaretake R () State_admin.memento = Mario.save_state () # The Save of the game role is transparent to the client Mario.fight () Mario.state_display () ma Rio.recovery_state (State_admin.memento) # Game role Recovery is transparent to the client Mario.state_display () main ()
当前角色状态:体力: 100攻击: 100防御: 100当前角色状态:体力: 0攻击: 0防御: 0当前角色状态:体力: 100攻击: 100防御: 100
Comments

Encapsulate the details that need to be saved in the Memento class, and change the details of the save without affecting the client.

The memento mode comparison is useful for classes that are complex, but need to maintain or record property history, or if a property that needs to be saved is only a small part of many properties, originator can revert to the previous state based on the saved memento information.

The command pattern also has a similar undo effect, and if you need to implement a command revocation function when using command mode in a system, the command mode can use Memo mode to store the state of the revocable operation. Sometimes the internal information of some objects must be stored outside of the object, but must be read by the object itself, when the use of memos can be complex object internal information to the other objects to shield, so as to properly maintain the encapsulated boundary.

When the status of a character changes, it is possible that the state is not valid, and you can use a temporarily stored memo to restore the state.

[Python design mode] 18th Chapter game role Backup--Memo mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.