Memento Pattern
Without violating encapsulation, capture and externalize an object's internal state so that the object can be restored to this state later
Applicability
- A snapshot of an object's state must be saved so that it can be restored to that state later
- A direct interface to obtaining the state would expose implementation details and break the object's encapsulation
Let's take a simple example of text area where content is modified overtime. We would like to store the change and restore its state back at any point in time. First of all, let's define a originator in the diagram as below
Next, we want to implement the Memento struct
And finally the implementation of caretaker that get/set memento to its internal storage
And there we have it the Memento Implementation in Golang. This pattern is mostly useful for the application that require the traverse between different modified state such as text editor, user actions, et cetera
P/s: The accompany code is included in this PR 😊
Happy coding 😎