The Memento Pattern: Saving State Without Breaking Encapsulation
From ArjanCodes ยท ยท 15 min

The player loads from YouTube only when you click. It uses YouTube's privacy-enhanced mode.
If the video does not play, the owner may have turned off embedding. Watch it on YouTube instead.
Description
๐จ๐ปโ๐ป Join me in Utrecht next year for an in-person Software Design Retreat! Design, discuss, and learn alongside fellow developers. Join the waitlist: https://arjancodes.com/retreat.
๐งฑ Build software that lasts. Join the Software Design Mastery program โ https://arjan.codes/mastery.
Undo sounds simple until every change needs its own reverse logic. The Memento pattern offers a different approach: save an object's state and restore it when you need to go back. In this video, I show how to implement the Memento pattern in modern Python using snapshots, dataclasses, protocols, and generics.
๐ฅ GitHub Repository: https://git.arjan.codes/2026/memento.
๐ฌ Join my Discord server: https://discord.arjan.codes.
โจ๏ธ Keyboard Iโm using: https://amzn.to/49YM97v.
๐ Chapters: 0:00 Intro 1:20 The Workflow 2:40 Take a Snapshot 6:08 Originator, Memento, and Caretaker 11:02 What Should Be in the Snapshot? 12:03 Undo and Redo 13:12 Memento vs Command 14:01 Beyond Undo 14:35 When Not to Use It 15:07 Final Thoughts
#arjancodes #softwaredesign #python