Memo mode

introduce

  • It’s a pattern of behavior
  • Used to save the current state of the object and restore to that state later.
  • You need to ensure that the state of the stored object cannot be accessed externally, and that the internal integrity is not disclosed to the outside world

define

  • To capture the internal state of an object and save the state outside of the object in order to restore the previously saved state without breaking the closure

Usage scenarios

  • You need to save the state or part of the state of an object at a certain time

  • Using an interface to allow other objects to access these states would expose the implementation details of the object and break the encapsulation of the object. An object does not want direct access to its internal state, but can access its internal state indirectly through an intermediate object

UML

instructions

  1. Originator: Creates a memo that records and restores its internal status. Colleagues can also decide, as needed, which states Memento stores itself

  2. Mement: Memo role that stores the internal status of the Originator and prevents objects other than the Originator from accessing Memento

  3. Caretaker: stores the memo and cannot operate or access its contents. The Caretaker can only pass the memo to another object

Simple implementation

1. Memo class, used to record the game needs to record the details

Public class Memoto {public int mCheckpoint; public int mLifeValue; public String mWeapon; @Override public StringtoString() {
            return "Memoto [mCheckpoint="+mCheckpoint+
                    " mLifeValue"+mLifeValue+
                    " mWeapon"+mWeapon+"]"; }}Copy the code

2. Create a game class (Originator) to implement the basic logic of the game. It can create and obtain memos internally, but does not implement the logic of storage and recovery

/* * Originator */ public class CallOfDuty {private int mCheckpoint = 1; private int mLifeValue = 100; private String mWeapon ="Desert Eagle";

        public void play(){
            System.out.println("Play games:" + String.format("Level %d", mCheckpoint) + "In the trenches.");
            mLifeValue -=10;
            System.out.println("Progress has been upgraded.");
            mCheckpoint++;
            System.out.println("Reach"+String.format("Level %d", mCheckpoint));
        }

        public void quit(){
            System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
            System.out.println("Properties before exit:"+this.toString());
            System.out.println("Quit the game");
            System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -"); } // Create a Memoto public MemotocreateMemoto(){
            Memoto memoto = new Memoto();
            memoto.mCheckpoint = mCheckpoint;
            memoto.mLifeValue = mLifeValue;
            memoto.mWeapon = mWeapon;
            return memoto;
        }

        public void restore(Memoto memoto){
            this.mCheckpoint = memoto.mCheckpoint;
            this.mLifeValue = memoto.mLifeValue;
            this.mWeapon = memoto.mWeapon;
            System.out.println("Restored game attributes:"+this.toString());
        }

        @Override
        public String toString() {
            return "CallOfDuty [mCheckpoint="+mCheckpoint+
                    " mLifeValue"+mLifeValue+
                    " mWeapon"+mWeapon+"]"; }}Copy the code

3. The Caretaker class is created to save the game progress and restore the logic, but does not operate the actual game data

Public class Caretaker {// Memoto Memoto; Public void archive(Memoto Memoto){this.memoto = Memoto; } // Get the archive public MemotogetMemoto() {returnmemoto; }}Copy the code

4. Implementation of Main function

Public class Main {public static void Main (String[] args) {CallOfDuty CallOfDuty = new CallOfDuty(); // start the game callOfDuty. Play (); // build caretaker, save caretaker = new caretaker (); // caretaker executes the caretaker. Archive (callOfDuty. CreateMemoto ()); // Quit the game callofdue.quit (); // caretaker resumes the game and the caretaker resumes the game progress. CallOfDuty callOfDuty1 = new CallOfDuty(); callOfDuty1.restore(caretaker.getMemoto()); }}Copy the code

5. Running result

Playing video games: 1 in the fight to kill Schedule the upgrade to 2 off -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - in front of the exit attribute: CallOfDuty [mCheckpoint=2 mLifeValue90 mWeapon Desert Eagle] quit --------------------- CallOfDuty [mCheckpoint=2 mLifeValue90 mWeapon Desert Eagle]Copy the code
  • Conclusion: Instead of storing the CallOfDuty object directly, we store the data of the CallOfDuty object through Memoto, and then store the Memoto object. The access to the Memoto object is handed to the Caretaker. In the whole process, direct access to the CallOfDuty role is shielded externally, which satisfies the object state access function and keeps the structure of the module clear and neat.

expand

Android source memorandum design pattern implementation