



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!




1
3
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
5
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
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
7
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.
13
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:
draws each instance of the rectangles.Let’s start with our visRectangle class which actually 15
17
19
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