
TypeScript 4 Design Patterns and Best Practices
By :

Memento is a pattern that deals with saving and restoring an object's state across parts of the application without exposing any implementation details. You can think of it as a state management pattern that offers a simple way of storing data in a repository and then when needed, restores the previous data on demand.
There are three main components of this pattern. You have an object called the Caretaker to maintain a list of Memento objects that offer a simple interface to store and retrieve a state. The last component is the Originator object, which is the object that uses the state to perform its business logic. The Originator coordinates with the Memento object whenever it wants to save or restore its state. The two entities (Caretaker and Originator) do not depend on each other when managing this transition of a state as you abstract all the logic inside the Memento.
Let's explain in more detail when to use this pattern.