Momento Pattern in kotlin | Design Patterns

Memento pattern is a behavioral design pattern. 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.

package behavioral

import java.util.ArrayList

class Memento(val savedState: String)

class Originator(var state: String? = null) {
    fun set(state: String) {
        println("Originator: Setting state to $state")

        this.state = state

    fun saveToMemento(): Memento {
        println("Originator: Saving to Memento.")

        return Memento(state!!)

    fun restoreFromMemento(memento: Memento) {
        state = memento.savedState

        println("Originator: State after restoring from Memento:  ${state!!}")

class Caretaker(val savedStates: ArrayList<Memento> = ArrayList<Memento>()) {
    fun addMemento(memento: Memento) {

    fun getMemento(index: Int): Memento = savedStates[index]

fun main(args: Array<String>) {
    val caretaker = Caretaker()

    val originator = Originator()


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

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to

or Register below

Join My Facebook Group
Join Group