Momento Pattern in kotlin | Design Patterns

Memento pattern is used to restore the 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 a 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 operations in an editor. The 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 the future to undo.
  • memento : the object that is going to maintain the state of the originator. It is just a POJO.
  • caretaker : the object that keeps track of multiple memento. Like maintaining save points.

The originator will store the state information in the memento object and retrieve old state information when it needs to backtrack. 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) {
        statesList.add(m)
    }

    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()
    caretaker.addMemento(memento)

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

The 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

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions