[JAVA] [Gang of Four] Design pattern learning --Memento

Memento --Keep Memento

Table of Contents Memento is interesting. It's one of my favorite movies. memento.jpg

In this movie, a man who has only 10 minutes of memory due to an obstacle caused by an incident struggles to find out the mystery of the incident and the circumstances in which he is currently located. He used ink on his body to inform himself of important events that he should not forget because he only remembers 10 minutes.

Is the purpose of the pattern to save the state of the object at a certain point in time? Private members are basically inaccessible from the outside. However, I think there are many situations where you want to save the entire state of a particular object so that various software can completely return to the previous state with ctrl (or command) + z keys.

I understand that this pattern is used in such cases. It seems that there was a Command pattern in a similar pattern ...

Purpose

Instead of breaking the encapsulation, it captures the internal state of the object and externalizes it so that the object can be returned to this state later.

Component

-Memento A class that saves the state of an object -Originator An object that you want to save the state at a certain point in time. Create a memento object and use the memento object to restore it. ยท Caretaker Memento object vault

Implementation

Implement a sample program that feels like creating save data for a game.

Memento A class that saves the state of an object

Save data class Save a capture of your level, money, and experience at any point in time.

SaveData.kt


package memento

class SaveData(val level: Int, val gold: Int, val point: Int) {
    fun show() {
        println("level:$level Money:$gold XP:$point")
    }
}

Originator An object whose state you want to save at some point. Create a memento object and use the memento object to restore it.

Information currently playing

Game.kt


package memento

class Game(private var level: Int, private var gold: Int, private var point: Int) {

    fun show() {
        println("level:$level Money:$gold XP:$point")
    }

    /**
     *Defeated the enemy
     */
    fun enemyDown(enemyLevel: Int) {
        point += enemyLevel

        if (point >= 10) {
            level += point / 10
            point %= 10
        }

        gold += enemyLevel
    }

    /**
     *Get current state
     */
    fun getData(): SaveData {
        return SaveData(level, gold, point)
    }
}

Caretaker Memento object vault

MemoryCard.kt


package memento

class MemoryCard {
    private val memory = ArrayList<SaveData>()

    /**
     *Save
     */
    fun save(data: SaveData) {
        memory.add(data)
    }

    /**
     *Load (restore)
     */
    fun load(index: Int): Game {
        val data = memory[index]
        return Game(data.level, data.gold, data.point)
    }
}

People who use

Client.kt


package memento

class Client {
    init {
        //Refers to a memory card
        val memoryCard = MemoryCard()

        //Game Start
        var game = Game(1, 0, 0)
        //Defeat the enemy
        game.enemyDown(1)
        game.enemyDown(5)
        game.enemyDown(6)

        //save
        memoryCard.save(game.getData())

        //Current state
        game.show()

        //Defeat the enemy
        game.enemyDown(4)
        game.enemyDown(2)
        game.enemyDown(8)

        //save
        memoryCard.save(game.getData())

        //Current state
        game.show()

        //Return to the originally saved state because there was an item that you failed to acquire
        game = memoryCard.load(0)

        //Current state
        game.show()
    }
}

Output result

[out-put]
Level: 2 Possession: 12 Experience: 2
Level: 3 Possession: 26 Experience: 6
Level: 2 Possession: 12 Experience: 2

that's all

Recommended Posts

[Gang of Four] Design pattern learning --Memento
[Gang of Four] Design pattern learning
[Gang of Four] Design pattern learning --Singleton
[Gang of Four] Design Pattern Learning --Decorator
[Gang of Four] Design pattern learning --Visitor
[Gang of Four] Design pattern learning --Mediator
[Gang of Four] Design pattern learning --Iterator
[Gang of Four] Design pattern learning --Facade
[Gang of Four] Design pattern learning --Composite
[Gang of Four] Design pattern learning --Prototype
[Gang of Four] Design pattern learning --State
[Gang of Four] Design pattern learning --Interpreter
[Gang of Four] Design pattern learning --Builder
[Gang of Four] Design pattern learning --Bridge
[Gang of Four] Design pattern learning --Proxy
[Gang of Four] Design pattern learning --Strategy
[Gang of Four] Design pattern learning --Adapter
[Gang of Four] Design pattern learning --Observer
[Gang of Four] Design pattern learning --Command
[Gang of Four] Design pattern learning --Fly Weight
[Gang of Four] Design pattern learning --Abstract Factory
[Gang of Four] Design pattern learning --Factory Method
[Gang of Four] Design pattern learning --Chain of Responsibility
[Gang of Four] Design pattern learning --Template Method
Gang of Four (GoF) Patterns in Python
Design Pattern #Builder
Design Pattern #Adapter
Design Pattern #Decorator
Pattern recognition learning in video Part 1 Field of Pattern Recognition
Design Pattern #Observer
Design Pattern #Singleton
Design Pattern #Proxy
Learn the design pattern "Chain of Responsibility" in Python
Design Pattern #Factory Method
GoF design pattern is just an "introduction of abstraction layer"