M6 · State, Resources, Two-Axis GrowthSession 12BehavioralKotlin replaces itOutline

Intent. Capture and restore an object’s state without breaking encapsulation.

In Kotlin. An immutable data class snapshot makes this nearly free.

Save and restore state without exposing the object’s internals. Immutable snapshots make the mechanics trivial in Kotlin, so the real work is deciding what belongs in a snapshot – too much and undo is expensive, too little and it is wrong.

Structure

classDiagram
    class Originator {
        -state
        +save() Memento
        +restore(m)
    }
    class Memento {
        -state
        +getState()
    }
    class Caretaker {
        -history: List~Memento~
        +backup()
        +undo()
    }
    Originator ..> Memento : creates
    Caretaker o--> Memento : stores, cannot read
    Caretaker --> Originator