What is a Memento Design Pattern and why do we need them?

Gautam Anand
3 min readSep 13, 2023

--

Photo by Laura Ockel on Unsplash

This tutorial will teach you about the Memento Design Pattern which is Behavioural Design Pattern and how to use it. First, we’ll go over some theories. Then, we’ll create an example to demonstrate how to use the pattern.

The Memento Design Pattern provides a method for implementing undoable actions.

When to Use it

The Memento Design Pattern is typically used in situations where some actions are irreversible, necessitating a rollback to a previous state. However, if the Originator’s state is heavy, using the Memento Design Pattern can result in an expensive creation process and increased memory usage.

Advantages of Using it

It offers a simple method for keeping track of an object’s life cycle history and preserves the state of the object while maintaining encapsulation. Without compromising the object’s encapsulation, you can create snapshots of its state.

How it works

This pattern’s classes and objects are as follows:

Memento

  • The internal state of the Originator object is saved. The memento may store as much or as little of the originator’s internal state as the originator deems necessary.
  • Safeguard against access by objects other than the originator Mementos have two interfaces. The Caretaker sees a limited interface to the Memento, and can only pass the memento to the other objects. Originator, on the other hand, sees a broad interface that allows it to access all of the data required to restore itself to its previous state. Ideally, only the person who creates the memento should be able to access the memento’s internal state.

Originator

  • Makes a memento with a snapshot of its current internal state.
  • Uses the memento to return to its original state.

Caretaker

  • Is in charge of the memento’s safekeeping.
  • Never operates on or examines a memento’s contents.

Checkout below code for example:-

class Memento(val savedTime: String?)
internal class Clock {
private var time: String? = null
fun set(time: String) {
println("Time set to $time")
this.time = time
}

fun saveToMemento(): Memento {
println("Saving time to Memento")
return Memento(time)
}

fun restoreFromMemento(memento: Memento) {
time = memento.savedTime
println("Time restored from Memento: $time .")
}

}

object Design {
@JvmStatic
fun main(args: Array<String>) {
val savedTimes: MutableList<Clock.Memento> = ArrayList()
val clock = Clock()

//record the alarms
clock.set("6 am")
savedTimes.add(clock.saveToMemento())
clock.set("9 am")
savedTimes.add(clock.saveToMemento())
clock.set("12 pm")
savedTimes.add(clock.saveToMemento())
clock.set("6 pm")
clock.restoreFromMemento(savedTimes[0])
}
}

Verify the output.

Time set to 6 am.
Saving time to Memento
Time set to 9 am.
Saving time to Memento
Time set to 12 pm.
Saving time to Memento
Time set to 6 pm.
Time restored from Memento: 6 am.

Conclusion

You should have the Memento Pattern in your programming toolkit since it is a strong design pattern. This method is used to build “Ok” and “Cancel” AlertDialog boxes. The status of the activity/fragment is saved when the dialog loads, allowing you to interact with it. The activity/ fragment returns to its original state if you click Cancel.

I would like to express my gratitude to Rohit Chandekar for sharing ideas and valuable feedback for this article.

Hope you have enjoyed this article! See you next time!

Thanks for reading!

#Memento #Design Pattern #Android #Kotlin

--

--