Design Pattern In Use for iOS Developers: Memento pattern

Tram Ho

Sometimes we need to record the internal state of an object. This is required when performing at checkpoints and provides an undo mechanism that allows users to recover from errors. We have to store state information somewhere so we can restore objects to their previous state. But objects often encapsulate part or all of their state, making it inaccessible to other objects and not stored externally. Publicing these states will violate packaging rules, which can reduce the reliability and scalability of the application. In such cases we can think of Memento Pattern, which will help us to solve this problem.

What is Memento Pattern?

Memento is one of the Behavior Pattern. Memento is a design pattern that can save the state of an object to restore later without violating packaging rules.

The state data saved in the memento object cannot be accessed outside the saved and restored object. This protects the integrity of saved state data.

Undo (undo) or ctrl + z is one of the most used operations in a text editor (editor). Memento designs are used to perform Undo operations. This is done by saving the current state of the object whenever it changes state, from which we can restore it in case of any errors.

How to set up Memento Pattern?

Participants in the Memento form:

Originator: represents the object we want to save. It uses memento to save and restore its internal state. Caretaker: It never performs operations on the content of memento and it doesn’t even check the content. It holds the memento object and is responsible for safeguarding the object. To restore the previous state, it returns the memento object to the Originator. Memento: represents an object to store the status of Originator. It protects against the access of objects other than Originator. The Memento class provides 2 interfaces: 1 interface for Caretaker and 1 for Originator. The Caretaker interface is not allowed to allow any operation or any access to the internal state stored by memento and thus ensure packaging rules. The Originator interface allows it to access any state variable needed to restore the previous state. The Memento class is usually an inner class of Originator. So originator has access to memento’s fields, but external classes do not have access to these fields.

Benefit of Memento Pattern

  • Benefit:

Assurance of packaging rules: direct use of the object’s state can reveal inside details of the object and violate packaging principles. Simplify the Originator’s code by having Memento keep the Originator’s state and Caretaker managing Originator’s change history.

  • Some issues to consider when using Memento Pattern:

When a large number of Mementos are created, there may be memory and performance issues in the application. It is difficult to ensure that Memento’s inner state is not changed.

How to USE Memento Patter

  • Apps that need functionality need Undo / Redo: save the state of an external object and can restore / rollback later.
  • Suitable for applications that need transaction management.

iOS Case Study

Here we will try to understand and replace the methods according to Memento Pattern in iOS, persist () and recover () methods are added to the Memento protocol that give developers where to store and unarchive properties. . Each attribute name corresponds to a dictionary element key and each attribute value corresponds to the dictionary element value that matches the key.

Code implementing in the ViewController.swift class:

Share the news now

Source : Viblo