Patterns: behavior · 3 MIN READ

Memento: retain a restorable snapshot

Let the owner define what a valid snapshot contains.

Let the owner define what a valid snapshot contains. Memento stores state so its owner can restore a previous version without exposing editable internals to the caretaker. Trace the worked Go example, then compare the benefit with the cost before applying this design to your own project.

Behavioral pattern · A MAP OF THE IDEA

  1. Originator creates an owned snapshot
  2. Caretaker retains opaque history
  3. Originator validates and restores

Useful when

  • Local undo needs previous object state
  • History should not depend on internal field details

Watch out

  • History can retain substantial memory
  • Concurrent edits need a restore conflict policy

Implementation & operation cost: Medium

Fit depends on the task

Small tool or CLI4/5
Domain-rich application3/5
Independent distributed services2/5

Editorial suitability, not benchmarks or a universal ranking. Styles describe different axes and can be combined.

01

The design decision

Memento stores state so its owner can restore a previous version without exposing editable internals to the caretaker. In Go, use unexported snapshot fields and copy mutable collections. A history manager should not need to understand every invariant inside the edited object.

02

A complete Go example

Read this program from the entry point and follow the calls shown in the diagram. The output is included at the end so you can check the behavior without a compiler.

go
package main

import (
	"fmt"
	"slices"
)

type Snapshot struct{ lines []string }
type Document struct{ lines []string }

func (d Document) Save() Snapshot      { return Snapshot{lines: slices.Clone(d.lines)} }
func (d *Document) Restore(s Snapshot) { d.lines = slices.Clone(s.lines) }

func main() {
	d := Document{lines: []string{"first"}}
	s := d.Save()
	d.lines[0] = "changed"
	d.Restore(s)
	fmt.Println(d.lines[0])
}

// Output:
// first
Read the code

The snapshot copies lines on save and restore, preventing later edits from changing the saved history. The originator owns both operations. Across package boundaries the unexported fields keep the caretaker from modifying snapshot internals directly.

03

Tradeoffs and the simpler alternative

Large histories need memory limits, deltas or durable versioning. Snapshots of external resources cannot rewind the outside world. For simple value types an ordinary copy may be sufficient; for concurrent editing a stale restore needs a conflict policy.

TAKE THIS WITH YOU

Lets the owner restore previously captured state. Large histories need memory limits, deltas or durable versioning. Snapshots of external resources cannot rewind the outside world. For simple value types an ordinary copy may be sufficient; for concurrent editing a stale restore needs a conflict policy.

Check your understanding

Which statement best describes Memento: retain a restorable snapshot?

Go to the source

Progress stays in this browser. No account needed.