Memento Design Pattern in Java

By | July 8, 2020

In this post, We will talk and learn about the Memento Design Pattern in Java.

Key Points About Memento Design Pattern :

  • Memento design pattern falls under behavioral design pattern.
  • This pattern is mainly used when we want to save the state of an object so that we can restore later on.
  • Memento pattern is used to implement this in such a way that the saved state data of the object is not accessible outside of the object, this protects the integrity of saved state data.
  • Memento pattern mainly uses three actor Classes:
    • Memento: It contains the state of an object to be restored.
    • Originator: It creates and stores states in Memento objects and
    •  Caretaker: This object is responsible to restore object state from Memento.

Memento Design Pattern in JDK:

Now Let’s move towards the implementation of the Memento Design Pattern.

Below is the complete source code:

Memento Design Pattern

CareTaker.java

 

Memento.java

 

Originator.java

 

ClientTest.java

Output of this Program:

Current State is: State – 4
First saved State was: State – 2
Second saved State was: State – 3

You May Also Like:

That’s all about the Memento Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.