Memo (Memento): captures the internal state of an object without destroying the package, and saves the state outside the object. The object can then be restored to its previously saved state.
- Memo mode comparisons apply to classes that feature more complex features, but need to maintain or record property history, or if you need to save a property that is just a small subset of many properties, originator can revert to the previous state based on the saved memento information.
- If you need to implement the Undo function of a command when using command mode in a system, Command mode can use Memo mode to store the state of the revocable operation.
- Using memos allows you to mask complex objects ' internal information to other objects, and when the status of a character changes, it is possible that the state is not valid, and you can use a temporarily stored memo to restore the state.
After describing the definition of the memo pattern, let's look at the structure of the memo pattern:
There are three main types of roles in memo mode:
- Initiator Role : records the internal state of the current moment and is responsible for creating and recovering Memo data.
- Memo Role : Responsible for storing the internal state of the initiator object, which is provided to the initiator when the recovery is in progress.
- Manager Role : Responsible for saving memo objects.
JS Memo Mode