Momento Pattern in kotlin | Design Patterns

Memento pattern is used to restore state of an object to a previous state. As your application is progressing, you may want to save checkpoints in your application and restore back to those checkpoints later.

The purpose of the memento design pattern is to provide the ability to execute an undo action in order to restore an object to a previous state. The Memento pattern is also known as Token.

For example, when you use text editor such as vim or emacs, do you use undo? Why do you think this undo command can perform? As one way, all statuses(sometimes not all) are recorded as a type of object. Then you can recover this object to use this recovered data in your application.

Undo or backspace or ctrl+z is one of the most used operation in an editor. Memento design pattern is used to implement the undo operation. This is done by saving the current state of the object as it changes state.

One important point to not in implementing memento design pattern is, the encapsulation of the object should not be compromised.

Memento Pattern participants

  • originator : the object for which the state is to be saved. It creates the memento and uses it in future to undo.
  • memento : the object that is going to maintain the state of originator. Its just a POJO.
  • caretaker : the object that keeps track of multiple memento. Like maintaining savepoints.

The originator will store the state information in the memento object and retrieve old state information when it needs to back track. The memento just stores what the originator gives to it. Memento object is unreachable for other objects in the application.

Complete program for Kotlin Momento pattern

import kotlin.collections.ArrayList

class Memento(val state: String)

class Originator {

    //this String is just for example
    //in real world application this
    //will be the object for which the state to be stored
    var state: String? = null

    fun createMemento(): Memento {
        return Memento(state!!)

    fun setMemento(memento: Memento) {
        state = memento.state

class Caretaker {
    private val statesList = ArrayList<Memento>()

    fun addMemento(m: Memento) {

    fun getMemento(index: Int): Memento {
        return statesList.get(index)

fun main(args: Array<String>) {
    val originator = Originator()
    originator.state = "Ironman"
    var memento = originator.createMemento()
    val caretaker = Caretaker()

    originator.state = "Captain America"
    originator.state = "Hulk"
    memento = originator.createMemento()
    originator.state = "Thor"
    println("Originator Current State: " + originator.state!!)
    println("Originator restoring to previous state...")
    memento = caretaker.getMemento(1)
    println("Originator Current State: " + originator.state!!)
    println("Again restoring to previous state...")
    memento = caretaker.getMemento(0)
    println("Originator Current State: " + originator.state!!)

Output of the Momento design pattern in kotlin

Originator Current State: Thor
Originator restoring to previous state...
Originator Current State: Hulk
Again restoring to previous state...
Originator Current State: Ironman

Join My Facebook Group
Join Group

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions