0

How can one differentiate between State and Momento design pattern, whereas both are used for State Preservation..?

  • 1
    `memento` is all about exposing **private state in a immutable opaque object** ( bold part is the most important ). *Undo* is just one narrow use case that it can be used in. –  Aug 28 '17 at 22:28
  • 1
    `state pattern` is about an object's **behavior changing** when its **private internal state** changes. This has nothing to do with *state preservation* and is a pure behavioral pattern. –  Aug 28 '17 at 22:31

1 Answers1

0

Memento Pattern

Imagine a program that should transmit a file over TCP/IP with FTP. Well, you are not sure that the remote server is available. You will probably implement a proxy between the class that send the message and the class that ask for the file to be sent. If this proxy find out that the remote server is unavailable for the moment, it could possibly ask a keeper to keep it's state in a dedicated memento list without violating the encapsulation (login, password, message, etc). Later, the keeper, running in another thread, could possibly check the list of mementos to be sent and rebirth the proxy(s) that will send the message(s) (or try to). You can adapt this to the publish / subscribe pattern, email sending, etc.

State Pattern

Imagine you are writing a game with some kind of mutant superhero. In a certain condition you need to be a human. You use the concrete state needed at that time. Now, imagine you need to become that beast you can be to save the lady you love. Then you get back to the state dedicated to this situation. But, at anytime, you use the same leg, foot, or whatever object reference to accomplish your duty.

Alex C.
  • 166
  • 3
  • 9