Memento
Memento Pattern holds a state of an object so that it can be restored at the appropriate time.The memo mode is behavioral.
introduce
Intention: Capture the internal state of an object without destroying its encapsulation and save the state outside the object.
Main Solution: The so-called memo mode is to capture the internal state of an object without destroying the encapsulation and save the state outside the object so that the object can be restored to its original state in the future.
When to use: Many times we always need to record the internal state of an object in order to allow the user to cancel uncertain or incorrect operations and return to his original state so that he has "regret medicine" to eat.
How to fix this: A memo class is used to store object states exclusively.
Key Code: Customer is not coupled to the memo class, but to the memo management class.
Application examples: 1. Regret medicine.2. Archives when playing games.3. ctri + z in Windows.4. Backward in IE.4. Transaction management of database.
Advantages: 1. Provides a mechanism for users to restore state, which enables users to return to a historical state more easily.2. Encapsulation of information is implemented so that users do not need to care about the details of state preservation.
Disadvantages: Consuming resources.If a class has too many member variables, it will consume a large amount of resources, and each save will consume a certain amount of memory.
Use scenarios: 1. Relevant state scenarios where data needs to be saved/restored.2. Provide a rollback operation.
Notes: 1. In order to comply with the Dimit principle, a class for managing memos should also be added.2. To save memory, use prototype mode + memo mode.
#include <windows.h> #include <iostream> using namespace std; // Memo (information to be saved) class Memento { public: Memento(){} Memento(int iLifeValue,int iAttack,int iDefense) :m_iLifeValue(iLifeValue),m_iAttack(iAttack), m_iDefense(iDefense) { } // Set Life Value void SetLifeValue(int iValue) { m_iLifeValue = iValue; } int GetLifeValue() { return m_iLifeValue; } // Set Attack void SetAttack(int iAttack) { m_iAttack = iAttack; } int GetAttack() { return m_iAttack; } // Set up defense void SetDefense(int iDefense) { m_iDefense = iDefense; } // Get Defense int GetDefense() { return m_iDefense; } private: int m_iLifeValue; // Value of Life int m_iAttack; // Aggressivity int m_iDefense; // Defense }; // Game Roles class PlayerRole { public: PlayerRole(int iLifeValue, int iAttack, int iDefense) :m_iLifeValue(iLifeValue), m_iAttack(iAttack), m_iDefense(iDefense) { } public: // Save Progress Memento Save() { Memento mMemento; mMemento.SetLifeValue(m_iLifeValue); mMemento.SetAttack(m_iAttack); mMemento.SetDefense(m_iDefense); return mMemento; } // Load Progress void Load(Memento &mMemento) { m_iLifeValue = mMemento.GetLifeValue(); m_iAttack = mMemento.GetAttack(); m_iDefense = mMemento.GetDefense(); } // Player Information Display void Show() { cout <<" Value of Life: " << m_iLifeValue << endl; cout <<" Aggressivity: " << m_iAttack << endl; cout <<" Blood Volume: " << m_iDefense << endl; } // Player Lost void PlayerDefeat() { m_iLifeValue = 0; m_iAttack = 0; m_iDefense = 0; } private: int m_iLifeValue; // Value of Life int m_iAttack; // Aggressivity int m_iDefense; // Defense }; // Game Progress Management class StateCareTaker { public: StateCareTaker() { } void Save(Memento &mMemento) { m_Memento = mMemento; } Memento Load() { return m_Memento; } private: Memento m_Memento; }; int main() { PlayerRole mPlayer(200,300,400); mPlayer.Show(); StateCareTaker mStateTaker; // Management Class mStateTaker.Save(mPlayer.Save()); mPlayer.PlayerDefeat(); mPlayer.Show(); mPlayer.Load(mStateTaker.Load()); mPlayer.Show(); system("pause"); return 0; }