I. Introduction to memo mode

1. What is the mediator model

Memento Pattern is a kind of behavioral design Pattern, which is defined as: Without violating encapsulation , Capture and externalize an object internal state to that the object can be restored to this state later. Capture the internal state of an object and save the state outside of the object so that the object can be restored to its original saved state later. In simple terms, the memo mode is to create a backup of the target object and provide the corresponding restore method, for our operation to provide a regret medicine.

2. Service scenarios

Memo mode is a simple and practical design mode. In fact, memo mode has many applications in daily life, such as the Moonlight box used by Zhizunbao in The Westward Journey, or the Save/Load method in the game. I wonder if you like playing video games in front of the screen? Many games have a Save feature, and many players use Save/Load to deal with many of the game’s difficulties and details in order to achieve a perfect finish. The way the archive is read and saved here is a typical memo pattern. When we click Save, we Save the game state to disk so that we can Load the saved state when we need it.So today we will implement a game Save/Load function through simple code.

Second, the realization of memo mode

1. Design idea

The first step is to create a simple game flow:The second step is to simply analyze how many objects exist in the process.

  1. Game Object (GAME) : Responsible for recording the properties and behavior of the game itself
  2. Backup objects (memento) : Responsible for recording the state of the game between points
  3. Backup management object (caretaker) : caretaker maintains a backup of the game

Next, look at the code implementation:

2. Code implementation of memorandum mode

1. Create game objects:

public class Game {

	/ / life value
	private Long hp = 1000l;
	/ / mana
	private Long mp = 500l;
	// Game progress
	private Integer level = 1;

	public void gameStart(a) {
		System.out.println("Game on!");
	}

	public void run(a) {
		// The game proceeds to reduce HP and increase MP level
		hp -= 300l;
		mp -= 100l;
		level += 1;
		if (hp <= 0) {
			System.out.println("HP below 0, game over!");
		} else{ getState(); }}// Get the state of the game
	public void getState(a) {
		System.out.println("-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -");
		System.out.println("Now the character Hp is :" + hp);
		System.out.println("At this time the character Mp is :" + mp);
		System.out.println("Game progress at this point :" + level);
	}

	public void save(Memento memento) {
		memento.save(this);
	}

	public void load(Memento memento) {
		System.out.println("Read game progress");
		Game game = memento.load();
		this.hp = game.hp;
		this.mp = game.mp;
		this.level = game.level;
		getState();
	}

	public Long getHp(a) {
		return hp;
	}

	public void setHp(Long hp) {
		this.hp = hp;
	}

	public Long getMp(a) {
		return mp;
	}

	public void setMp(Long mp) {
		this.mp = mp;
	}

	public Integer getLevel(a) {
		return level;
	}

	public void setLevel(Integer level) {
		this.level = level; }}Copy the code

2. Create game backup objects

public class Memento {
	private Game game = new Game();

	public void save(Game game) {
		this.game.setHp(game.getHp());
		this.game.setMp(game.getMp());
		this.game.setLevel(game.getLevel());
	}

	public Game load(a) {
		// TODO Auto-generated method stub
		returngame; }}Copy the code

3. Create a backup manager

public class Catetaker {

	private Memento memento;

	public void setMemento(Memento memento) {
		this.memento = memento;
	}

	public Memento getMemento(a) {
		return this.memento; }}Copy the code

The test code

public class Client {
	public static void main(String[] args) {
		Game game = new Game();
		Catetaker catetaker = new Catetaker();
		catetaker.setMemento(newMemento()); game.gameStart(); game.run(); game.save(catetaker.getMemento()); game.run(); game.load(catetaker.getMemento()); }}Copy the code

Test results: At this point, a simple save/load model of the game is done.

3. Class diagram design of memo pattern

  1. Originator: Records the current status and behavior of an object.
  2. Menento(memo role) : Responsible for storing the internal state of the initiator and providing restoration methods.
  3. Caretaker: Stores and manages the Caretaker role.

Iii. Summary of memo model

1. Characteristics of the memo mode

Advantages:

  1. Provides the user with a mechanism to recover state, often used with command mode.
  2. Information encapsulation is realized, and users do not need to care about the details of information.

disadvantages

  1. Consuming resources. If too many objects need to be backed up, additional resources are wasted.

2. Usage scenarios of the memo mode

  1. The collocation command mode provides undo instructions
  2. Scenarios where data needs to be saved/restored

3. Develop

(1) Prototype mode with use memo

We can use prototype mode to copy objects faster when we backup them.

(2) Use of multi-state memos

Some students may wonder that the Caretaker object is not necessary in the example above. In fact, the Caretaker object is mainly used to manage the backup of the multi-state object. If an object can create multiple backups in different states at the same time, the Caretaker object is used for management and maintenance.

Four, conclusion

Today’s proxy mode explanation is all over, the related code of the design mode has been included in Gitee, you can take it directly if you need it:

Gitee.com/xiaolong-ob…

If you have any questions, you are welcome to leave a message in the comment section or send a private message to the blogger, who will answer them for you in the first time. The code word is not easy, feel the harvest of friends remember to pay attention to the blogger, do not be white whoring monster oh ~ blogger here wish you can be promoted in the New Year, to the peak of life!