Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Memento

  • Be the first to comment

  • Be the first to like this

Memento

  1. 1. MEMENTO PATTERN
  2. 2. INTENT • Is to capture the internal state of an object without violating encapsulation and thus providing a man for restoring the object into initial state when needed.
  3. 3. UML CLASS DIAGRAM
  4. 4. • Memento • Stores internal state of the Originator object. • It must have two interfaces, one for the caretaker – this interface must not allow nay operation or any access to internal state stored by the memento and thus honors encapsulation. The other interface is to the originator and allows the originator to access any state variables necessary to for the originator to restore previous state. • Originator • Creates a memento object capturing the originators internal state • Uses the memento object to restore its previous state • Caretaker • Responsible for keeping the memento • The memento is opaque to the caretaker, and the caretaker must not operate on it.
  5. 5. APPLICABILITY AND EXAMPLES • Is used when a snapshot of an object’s state must be captured so that it can be restored to that state later and in situations where explicitly passing the state of the object would violate encapsulation. • http://www.oodesign.com/memento-pattern-calculator-example-java-sourcecode.html
  6. 6. SPECIFIC PROBLEMS AND IMPLEMENTATION • Database Transactions The mechanism of transaction rolling back use the memento design pattern. Consider an object representing a db table, a transaction manager object which is responsible of performing transactions must perform operations on the table object while having the ability undo the operation if it or any operations 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.
  7. 7. CONSEQUENCES • 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 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.
  8. 8. RULES OF THUMB • Command and Memento act as magic tokens to be passed around and invoked at a later time. In command, the token represents a request; in memento, it represents the internal state of an object at a particular time. Polymorphism is important to Command, but not to Memento since its interface is so narrow that it can only be passed as a value. • Command can use Memento to maintain the state required for an undo operation. • Memento is often used in conjunction with Iterator. An Iterator can use a Memento to capture the state of an iteration.
  9. 9. REFERENCES • http://sourcemaking.com/design_patterns/memento • http://www.oodesign.com/memento-pattern.html

×