Skip to main content

Memento Design Pattern - Java

Plan

Demonstrate Memento design pattern using Java

Intent of Memento Design Pattern

Without violating encapsulation, capture and externalize an object's internal state so that the object state can be restored to this later.

Example

Lets write a program to build Undo feature of an Editor !

Most of the editors have an Undo feature which allows a user to undo the current changes to a document and get back to previously saved versions of a document.

We can use Mememto design pattern to achieve this in an Object Oriented way.

Approach

MSEditor

Let's say you have an editor MSEditor. MSEditor provides an interface to the user to perform operations like write content, change font type, change font size, save current state, restore previous state, and get content type, font type and font size. It implements Editor interface in order to hide the internal details of how the editor saves or restores the state.

Editor

This the interface implemented by the MSEditor class. We use this interface so that we can hide all the internal details from the end user.

Example Sudo Code:

interface Editor {
setContent()
setFontSize()
setFontType()
save()
restore()
getContentType()
getFontSize()
getFontType()
}

EditorState

This is a container for editor's state. It is an implemenation of State interface. EditorState hides the details by implementing State interface.

State

This is the interface implemented by EditorState. Note that there is only one method in this interface. EditorState will have other methods to store the state of editor but they are hidden by implementing State interface.

Example:

interface State {
getMetadata()
}

History

This is the place where the MSEditor stores the State of an editor. History class uses a List data structure of type State to store the states. Note that the type of objects stored inside History is State but not EditorState. This is an important point to be noted in Memento design pattern. This way, history class will have access to the methods in State interface only and it will not have access to the details of EditorState.

Sequence Diagram

Which Object Oriented Principles are in action ?

  • MSEditor abstracts the details of how the save() and restore() operations work. User does not see the implementation details. User just uses these operations to get the work done
  • Internal details of the state are hidden from History by using State interface as the type when storing EditorState. This way, history object can not tamper the state

How to run the program ?

Clone the github repository

  • git clone git@github.com:smarigowda/MementoDesignPatternJava.git

Open the project in IntelliJ and run the main program

Notes

  • Memento's ( which is EditorState in our case) are shielded from other clients ( which is History in our case) other than the Originator ( which is MSEditor in our case)
  • Simplifies Originator's code
  • This pattern may be expensive in terms of memory usage
  • Caretaker (History in our case) should track the Originator's lifecycle to be able to destroy obselete mementos