1.Topics‎ > ‎Architecture‎ > ‎Patterns‎ > ‎a.Design Patterns‎ > ‎D.Behavioral Patterns‎ > ‎

Memento Pattern

  1. Summary
    1. It Memento stand for snapshot and Memento Pattern is used to implement persistency supports in objects.
    2. The intent of this pattern is to capture the internal state of an object without violating encapsulation and thus providing a mean for restoring the object into initial state when needed.
  2. Overview Tutorials 
    1. http://www.oodesign.com/memento-pattern.html
  3. Examples
    1. Saving the state of Game Objects
  4. Specific Considerations
    1. Database Transactions
      1. Transactions are operations on the database that occur in an atomic, consistent, durable, and isolated fashion. A transaction can contain multiple operations on the database; each operation can succeed or fail, however a transaction guarantees that if all operations succeed, the transaction would commit and would be final. And if any operation fails, then the transaction would fail and all operations would rollback and leave the database as if nothing has happened.
      2. This mechanism of rolling back uses the memento design pattern. Consider an object representing a database table, a transaction manager object which is responsible of performing transactions must perform operations on the table object while having the ability to undo the operation if it fails, or if any operation on any other table object fails. To be able to rollback, the transaction manager object would ask the table object for a memento before performing an operation and thus in case of failure, the memento object would be used to restore the table to its previous state.
    2. Memento and Performance
      1. Memento protects encapsulation and avoids exposing originator's internal state and implementation. It also simplifies originator code such that the originator does not need to keep track of its previous state since this is the responsibility of the CareTaker.
      2. Using the memento pattern can be expensive depending on the amount of state information that has to be stored inside the memento object. In addition the caretaker must contain additional logic to be able to manage mementos.
    3. Memento VS Command Pattern 
      1. Commands can use mementos to maintain state for undoable operations.
  5. Implementation 
    1. .Net and Java support Sterilization mechanism for   implementing memento pattern.