The memento design pattern in Kotlin
Tags: designpatterns, kotlin
Published: 2021-01-07 14:15:55

---
title: The memento design pattern in Kotlin
published: true
date: 2021-01-07 14:08:27 UTC
tags: DesignPatterns,Kotlin
canonical_url: https://le0nidas.gr/2021/01/07/the-memento-design-pattern-in-kotlin/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/ka9nav5rdi36ap03439e.png
---

I started playing with the memento pattern for a use case I was researching when I realized that the Kotlin implementation had a, potentially, show stopper in comparison with the Java one:

> I could not use a private property from within the same file

Why was that a show stopper? We’ll see, but first, what is the memento pattern?

#### Memento pattern

This pattern is a good way to implement a functionality that helps in restoring previous states. One good example is the undo in our text editors. You can write, edit, delete and then, by hitting undo, take each action back.

There are three main ingredients for this pattern:

1. the **originator** that holds the current state and creates snapshots of itself,
2. the **memento** that, in essence, is the snapshot with perhaps some additional metadata and
3. the **caretaker** that orchestrates the backup/restore of the state

So in our example the originator is the editor which knows what the text is, the carets position etc, the memento a copy of those values and the caretaker can be the interface between the user and the editor.

#### Java implementation

Lets try to have an overly simplified version of the above example in Java:

{% gist https://gist.github.com/le0nidas/8941cb4df909bc86fcb8be98d7f3aed2 %}

Here the editor, besides manipulating text, is able to produce snapshots of its state in a way that only itself can access the state’s values. The `Memento` class might be public, in order to allow the caretaker to handle instances of it, but its fields are private and **only** the originator can read them.  
A great way to copy something while having the smallest possible API surface and maximum privacy.

As a matter of fact, here is the caretaker and its usage:

{% gist https://gist.github.com/le0nidas/069168256b5517de305494c0a524a49d %}

As you can see the `UI` uses the editor to write, edit, delete but before that it saves a backup with the editor’s state in order to restore it every time the user hits undo!

#### Kotlin implementation

So lets move originator and memento to Kotlin. `Ctrl+Alt+Shift+K` and boom.. we have a problem:

![](https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-11-39.png?w=342)

Kotlin, in contrast with Java, does not allow accessing private properties when in the same file.

What do we do? Well we can always make the properties public:

{% gist https://gist.github.com/le0nidas/7f76a1ae53f02440b8ed1d9ceae28a58 %}

but this way we, indirectly, expose the editors state:

![](https://le0nidasgr.files.wordpress.com/2021/01/screenshot-from-2021-01-07-15-20-18.png?w=320)

Another way to implement the pattern is to have `Memento` as an interface with no state for the public API and have a private implementation of it for internal usage:

{% gist https://gist.github.com/le0nidas/e5a6dc24a539d5b0427e64aa62dd0ddf %}

this way we do not expose any state but we do open a bit our API. We now have an interface that can be implemented and given to the `restore()` function.

##### Inner classes

Fortunately Kotlin has inner classes. An inner class can access the outer class’s members but, most importantly, can be extended only from within the outer class. This means that this:

{% gist https://gist.github.com/le0nidas/9f02e58faf1bc29221dc7de80554832a %}

checks all our boxes. We keep the originator’s state private and our overall API small!