Skip to content

Instantly share code, notes, and snippets.

@le0nidas
Created January 7, 2021 13:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save le0nidas/e5a6dc24a539d5b0427e64aa62dd0ddf to your computer and use it in GitHub Desktop.
Save le0nidas/e5a6dc24a539d5b0427e64aa62dd0ddf to your computer and use it in GitHub Desktop.
class Editor {
// ...
fun backup(): Memento {
return ActualMemento(text, caretPosition)
}
fun restore(memento: Memento) {
if (memento !is ActualMemento) return
text.clear()
text.addAll(memento.text)
caretPosition = memento.caretPosition
}
// ...
interface Memento
private class ActualMemento(text: List<String>, caretPosition: Int) : Memento {
val text: List<String>
val caretPosition: Int
init {
this.text = ArrayList(text)
this.caretPosition = caretPosition
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment