Greetings, traveler!
This article will discuss another Behavioral Design Pattern, the Memento. This pattern allows you to capture a snapshot of an object’s current state and restore it later.
Example of usage
Let’s not delay and immediately analyze an example. Imagine you have a game where your character can be saved at specific points, as is often the case in video games. Of course, after saving, things can go wrong. You may need to roll back to a previous checkpoint. In that case, you’ll get back everything you lost but also lose everything you’ve acquired for a while.
First, let’s create a protocol for our protagonist.
protocol ProtagonistProtocol {
var currentLocation: String { get set }
var weapon: String { get set }
var health: Int { get set }
}
Then, of course, we will create an implementation.
final class Protagonist: ProtagonistProtocol {
var currentLocation: String
var weapon: String
var health: Int
init(currentLocation: String, weapon: String, health: Int) {
self.currentLocation = currentLocation
self.weapon = weapon
self.health = health
}
}
Now, let’s create a checkpoint. It will save the current state of our game character during initialization. Additionally, the player can restore this state using the “quickLoad()” function. Since this function’s “protagonist” parameter is an inout parameter, it will not be constant, and any changes made to it within the function will affect the instance passed as a parameter.
final class CheckPoint: ProtagonistProtocol {
var currentLocation: String
var weapon: String
var health: Int
init(protagonist: ProtagonistProtocol) {
self.currentLocation = protagonist.currentLocation
self.weapon = protagonist.weapon
self.health = protagonist.health
}
func quickLoad(protagonist: inout ProtagonistProtocol) {
protagonist.currentLocation = currentLocation
protagonist.health = health
protagonist.weapon = weapon
}
}
Finally, let’s put our code to use.
var protagonist: ProtagonistProtocol = Protagonist(
currentLocation: "Castle",
weapon: "Sword",
health: 87
)
let checkpoint = CheckPoint(protagonist: protagonist)
protagonist.weapon = "Axe"
protagonist.currentLocation = "Dungeon"
protagonist.health = 77
dump(protagonist)
checkpoint.quickLoad(protagonist: &protagonist)
dump(protagonist)
Conclusion
Using the Memento design pattern allows you to create snapshots of objects and easily restore them to their previous state. This is useful not only for creating games but also for other situations where your module needs to maintain certain states that may be complex to recreate. With this pattern, you can achieve this goal without unnecessary complications.
After analyzing the Memento pattern, we will discuss the next Behavioral Design Pattern, the Observer. See you in the following article.
Check out other posts in the Design Patterns series:
- Visitor Design Pattern in Swift
- Themplate Method Design Pattern in Swift
- Strategy Design Pattern in Swift
- State Design Pattern in Swift
- Observer Design Pattern in Swift
- Mediator Design Pattern in Swift
- Iterator Design Pattern in Swift
- Command Design Pattern in Swift
- Chain of Responsibility Design Pattern in Swift
- Proxy Design Pattern in Swift
- FlyWeight Design Pattern in Swift
- Facade Design Pattern in Swift
- Decorator Design Pattern in Swift
- Composite Design Pattern in Swift
- Bridge Design Pattern in Swift
- Adapter Design Pattern in Swift
- Singleton Design Pattern in Swift
- Prototype Design Pattern in Swift
- Builder Design Pattern in Swift
- Abstract Factory Design Pattern in Swift
- Factory Method Design Pattern in Swift
- Design Patterns: Basics