Memento Design Pattern Implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We are going to learn about memento design pattern implementation in this article. Sometimes we have a requirement that we want to save a particular state of object which we may need afterwards and we would like it back. The pattern helps solve this problem.

 

Memento Design Pattern Implementation:

Memento pattern provides the ability to restore an object to its previous state.

Memento helps capture internal state of an object without losing encapsulation. We can store state of an object as well as restore it whenever needed later.

Generic Class Diagram:

Memento Class Diagram

Memento Class Diagram

Component involved are:

  • Memento: 
    • Used for storing internal state of an object. Here object is instance of Originator class.
    • Has relation to Originator as well as CareTaker.
  • Originator:
    • Creates Memento instance so that internal state can be stored.
    • Uses Memento to restore to previous state.
  • CareTaker: Will have relation with Memento and Originator.

Memento Pattern Example:

Consider we are playing a game in which we make a high score and we want to save that score. We want to retrieve high score even if the score is not different. How are we going to solve this problem? Let us try to think components for memento design pattern implementation. What are our pre-requisites?

  • There should be some way in which state of object can be saved.
  • We don’t want calling code to directly have access to memento object.
  • Memento object should only be created through Orignator. i.e. CareTaker in itself should not be able to perform any operations on it.

Let us design our classes based on above analysis.

  • Create interface MementoToUser which is a medium for User to access Memento. It will not have any operations defined.
  • Create interface MementoToController which is a medium for game controller to perform operations on it. i.e. we want to get score it.
  • Create a class Memento which implements both MementoToUser and MementoToController.
  • Create a parameterized constructor for Memento to take one integer parameter. This is required because we are going to create this object by passing highest score to it.
  • Implement getScore() method so that it returns highest score.
  • Create an interface Game and declare methods saveGame(), loadGame(MementoToUser memento), setHighestScore(int score) and getHighestScore()
  • Create class GameImpl which implements Game.

If you have git installed, then you can directly clone the design-patterns repository. I have kept code for all design patterns at https://github.com/theJavaGeek/design-patterns. Each branch is a different design pattern.

Go to your working directory and then follow below steps:

  1. git clone https://github.com/theJavaGeek/design-patterns.
  2. cd design-patterns
  3. git checkout memento

If you don’t have git installed, you can either directly download zip or copy paste from below.

MementoToUser.java

MementoToController.java:

Memento.java:

Game.java:

GameImpl.java:

We are all set to test memento design pattern implementation now.

Client.java:

Output:

Here is what happened:

  • We created a Game object and set highest score as 200 on line 1 and 2.
  • We created a memento and called saveGame()on line 3 from game object and set new score as 100.
  • Now we want previously set score i.e. 200.
  • So we have retrieved that score from memento object by calling loadGame(). from Game.
  • Note that we are passing a MementoToUser, which will actually retrieve score from getScore() method of MementoToController, This is possible because we made Memento to implement MementoToUser as well as MementoToController.

Example Class Diagram:

Memento Pattern Example

Memento Pattern Example

I hope this article helped understand memento design pattern implementation.

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *