I. INTRODUCTION
Learn about the memo mode in design mode today. Speaking of this model, perhaps not very familiar, but believe that when writing code, write the wrong, the function of CTRL + Z is still relatively more, such as Word,photoshop and other editing software, revocation function is essential. The memo pattern is a design pattern that is a good way to implement undo functionality. It can get our system back to the previous state, Hmm, the legendary moonlight Box!Here's a look at the definition of the memo pattern and the UML class diagram:captures the internal state of an object without compromising encapsulation, and saves the state outside that object. The object can then be restored to its previously saved state。
Two. Examples of memo patterns
When it comes to Memo mode, it's often useful in the game! The legendary SL Dafa (Soave load) has helped me to play countless games, every boss must archive is also I have always believed in the creed. This time, let's take a look at the application of the memo pattern through a simple game example.No memo mode of the situation I did not say, was the boss seconds after the "Hero please re-come" when the discovery did not archive, the consequences are not deleted the game is to drop the keyboard ...Not much to say, on the code, look at the example of the memo pattern:
Design Pattern.cpp:Defines The entry point for the console application.//#include "stdafx.h" #include <iostream> #include <vector>using namespace std;//Player Archive class, backup, equivalent to Mementoclass playermemento{//in the pattern here we use it as a friend class for the player class, Can directly access its private variable friend class Player;private:int m_hp; blood volume int m_atk; Attack int m_def; Defense Public:playermemento (int hp, int atk, int def): m_hp (HP), M_ATK (ATK), M_def (def) {}};//archive point, place where memo is stored, Equivalent to Caretakeclass checkpoint{private:vector<playermemento*> m_mementovector;public://archive void Save ( playermemento* Memento) {m_mementovector.push_back (memento);} Get archived playermemento* Load (int num) {return m_mementovector[num] According to the archive number;} The table forgot to empty the object void clear () {for (Vector<playermemento*>::iterator it = M_mementovector.begin (); it! = M_ Mementovector.end (); ++it) {delete *it;} M_mementovector.resize (0);}};/ /Player class, that is, we need to back up the class, equivalent to the memo mode inside the Originator.class player{private:int m_hp; blood volume int m_atk; Attack int m_def; Defense Public:player (int hp, int atk, int def): m_hp (HP), M_ATK (ATK), M_def (DEf) {}playermemento* Save () {cout << "to hit the boss, so nervous, save a file!" "<< Endl;return new Playermemento (M_HP, M_ATK, m_def);} void Load (playermemento* memento) {m_hp = MEMENTO->M_HP;M_ATK = Memento->m_atk;m_def = Memento->m_def;cout < < "Wow, there are archives, SL DAFA good!" "<< Endl;} void Display () {cout << current life: << m_hp << "Current attack:" << m_atk << "Current Defense:" << m_def &L t;< Endl;} Wounded, blood loss void hurt (int v) {m_hp-= v;if (m_hp <= 0) {m_hp = 0;cout << "omg! I'm hanging up! "<< Endl;}}; int _tmain (int argc, _tchar* argv[]) {//Create a Player object, and an archive point object player* player = new player (100, 200, 300); checkpoint* CheckPoint = new CheckPoint ();p layer->display ();//Every Boss must archive Checkpoint->save (Player->save ()); /received 10,000 damage, was boss seconds Player->hurt (10000);p layer->display ();//I have archived, I am capricious! Player->load (checkpoint->load (0));p layer->display (); System ("pause"); return 0;}Result: Current life: 100 Current attack: 200 Current defense: 300 to hit the boss, so nervous, save a file!
Omg! I'm hanging up!
Current life: 0 Current attacks: 200 current defenses: 300
Wow, there are archives, SL DAFA Good!
Current life: 100 Current attacks: 200 current defenses: 300
Please press any key to continue ...
We simply look at the above program, the player has a number of property fields, the game process is also related to the state attributes of the various objects in the game, certainly more complex than this. To completely restore a game process, we need to restore the object to its previous state. We set up a memo class for the player, which simply encapsulates the player's related attribute field, creates an object with the player's Save method, saves all the current player's state, and stores it in the checkpoint object. The checkpoint object has only the ability to save and get archived as needed. When we need to restore the state of the object, we only need to get the memo object from the Checkpoint object and restore the original properties according to the memo by the player's Load method.
Three. Summary of the Memorandum pattern
Finally, let's look at the advantages and disadvantages of the memo pattern and the timing of the use.
Advantages:1) provides a mechanism for object state recovery, allowing us to restore an object to its previous backup state when the object's current state is incorrect or not as expected, and it can be a partial property that does not have to be backed up with all fields.2) The memo implements the encapsulation of information, a memo object is a representation of the state of the sender object and is not altered by other code.
Disadvantages:If you store more state objects or have more state attributes, you may have a significant performance overhead.
use time:When we may need to restore the object state to some point before, we can consider using memo mode, such as undo operation, Game archive and so on.
Design Pattern Learning Notes-Memo Mode