Applied Design Patterns with Java

Behavioral :: Memento (283) {C ch 21}


Example - Java :: Patterns\Behavioral\Memento

Pattern Concept: to allow saving data about an object, so that it can be restored later. The Memento pattern provides a means to save and restore an object's state without making each object doing the work, and yet preserving Encapsulation. The Memento pattern has privileged access to the object's that need to have their information saved. Cooper's Memento pattern example uses a Java specific facility - package access. This is the access control for a class member that is unlabeled as public, protected, or private. This unlabled access control allows another class in the SAME PACKAGE to access the class member. Some Java authorities refer to this access control mode as 'friendly access'. Note that this Memento solution is not portable to other OOP languages.


Example - UML : MemDraw
Here is Cooper's Class Diagram for the application using this logic, followed by the Rose equivalent:





The example Java program is called 'MemDraw'.

The UML diagram is above, and the list of Java files is below:

Issues and consequences of the Memento pattern include:

Catalog Behavioral Prev Next