Memento.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. /*
  3. * C++ Design Patterns: MEMENTO
  4. * http://www.plantuml.com/plantuml/uml/NO_Dxjem4CJlVegzqN_RDEeLHH6YuWXKXPxH9fc2Mtz4wmqIglf0VOw-M9Ce0OGljdupE-DT9MNHpZkJBsYxsMvsnpqjSjEVsd5Az8K5ohyWzDjGVpBwAlRAYjDUxDa6rYWqAGU-eZbjuH4qJknxQE8B-eOdz6Skci2z9Yah7lAZxSKnl3zGWau1xsmWJyivxsqor09Mp9wt8VXY6n1ygpVoy1Ka6VDOztqsHvZffYBBdlvXJ8WAuaIxpsD9KPL57LsKfSGRkxNhK4wZHJuAPP5NvUmztbei1q1hNFXgex0RjGE7H3uszeU5a5iWrDocph7Q6AX1ZsBr4_fnT2C8pZQf3FpVNsKTB3LSPxFQKXixeHRYeIeO9noJ82btSazC_JNEVZ883GsBcLN_whpx3m00
  5. *
  6. */
  7. #include <iostream>
  8. class Memento {
  9. private:
  10. friend class Originator;
  11. Memento(int s) : private_state(s) {}
  12. void setState(int s) {
  13. private_state = s;
  14. }
  15. int getState() {
  16. return private_state;
  17. }
  18. private:
  19. int private_state;
  20. };
  21. class Originator {
  22. public:
  23. void setState(int s) {
  24. private_state = s;
  25. }
  26. int getState() {
  27. return private_state;
  28. }
  29. void setMemento(Memento* const m) {
  30. private_state = m->getState();
  31. }
  32. Memento* createMemento() {
  33. return new Memento(private_state);
  34. }
  35. int getMementoState(Memento* const m) {
  36. return m->getState();
  37. }
  38. private:
  39. int private_state;
  40. };
  41. class CareTaker {
  42. public:
  43. CareTaker(Originator* const o) : private_originator(o) {}
  44. ~CareTaker() {}
  45. void save() {
  46. std::cout << "Save state: " << private_originator->getState() << std::endl;
  47. Undo_Memento=Saved_Memento;
  48. Redo_Memento=Saved_Memento;
  49. Saved_Memento=(private_originator->createMemento());
  50. }
  51. void printSavedStates() {
  52. std::cout << "Saved states: " ;
  53. if (Saved_Memento==0) {
  54. std::cout << "it's empty" << std::endl;
  55. } else {
  56. std::cout << "saved state " << private_originator->getMementoState(Saved_Memento) ;
  57. std::cout << " undo state " << private_originator->getMementoState(Undo_Memento) ;
  58. std::cout << " redo state " << private_originator->getMementoState(Redo_Memento) << std::endl;
  59. }
  60. }
  61. void undo() {
  62. if (Undo_Memento==0) {
  63. std::cout << "Unable to undo state." << std::endl;
  64. return;
  65. }
  66. if (private_originator->getMementoState(Saved_Memento)==private_originator->getMementoState(Undo_Memento)) {
  67. std::cout << "Unable to undo state." << std::endl;
  68. return;
  69. }
  70. Memento* m = Undo_Memento;
  71. Redo_Memento = Saved_Memento;
  72. Saved_Memento = Undo_Memento;
  73. private_originator->setMemento(m);
  74. std::cout << "Perform Undo : " << private_originator->getState() << std::endl;
  75. }
  76. void redo() {
  77. if (Redo_Memento==0) {
  78. std::cout << "Unable to redo state." << std::endl;
  79. return;
  80. }
  81. Memento* m = Redo_Memento;
  82. Saved_Memento=Redo_Memento;
  83. private_originator->setMemento(m);
  84. std::cout << "Perform Redo : " << private_originator->getState() << std::endl;
  85. }
  86. private:
  87. Originator* private_originator;
  88. Memento* Saved_Memento; // for saved
  89. Memento* Undo_Memento; // for undo
  90. Memento* Redo_Memento; // for redo
  91. };
  92. int main() {
  93. Originator *originator = new Originator();
  94. CareTaker *careTaker = new CareTaker( originator);
  95. originator->setState(0);
  96. careTaker->save();
  97. originator->setState(1);
  98. careTaker->save();
  99. originator->setState(2);
  100. careTaker->save();
  101. careTaker->printSavedStates();
  102. careTaker->undo();
  103. careTaker->printSavedStates();
  104. careTaker->redo();
  105. careTaker->printSavedStates();
  106. careTaker->undo();
  107. careTaker->undo();
  108. careTaker->undo();
  109. careTaker->printSavedStates();
  110. }