Welcome to follow the official wechat account: FSA Full stack action 👋

I. Introduction of memo mode

The Memento Pattern, also called the snapshot Pattern, belongs to the behavior Pattern. To capture the internal state of an object without breaking the closure, saving some state of the object so that the object can be restored at an appropriate time. The way the memo pattern is implemented requires that the saved object state cannot be accessed externally by the object

  • The core of

    • Originator Originator: Records the current internal state and is responsible for creating and restoring Memento data, allowing all data required to return to the previous state. The Originator can determine which internal state Memento stores as required
    • Memento Memento: Is responsible for storing the internal state of the Originator Originator object and providing the internal state required by the Originator when needed.
    • Caretaker manager: manages, stores, and serves the Caretaker, only passing the Caretaker to other roles
  • Application scenarios

    • When you play the game, there will always be a save function, and the next time you log in, you can pick up where you left off
    • Board game, database transaction rollback
    • When an object’s internal state needs to be recorded, it can be restored to its original state to allow the user to cancel uncertain or incorrect operations
    • Provide a rollback action, such as CTRL + Z, browser back button
  • advantages

    • This provides a mechanism for the user to recover state
    • Information encapsulation is realized, so that users do not need to care about the details of state preservation
  • disadvantages

    • More resources are consumed, and memory is consumed with each save

Second, memorandum mode code implementation

Take the “King of Boxing” combat game as an example, a game character has the initial health and attack, with each punch, the health drops, while the attack increases, combined with the snapshot cheat, each time the low health, restore to the original saved state.

The creator (aka the game character) :

/** * Initiator: a game character **@author GitLqr
 */
public class RoleOriginator {

	// Health is reduced
	private int live = 100;
	// Damage increases
	private int attack = 50;

	public void display(a) {
		System.out.println("Health:" + live);
		System.out.println("Attack power:" + attack);
	}

	/** * Brawl (+ 2 damage per fight, + 10 hit points per fight) */
	public void fight(a) {
		live -= 10;
		attack += 2;
	}

	/** * Save snapshot, store current status */
	public RoleStateMemento saveState(a) {
		return new RoleStateMemento(live, attack);
	}

	/** * restore */
	public void recoveryState(RoleStateMemento memento) {
		this.live = memento.getLive();
		this.attack = memento.getAttack(); }}Copy the code

Creating a snapshot:

/** * Memo (snapshot) : snapshot of role status **@author GitLqr
 */
public class RoleStateMemento {

	/ / life value
	private int live;
	/ / damage
	private int attack;

	public RoleStateMemento(int live, int attack) {
		super(a);this.live = live;
		this.attack = attack;
	}

	public int getLive(a) {
		return live;
	}

	public void setLive(int live) {
		this.live = live;
	}

	public int getAttack(a) {
		return attack;
	}

	public void setAttack(int attack) {
		this.attack = attack; }}Copy the code

Create manager:

/** * Manager: role status snapshot manager **@author GitLqr
 */
public class RoleStateCaretaker {

	// Depending on development needs, collections can be used to store previous snapshot information
	private RoleStateMemento memento;

	public RoleStateMemento getMemento(a) {
		return memento;
	}

	public void setMemento(RoleStateMemento memento) {
		this.memento = memento; }}Copy the code

Use:

public static void main(String[] args) {
    RoleOriginator roleOriginator = new RoleOriginator();
    roleOriginator.display();

    System.out.println("Save current state snapshot");
    RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
    roleStateCaretaker.setMemento(roleOriginator.saveState());

    roleOriginator.fight();
    roleOriginator.fight();
    roleOriginator.fight();
    roleOriginator.display();

    System.out.println("State snapshot before restore");
    roleOriginator.recoveryState(roleStateCaretaker.getMemento());
    roleOriginator.display();
}
Copy the code

If this article is helpful to you, please click on my wechat official number: FSA Full Stack Action, which will be the biggest incentive for me. The public account not only has Android technology, but also iOS, Python and other articles, which may have some skills you want to know about oh ~