DEV Community

Sergey Leschev
Sergey Leschev

Posted on • Updated on

💾 Memento Pattern

Memento Pattern

The memento pattern is used to capture the current state of an object and store it in such a manner that it can be restored at a later time without breaking the rules of encapsulation.



typealias Memento = [String: String]
protocol MementoConvertible {
    var memento: Memento { get }
    init?(memento: Memento)

struct GameState: MementoConvertible {

    private enum Keys {
        static let chapter = "com.valve.halflife.chapter"
        static let weapon = "com.valve.halflife.weapon"

    var chapter: String
    var weapon: String

    init(chapter: String, weapon: String) {
        self.chapter = chapter
        self.weapon = weapon

    init?(memento: Memento) {
        guard let mementoChapter = memento[Keys.chapter],
              let mementoWeapon = memento[Keys.weapon] else {
            return nil

        chapter = mementoChapter
        weapon = mementoWeapon

    var memento: Memento {
        return [ Keys.chapter: chapter, Keys.weapon: weapon ]
Enter fullscreen mode Exit fullscreen mode


enum CheckPoint {

    private static let defaults = UserDefaults.standard

    static func save(_ state: MementoConvertible, saveName: String) {
        defaults.set(state.memento, forKey: saveName)

    static func restore(saveName: String) -> Any? {
        return defaults.object(forKey: saveName)
Enter fullscreen mode Exit fullscreen mode


var gameState = GameState(chapter: "Black Mesa Inbound", weapon: "Crowbar")

gameState.chapter = "Anomalous Materials"
gameState.weapon = "Glock 17", saveName: "gameState1")

gameState.chapter = "Unforeseen Consequences"
gameState.weapon = "MP5", saveName: "gameState2")

gameState.chapter = "Office Complex"
gameState.weapon = "Crossbow", saveName: "gameState3")

if let memento = CheckPoint.restore(saveName: "gameState1") as? Memento {
    let finalState = GameState(memento: memento)
Enter fullscreen mode Exit fullscreen mode

Sources: Github

In software engineering, behavioral design patterns are design patterns that identify common communication patterns between objects and realize these patterns. By doing so, these patterns increase flexibility in carrying out this communication.

🐝 Chain Of Responsibility
👫 Command Pattern
🎶 Interpreter Pattern
🍫 Iterator Pattern
💐 Mediator Pattern
💾 Memento Pattern
👓 Observer Pattern
🐉 State Pattern
💡 Strategy Pattern
📝 Template Method
🏃 Visitor Pattern
🌰 Abstract Factory
👷 Builder Pattern
🏭 Factory Method
🔂 Monostate Pattern
🃏 Prototype Pattern
💍 Singleton
🔌 Adapter Pattern
🌉 Bridge Pattern
🌿 Composite Pattern
🍧 Decorator Pattern
🎁 Facade Pattern
🍃 Flyweight Pattern
Protection Proxy
🍬 Virtual Proxy

I have a clear focus on time-to-market and don't prioritize technical debt. And I took part in the Pre-Sale/RFX activity as a System Architect, assessment efforts for Mobile (iOS-Swift, Android-Kotlin), Frontend (React-TypeScript) and Backend (NodeJS-.NET-PHP-Kafka-SQL-NoSQL). And I also formed the work of Pre-Sale as a CTO from Opportunity to Proposal via knowledge transfer to Successful Delivery.

🛩️ #startups #management #cto #swift #typescript #database
📧 Email:
👋 LinkedIn:
👋 LeetCode:
👋 Twitter:
👋 Github:
🌎 Website:
🌎 Reddit:
🌎 Quora:
🌎 Medium:
🖨️ PDF Design Patterns: Download

Top comments (0)