Which are examples of the memento pattern?
Classic examples of the memento pattern include a pseudorandom number generator (each consumer of the PRNG serves as a caretaker who can initialize the PRNG (the originator) with the same seed (the memento) to produce an identical sequence of pseudorandom numbers) and the state in a finite state machine.
What is memento pattern in Swift?
Memento is a behavioral design pattern that allows making snapshots of an object’s state and restoring it in future. The Memento doesn’t compromise the internal structure of the object it works with, as well as data kept inside the snapshots.
Where is memento pattern used?
The memento pattern is typically used to store the historical state of an object and, therefore, allow previous states to be restored at any time. The pattern shares many similarities with the command pattern , which we explored in our previous Behavioral Design Patterns: Command Design Pattern post.
How do you implement Memento?
Check list
- Identify the roles of “caretaker” and “originator”.
- Create a Memento class and declare the originator a friend.
- Caretaker knows when to “check point” the originator.
- Originator creates a Memento and copies its state to that Memento.
- Caretaker holds on to (but cannot peek into) the Memento.
What is the best reason to use a design pattern?
Crafted by experienced object-oriented practitioners, design patterns can make your designs more flexible, more resilient to change, and easier to maintain. If you’ve encountered design patterns only in passing, in a nutshell, they’re general object-oriented solutions that you can use in your own designs.
What is a KVO?
“Keep vein open”, a medical acronym for an intravenous drip that is flowing just enough to keep the IV open for future use (sometimes written as TKO – “to keep open”)
What is the implementation of the memento pattern?
Implementation. Memento pattern uses three actor classes. Memento contains state of an object to be restored. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento. We have created classes Memento, Originator and CareTaker.
Which is an opaque object in the memento pattern?
The memento object itself is an opaque object (one which the caretaker cannot, or should not, change). When using this pattern, care should be taken if the originator may change other objects or resources—the memento pattern operates on a single object.
Can a modification of a memento be repeated?
Any modification can be undone and repeated, though. The “undo” is based on the collaboration between the Memento and Command patterns. The editor tracks a history of performed commands. Before executing any command, it makes a backup and connects it to the command object. After the execution, it pushes the executed command into history.
Who is responsible for restoring the state of a memento?
Memento contains state of an object to be restored. Originator creates and stores states in Memento objects and Caretaker object is responsible to restore object state from Memento.