1

This is the one thing I don't understand about this pattern. Why doesn't a Memento object just have a constructor and getState() method? When does it make sense to set the Memento's state after creating it? Isn't that like changing the past?

Outback
  • 542
  • 2
  • 8
  • 20
  • The Wikipedia example (http://en.wikipedia.org/wiki/Memento_pattern), has it implemented with only a constructor and a getState. – agbinfo Oct 09 '13 at 14:05
  • Hm, thanks for that, most examples and explanations I'm seeing include a setState function – Outback Oct 09 '13 at 20:36
  • Interesting point! Perhaps it's from the habit of having both get, set accessors. In which case the set should be private – Jay Wick Jan 04 '14 at 11:42

1 Answers1

0

You should not have setState method in Memento class. It makes no sense. Memento is just a token that Caretaker keeps to restore the state of Originator. If Caretaker wants a new Memento with new state information it should get it from Originator.

Here is a good example.

Prashant Kumar
  • 678
  • 6
  • 7