Memento design pattern
Memento pattern is a behavioral design pattern. To return an object’s state to a prior state, the memento pattern is utilized. You might wish to preserve checkpoints as your program develops so that you can subsequently restore to those checkpoints. Memento design pattern has three design components.
- Originator — What we need to maintain the state.
- Memento — The component that will preserve the originator’s state
- Caretaker — Who keep the track of the originator
When we are implementing this, originator coupled with memento and the pass state to the caretaker. Whenever we need to go to previous state we talk to the caretaker and we check what is the previous state and move.
Benefits of using memento design pattern
- This design pattern offers a recovery mechanism for any failure.
- It offers a simple method for keeping track of an object’s life cycle history.
- Without sacrificing encapsulation, it stores the state of the object.
- Without compromising the object’s encapsulation, it creates snapshots of the object’s state.