What is Memento design pattern?

Memento design pattern is used to implement rollback feature in an object. In a Memento pattern there are three objects:

  • Originator: This is the object that has an internal state.
  • Caretaker: This is the object that can change the state of Originator. But it wants to have control over rolling back the change.
  • Memento: This is the object that Caretaker gets from Originator, before making and change. If Caretaker wants to Rollback the change it gives Memento back to Originator. Originator can use Memento to restore its own state to the original state.
  • E.g. One good use of memento is in online Forms. If we want to show to user a form pre-populated with some data, we keep this copy in memento. Now user can update the form. But at any time when user wants to reset the form, we use memento to make the form in its original pre-populated state. If user wants to just save the form we save the form and update the memento. Now onwards any new changes to the form can be rolled back to the last saved Memento object.
Read the full book at www.amazon.com
Posted in Design Pattern, Design Pattern Interview Questions

Leave a Reply

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

*