iOS Design Patterns - Part 7: Memento | Ray Wenderlich

In this video you'll learn about the memento pattern which allows an object's state to be saved and restored later.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3816-ios-design-patterns/lessons/7
1 Like

Thank you for this video.


I think trying to save 2 items with memento pattern maybe confuse at the start but gives 2 examples for the students.
Save and retrieve the User it’s the easier example.

I suppose that Care Taker, cares only to “tag” the data and gives them to the originator. This tag probably is the “key” that help us to create “versions” of the same object.

I keep that in my mind
Saving

  • Object needs to follow Encodable Protocol
  • Care takers accepts Encodable objects, encode it, tag with a key and pass the pair to originator
  • Originator saves the (encoded data, tag) pair.

Restore

  • Care taker takes from originator the encoded data for a specific tag
  • Care taker decode the data to the proper object

So Care taker must know how to tag, and match tags with objects in order to save and restore.
The objects need to be encodable, in order to be acceptable from the care taker.
The originator storage the data from the encoded object and the tag from the care taker as pair.

A very nice pattern.

1 Like