Table of Contents Memento is interesting. It's one of my favorite movies.
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 ...
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.
-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
Implement a sample program that feels like creating save data for a game.
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")
}
}
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)
}
}
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)
}
}
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()
}
}
[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