MEMENTO behavioral pattern - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

MEMENTO behavioral pattern

Description:

A Memento is an object that stores the snapshot of the internal state of another ... Memento's intent is to capture and externalize an object's state so that the ... – PowerPoint PPT presentation

Number of Views:62
Avg rating:3.0/5.0
Slides: 11
Provided by: lgr72
Category:

less

Transcript and Presenter's Notes

Title: MEMENTO behavioral pattern


1
MEMENTO behavioral pattern
  • LAVANYA GUNDAMARAJU

2
DEFINITION
  • A Memento is an object that stores the snapshot
    of the internal state of another object.
  • A memento is an alternate representation of
    another object, often in a format suitable for
    transmission across an external interface.

3
Why???
  • Undoable operations
  • Commands can use mementos to maintain state for
    undoable operations.
  • Reminder operations
  • A memento is a reminder of how things were.
  • State specific operations
  • A memento can be used with iterator to search
    collections for specific states.

4
What???
  • Capture and externalize
  • Memento's intent is to capture and
    externalize an object's state so that the object
    can be restored to that state at a later time.
    Its used in conjunction with Command and
    Iterator Patterns.
  • Internal state  should be available but
    not visible to other objects

5
How???
  • Public method
  • Issues
  • -Vulnerability The entire system vulnerable to
    change by external program code
  • -Security Data inside an object will no more
    be private and encapsulated from the outside
    world
  • Solution
  • Memento

6
Memento
  • Privileged classes
  • Other objects have only a more restricted access
    to the object
  • Preserve their encapsulation
  • Roles for objects

7
Contd
  • The Originator is the object whose state we want
    to save.
  • The Memento is another object that saves the
    state of the Originator.
  • The Caretaker manages the timing of saving the
    state, saves the Memento, and, if needed, uses
    the Memento to restore the state of the
    Originator.

8
Description
  • Memento is supposed to have two interfaces, a
    private one accessible only to the Originator and
    a public one accessible to the Caretaker.
  • Memento gives the Originator a wide interface
    and the Caretaker a narrow interface.
  • C Friend functions(Originator to be a friend
    of memento)
  • Java Inner Classes (memento inner class of
    originator)

9
  • public class Originator
  • private int number
  • private File file null
  • // create a Memento
  • public Memento getMemento()
  • return new Memento(this)
  • // restore the state of the originator
  • public void setMemento(Memento m)
  • number m.number
  • file m.file
  • //state that can be used for read/write from/to
    disk
  • private class Memento implements
    java.io.Serializable private int number
  • private File file null
  • // private constructor - only the originator
    can create
  • private Memento( Originator o)
  • number o.number
  • file o.file

10
Questions
  • ? ? ?
  • ?
  • ? ?
  • ?
  • ? ?
  • ?
Write a Comment
User Comments (0)
About PowerShow.com