Tuesday 15, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 6 August 2017

Design Patterns - Memento Pattern

Memento Pattern

Definition

To record an object internal state without violating encapsulation and reclaim it later without knowledge of the original object. A memento is an object that stores a snapshot of the internal state of another object.

Where to use

•When letting some info in an object be available by another object.
•When you want to create snapshots of a state for an object.
•When you need undo/redo features.

Benefits

Ability to restore an object to its previous state.

Drawbacks/consequences

Care should be taken if the originator may change other objects or resources - the memento pattern operates on a single object. Using memento to store large amounts of data from Originator might be expensive if clients create and return mementos frequently.

Memento Pattern Class Diagram


In the class-diagram above:
•An instance from the Originator class is the object that knows how to save itself. Uses memento to restore itself.
•An instance from the Caretaker class is the object that knows why and when the Originator needs to save and restore itself. Never operates or examines the contents of memento
•An instance of the Memento class is the lock box that is written and read by the Originator, and shepherded by the Caretaker.

The originator is any object that has an internal state, that we want to take a snapshot of. The caretaker is going to do something to the originators state, but wants to be able to later restore the originators state. The caretaker first asks the originator for a memento object, containing the snapshot. Then it performs the sequence of operations it was going to do. To roll back to the state before the operations, it returns the memento object to the originator.


Memento Pattern Example


01.public class Originator {
02.private String state;
03./* lots of memory using private data that does not have to be
04.saved. Instead we use a small memento object. */
05.public void set(String state) {
06.System.out.println("Originator: Setting state to " + state);
07.this.state = state;
08.}
09.public Object saveToMemento() {
10.System.out.println("Originator: Saving to Memento.");
11.return new Memento(state);
12.}
13.public void restoreFromMemento(Object m) {
14.if (m instanceof Memento) {
15.Memento memento = (Memento) m;
16.state = memento.getSavedState();
17.System.out.println("Originator: State after restoring
18.from Memento: " + state);
19.}
20.}
21.}
22.import java.util.ArrayList;
23.import java.util.List;
24.public class Caretaker {
25.private List<Object> savedStates = new ArrayListList<Object>();
26.public void addMemento(Object m) {
27.savedStates.add(m);
28.}
29.public Object getMemento(int index) {
30.return savedStates.get(index);
31.}
32.}
33.public class Memento {
34.private String state;
35.public Memento(String stateToSave) {
36.state = stateToSave;
37.}
38.public String getSavedState() {
39.return state;
40.}
41.}
42. 
43.public class MementoExample {
44.public static void main(String[] args) {
45.Caretaker caretaker = new Caretaker();
46.Originator originator = new Originator();
47.originator.set("State1");
48.originator.set("State2");
49.caretaker.addMemento(originator.saveToMemento());
50.originator.set("State3");
51.caretaker.addMemento(originator.saveToMemento());
52.originator.set("State4");
53.originator.restoreFromMemento(caretaker.getMemento(1));
54.}
55.}
When MementoExample is executed the result is:
c:>Originator: Setting state to State1
c:>Originator: Setting state to State2
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State3
c:>Originator: Saving to Memento.
c:>Originator: Setting state to State4
c:>Originator: State after restoring from Memento: State3

Usage example

Often used in database transactions and undo/redo situations.

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • Design Patterns - Decorator Pattern Decorator Pattern Definition The Decorator pattern lets you attach additional responsibilities and modify an instance functionality dynamically. Decorators provide a flexible alternative to subclassing for extending functi… Read More
  • Design Patterns - Singleton Pattern Singleton Pattern Definition The Singleton pattern provides the possibility to control the number of instances (mostly one) that are allowed to be made. We also receive a global point of access to it (them). Where to use … Read More
  • Design Patterns - Composite Pattern Composite Pattern Definition The Composite pattern helps you to create tree structures of objects without the need to force clients to differentiate between branches and leaves regarding usage. The Composite pattern lets c… Read More
  • Design Patterns - Abstract Factory Pattern Abstract Factory Pattern Definition The Abstract Factory pattern is a creational pattern which is related to the Factory Method pattern, but it adds another level of abstraction. What this means is that the pattern encapsu… Read More
  • Design Patterns - Bridge Pattern Bridge Pattern Definition Decouple an abstraction or interface from its implementation so that the two can vary independently. Bridge makes a clear-cut between abstraction and implementation. Where to use •When you want t… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: Design Patterns - Memento Pattern Rating: 5 Reviewed By: eHowToNow