Undo Changes With Memento Pattern

Memento is a behavioural design pattern that lets you capture the object’s internal state without exposing its internal structure, o that the object can be returned to this state later.

The memento design pattern has three states Memento Originator and Caretaker. Originator is the one we need to maintain the state (It acts like a memory generator or memento generator). The caretaker is the one who keeps track of the originator and memento itself (It can save all these mementoes Sates inside the caretaker. Caretaker has no way to change the memento state).

Follow the steps for creating Memento

  • Create a Memento class with the required state you want to persist.
  • Create an Originator class which on save, which saves the state to a memento class.
  • Make a caretaker class that has a list of memento states and a cursor pointing to the last know state.
  • Provide methods to go forward and backwards in the state timeline with undo and redo methods for the caretaker.
  • Create an Originator and a Caretaker. Create some states and do undo and redo on demand based on the requirement.

Sample:-

--

--

--

Associate Software Engineer. | linkedin.com/in/nisalj

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Why You Should Be Using TypeScript

Redirects, and their Effect on Performance or How a (Seemingly Minor) Third Party Change Affected…

ReactJs — Facebook SDK

Should you use React Native to build your startup’s mobile app?

Tutorial ESP32 Door Lock 3in1 Fingerprint Keypad 4x3 and RFID PN532

Tutorial ESP32 Door Lock 3in1 Fingerprint Keypad 4x3 and RFID PN532

Next.js Firebase v9: Create AuthProvider with Next.js Nookies

Handle Javascript events in a Web Assembly — WebGL application

How to install React Native cli on Red Hat Enterprise Linux 8

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nisal Jayathilaka

Nisal Jayathilaka

Associate Software Engineer. | linkedin.com/in/nisalj

More from Medium

Equals() Method Vs “==” Operator in Java

Design Patterns: Singleton

Combining cloneable interface with Memento Pattern

Singleton Pattern