Memento

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/4

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

5 Terms

1
New cards

Why

To capture and encapsulate the state of an object for later restoration. Checkpoint/rollback

2
New cards

What

Uses copying object state out/in to save the state of an object

Originator - creates memento containing snapshot of current state, uses memento to restore state

memento - stores internal state of originator. Only originator may see internals

Caretaker - keeps memento, does not access or examine memento contents

3
New cards

Copy object state out

Making a new object with the same state as “this”

4
New cards

Copy object state in

Copy another object’s state to “this”

5
New cards

Code (snapshot)

// originator
class Document {
public:
   Document(vector <string> lines={}): lines_(lines){}

   void insert(int line, const string &str) {
      int index = line-1;
      if (index <= lines_.size()+1)
	 lines_.insert(lines_.begin()+index, str); 
      else
	 cout << "line out of range" << endl; 
   }

   void remove(int line) {
      int index = line-1;
      if(index < lines_.size())
	 lines_.erase(lines_.begin()+index); 
      else
	 cout << "line out of range" << endl; 
   } 

   void show() const {
      for(int i = 0; i < lines_.size(); ++i)
	 cout << i + 1 << ". " << lines_[i] << endl; 
   }

   class Memento *checkpoint() const;  
   void rollback(const class Memento *); 

private:
   vector<string> lines_; 
}; 

class Memento {
public:
   Memento(const Document& doc): doc_(doc) {}
   const Document& getState() const {return doc_;}  
private:
   const Document doc_;
};
 
Memento *Document::checkpoint() const {
   return new Memento(*this); // copying the Document itself
}

void Document::rollback(const Memento *mem){
   *this = mem->getState();  // copying back
}

// client and caretaker
int main() {
   Document doc({
         "Lorem Ipsum is simply dummy text of the printing and typesetting",
         "industry. Lorem Ipsum has been the industry's standard dummy text",
         "ever since the 1500s, when an unknown printer took a galley of",
	 "type and scrambled it to make a type specimen book. It has",
	 "survived five centuries."});

   char option;
   
   do{
      static Memento *safeState = nullptr;

      doc.show();
      cout << endl;

     cout << "Enter option (i)nsert line, (e)rase line, (c)heckpoint, (r)ollback: ";
      cin >> option;

      int line; string str; 
      switch(option) {
      case 'i':
         cout << "line number to insert: ";
         cin >> line;
         cout << "line to insert: ";
	 cin.get();
         getline(cin, str);
         doc.insert(line, str);
         break;
      case 'e':
         cout << "line number to remove: ";
         cin >> line;
         doc.remove(line);
         break;
      case 'c':
	 safeState = doc.checkpoint();
	 break;
      case 'r':
	 if (safeState != nullptr){
	    doc.rollback(safeState);
	    delete safeState;
	    safeState = nullptr;
	 }else
	    cout << "No checkpoint created" << endl;
         break;
      }
   }while(option == 'i' || option == 'e' || option == 'c' || option == 'r');
}