Memo mode of design mode (C++)

Author: Zhai Tianbao Steven
Copyright statement: The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source

1. What is the memo mode?

       The memento pattern is a behavioral software design pattern that obtains the internal state of an object without breaking the encapsulation, saves the state outside the object, and restores it when the object needs to return to the state.

       Advantages of the memo pattern:

  1. Good encapsulation. The internal state of the initiator object is saved in the memo, which can only be read by itself, which plays a shielding role for other objects.
  2. A state restoration mechanism is provided. Similar to game save file loading.
  3. Simplified sponsor responsibilities. The storage and acquisition of the initiator state are separated.

      Disadvantages of memento pattern:

  1. The resource consumption is large, and the storage of different internal states of the initiator object will lead to an increase in overhead.

2. Memo mode

2.1 Structure diagram

       The client is the main function, and the progress of the game is stored in the memo, which is managed by the memo management class.

2.2 Code example

       Scene description: After playing the game, the progress is saved, and it can be loaded for replay later.

//Memento.h
/****************************************************/
#pragma once
#include <iostream>
#include <unordered_map>
#include <vector>
#include <list>
#include <string>

using namespace std;

// 备忘录类-游戏进度
class Memento 
{
public:
	// 构造函数
	Memento(string state) : m_state(state) {}

	// 获取状态
	std::string getState() const{ 
		return m_state;
	}

private:
	std::string m_state;
};

// 发起类-游戏
class Game 
{
public:
	// 设置状态
	void setState(string state) { 
		m_state = state;
	}

	// 获取状态
	string getState() { 
		return m_state;
	}

	// 保存状态至备忘录
	Memento saveStateToMemento() { 
		return Memento(m_state); 
	}

	// 从备忘录获取状态
	void getStateFromMemento(const Memento& memento) { 
		m_state = memento.getState(); 
	}

private:
	std::string m_state;
};

// 备忘录管理类-进度管理
class CareTaker 
{
public:
	// 添加备忘录
	void addMemento(const Memento& memento) { 
		m_mementos.push_back(memento);
	}

	// 获取备忘录
	Memento getMemento(int index) { 
		return m_mementos[index];
	}

private:
	std::vector<Memento> m_mementos;
};
//main.cpp
/****************************************************/
#include <iostream>
#include <string>
#include "Memento.h"

using namespace std;

int main() 
{
	Game game;
	CareTaker careTaker;
	// 通关
	game.setState("进度:第一关通过");
	game.setState("进度:第二关通过");
	// 保存进度,进度被管理系统管理
	careTaker.addMemento(game.saveStateToMemento());
	// 继续通关
	game.setState("进度:第三关通过");
	// 保存进度,进度被管理系统管理
	careTaker.addMemento(game.saveStateToMemento());
	// 继续通关
	game.setState("进度:第四关通过");
	// 当前进度
	cout << "当前" << game.getState() << endl;
	// 获取首个进度
	game.getStateFromMemento(careTaker.getMemento(0));
	cout << "1)" << game.getState() << endl;
	// 获取第二个进度
	game.getStateFromMemento(careTaker.getMemento(1));
	cout << "2)" << game.getState() << endl;

	return 0;
}

       The program results are as follows.

       After passing the second level, the game saves the progress, which is progress 1; after passing the third level, the progress is saved again, which is progress 2; after passing the fourth level, if you want to replay progress 1, you load progress 1, at this time the progress It becomes the state that just passed the second level; the loading progress is the same as the second level.

3. Summary

       I try my best to express my understanding of the memo mode with more common words and intuitive code routines. There may be some things that are not thoughtful. If you have different opinions, welcome to communicate in the comment area! I hope my example can help you understand the memento pattern better.

       If the article helps you, you can give me a like to let me know, I will be very happy ~ come on!

Guess you like

Origin blog.csdn.net/zhaitianbao/article/details/130235205