- Summary
- It Memento stand for snapshot and Memento Pattern is used
to implement persistency supports in objects.
- 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.
- Overview Tutorials
- http://www.oodesign.com/memento-pattern.html
- Examples
- Saving the state of Game Objects
- Specific Considerations
- Database Transactions
- 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.
- 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.
- Memento and Performance
- 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.
- 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.
- Memento VS Command Pattern
- Commands can use
mementos to maintain state for undoable operations.
- Implementation
- .Net and Java support Sterilization mechanism for implementing memento pattern.
|
|