Where is memento pattern used?

Where is memento pattern used?

The memento pattern is typically used to store the historical state of an object and, therefore, allow previous states to be restored at any time. The pattern shares many similarities with the command pattern , which we explored in our previous Behavioral Design Patterns: Command Design Pattern post.

What is memento design pattern in Java?

Memento is a behavioral design pattern that allows making snapshots of an object’s state and restoring it in future. The Memento doesn’t compromise the internal structure of the object it works with, as well as data kept inside the snapshots.

Who is responsible for storing mementos in the memento design pattern?

Caretaker is the helper class that is responsible for storing and restoring the Originator’s state through Memento object. A Caretaker object keeps and holds the Mementos, but never modify the Mementos.

What are the distinct roles defined by the Memento pattern?

The Memento design pattern defines three distinct roles: Originator – the object that knows how to save itself. Caretaker – the object that knows why and when the Originator needs to save and restore itself. Memento – the lock box that is written and read by the Originator, and shepherded by the Caretaker.

How is memento design pattern implemented in Java?

Memento design pattern is implemented with two objects – Originator and Caretaker. Originator is the object whose state needs to be saved and restored and it uses an inner class to save the state of Object. The inner class is called Memento and it’s private, so that it can’t be accessed from other objects.

Which is the best example of memento design?

One of the best real life example is the text editors where we can save it’s data anytime and use undo to restore it to previous saved state. We will implement the same feature and provide a utility where we can write and save contents to a File anytime and we can restore it to last saved state.

Who is responsible for restoring the state of a memento?

Memento contains state of an object to be restored. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento.

How are originator and caretaker used in Memento?

Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento. We have created classes Memento, Originator and CareTaker. MementoPatternDemo, our demo class, will use CareTaker and Originator objects to show restoration of object states.