Memento Pattern: Saving and Restoring Object States with Encapsulation - Prof. Atif M. Mem, Study notes of Programming Languages

The memento pattern is a design pattern that enables saving and restoring the internal state of an object without violating encapsulation. This pattern is particularly useful in systems that require undo commands. In this document, we explore the concept of the memento pattern, its philosophy, and an example implementation in java.

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-o4t
koofers-user-o4t 🇺🇸

10 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CMSC 433 – Programming Language
Technologies and Paradigms
Spring 2007
Memento Pattern
Apr. 17, 2007
2
What is it?
Suppose you would like to save the internal state
of an object so you can restore it later.
Ideally, it should be possible to save and restore
this state without making the object itself take care
of this task, and without violating encapsulation.
This is the purpose of the Memento pattern.
3
Preliminary Discussion
Objects frequently expose only some of their internal state
using public methods, but you would still like to be able to
save the entire state of an object because you might need to
restore it later.
In some cases, you could obtain enough information from
the public interfaces (such as the drawing position of
graphical object s) to save and restor e that data.
In other cases, the color, shading, angle and connection
relationship to other graphical ob jects need to be save d and
this information is not readily available.
This sort of information saving and restoration is common
in systems that need to support Undo commands.
4
Preliminary Discussion (contd…)
If all of the information describing an object is
available in public variables, it is not that difficult
to save them in some external store.
However, making these data public makes the
entire system vulnerable to change by external
program code, when we usually expect data inside
an object to be private and encapsulated from the
outside world.
pf3
pf4
pf5

Partial preview of the text

Download Memento Pattern: Saving and Restoring Object States with Encapsulation - Prof. Atif M. Mem and more Study notes Programming Languages in PDF only on Docsity!

1

CMSC 433 – Programming Language

Technologies and Paradigms

Memento PatternSpring 2007

Apr. 17, 2007

What is it?

of an object so you can restore it later.Suppose you would like to save the internal state

of this task, and without violating encapsulation.this state without making the object itself take careIdeally, it should be possible to save and restore

This is the purpose of the Memento pattern.

3

Preliminary Discussion

restore it later.save the entire state of an object because you might need tousing public methods, but you would still like to be able toObjects frequently expose only some of their internal state graphical objects) to save and restore that data.the public interfaces (such as the drawing position ofIn some cases, you could obtain enough information from this information is not readily available.relationship to other graphical objects need to be saved andIn other cases, the color, shading, angle and connection in systems that need to support Undo commands.This sort of information saving and restoration is common

Preliminary Discussion (contd…)

to save them in some external store.available in public variables, it is not that difficultIf all of the information describing an object is

outside world.an object to be private and encapsulated from theprogram code, when we usually expect data insideentire system vulnerable to change by externalHowever, making these data public makes the

5

The Memento Philosophy

to save.having privileged access to the state of the object you wantThe Memento pattern attempts to solve this problem by

object, thus preserving their encapsulation.Other objects have only a more restricted access to the

  • This pattern defines three roles for objects: (^) 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 the saving of the state, saves of the Originator.the Memento and, if needed, uses the Memento to restore the state

In Java

languages.done with varying degrees of success in variousits variables publicly available is tricky and can beSaving the state of an object without making all of

mode.little known and infrequently used protectionIn Java, this privileged access is possible using a

7

“private_protected” mode

Suppose you have classes A and B declared in the same module (file).

Class A contains a private-protected variable (^) x .

automatically initializesIn class B in the same module, we create an instance of A, which (^) x to 5.

Class B has direct access to the variable (^) x in class A and can print it out without compilation or execution error.

“private_protected” mode

protected.Variables with no declaration are treated as private

derived classes can access protected variables.Other classes can access public variables, and

access protected or private-protected variables.However, another class in the same module can

build Memento objects.It is this last feature of Java that we can use to

13

The “Undo” Strategy

list; it can keep a list of the lastThe Mediator is an ideal place to manage the Undo action (^) n (^) operations so that they can be undone. discussed earlier.Thus, the Mediator also functions as the Caretaker object for undoing later.there is a single place where these commands can be storedand undo in such a program, a Mediator is required so thatIn fact, since there could be any number of actions to save rectangles.creating new rectangles and changing the position ofIn this program we save and undo only two actions:

Lets See Some Code!

draws each instance of the rectangles.Let’s start with our visRectangle class which actually 15

visRectangle

The Memento Class

Drawing the rectangle is pretty straightforward.

position and size variables.visRectangle.java, and thus has access to thewhich is contained in the same file,Now, let’s look at our simple Memento class,

17

The Memento Class

The Memento Class Code Explained

save.we pass it the visRectangle instance we want toWhen we create an instance of the Memento class,

itself.saves a copy of the instance of the visRectangleIt copies the size and position parameters and

therestore them to and can do it directly, as we see inthe Memento knows which instance it has toLater, when we want to restore these parameters,

restore()

method.

19

createRect

previous state of the list of drawings as an Integer on the undo list.The rest of the activity takes place in the Mediator class, where we save the

Saving the Previous Position

before moving it in a Memento.We also save the previous position of a rectangle