This tutorial explains Gang of Four’s Memento design pattern in Java. It starts with explaining the Memento pattern’s definition, followed by the two mandatory requirements which must be met for applying the pattern. Next, it shows the UML class diagram for Memento pattern and explains the roles of the various constituent classes of the pattern. An example use case showing Memento pattern implementation is then explained using its class diagram, Java code for implementation and detailed explained of the code.
Introduction: Memento Design Pattern is a behavioral design pattern among the Gang Of Four(GOF)Article on GOF Patterns & their types Design Patterns. Being a behavioral design pattern, the memento pattern basically deals with how the objects communicate and share responsibilities among each other.
What is Memento Design Pattern
Memento design pattern provides ability to capture(save) an object’s state and then restore back this captured state when required by the system.
Memento design pattern can be chosen when the following 2 requirements occur in conjunction –
- The state of an object in the system needs to be captured/saved so that the object can be restored to that state later.
- Direct access to object’s state is not an option as that would break the encapsulation of object by exposing its internal structure.
Its important to understand the above two points to understand the applicable scenarios where Memento Pattern can be chosen for designing the system being implemented. If the system, of which the state is being saved, does not want the state saving or restoring process to know about internal implementation details of how the state is being handled by the system then Memento is to be used. I.e. the Memento pattern moves the part of saving and restoring of the state of the system to a separate set of classes which do not care about what makes up the state or how the system uses that state. Instead classes of Memento only care about storing the state when needed and reproducing it back on demand.
This separation of concerns between the actual system state and the memento implementation allows for single responsibility principleClick to Read tutorial on Single Responsibility Principle being effectively applied. This is because the responsibility of handling the system’s state is separated from the state capture and restoring responsibility.
Class Diagram for Memento Design Pattern
Explanation of Memento Design Pattern’s Class Diagram:
- There are 3 main participants in the memento pattern’s class diagrams –
Originatoris the object of which the state is to be stored. The responsibilities of storing the snapshot of its state and then restoring the state from
Originator. This is the reason why the methods
createMementoFromState()(for storing the state) and
setStateFromMemento()(for restoring the state) have been defined on the
Mementostores the internal state of the
Originatoris allowed storing to/restoring from the
Mementoobject. This allows the internal structure/state of
Originatorto not be visible to other classes thus achieving the encapsulation requirement of a
Caretakerholds the memento object and is responsible for its safekeeping. When a snapshot of the
Originator’s state is required then the
Originatorfor the snapshot as a memento object and stores the snapshot. When the
Originator’s state is to be restored then
Mementoobject back to the
- There is also a
Stateclass which holds the state of the
Mementoalso holds this state of the
Originator. As this is simply the representation of
Originator’s internal state hence there is no separate class for
Statedepicted in the class diagram. However, in the java example’s class diagram, which is next, I have included the
Memento Design Pattern – Example Use Case in Java – Class Diagram:
PlayerStatusCaretaker.javaExplanation of Java Example’s Class Diagram & Code
The Java class diagram above depicts Memento pattern implemented for a game which needs to keep track of player points & player level as the game is being played –
PlayerStatusrepresents the state of the Originator(
CurrentPlayerStatus). The State/PlayerStatus consists of two attributes –
PlayerStatusMementois the memento object which stores an instance of
PlayerStatuswhich is nothing but a snapshot of
CurrentPlayingStatus’s internal state.
CurrentPlayerStatusis the Originator i.e the object of which we need to store/restore the internal snapshot.
CurrentPlayerStatusholds its state in two attributes
playerLevelwhich are the exact same attributes in
PlayerStatusobject which is stored in the
PlayerStatusCaretakerin the above example drives the game and stores/restores the memento with the help of Originator. It starts the game. As the game progresses and the player reaches level 2 with 1200 points,
CurrentPlayerStatus(Originator) object to create a memento object and store the current
PlayerStatusby calling its method
createMementoFromStatus(). The player then moves to 2200 points and level 4 where he,hypothetically, loses a life and goes down to the last state stored. This is the state stored in the PlayerStatusMemento object which the
PlayerStatusCaretakersets by invoking the
CurrentPlayerStatus. The gameplayer’s status is thus restored to 1200 points and level 2.
- So, to sum-up, we were able to store a snapshot of
CurrentPlayerStatusand then restore it later. This is how the Memento Design Pattern works.
In the above tutorial we looked at what is memento design pattern,its class diagram with explanation. We then looked at a Java Use Case implementing Memento pattern through its class diagram, code for the classes shown in the java based class diagram, followed by explanation of both the class diagram & code. This concludes the tutorial on Memento design pattern.