18. Memorandum mode of design mode

Memorandum mode of design mode

table of Contents

Memorandum mode of design mode

1. Basic introduction

Two, UML diagram

3. Notes and details of the memo mode


1. Basic introduction

1) Memento Pattern captures the internal state of an object without destroying the encapsulation and saves this state outside the object. In this way, the object can be restored to its original state in the future

2) The memo mode can be understood here: a memo in real life is used to record something to be done, or to record a common opinion that has been reached, in case it is forgotten. At the software level, the memo mode has the same meaning. The memo object is mainly used to record a certain state of an object, or some data. When a rollback is required, the original data can be obtained from the memo object for recovery operations.

3) The memo mode is a behavioral mode

Two, UML diagram

Description of the schematic diagram-namely (role and responsibilities of the memorandum mode)

1) originator: object (object that needs to save state)

2) Memento: Memento object, responsible for keeping good records, that is, the internal state of Originator

3) Caretaker: Guardian object, responsible for saving multiple memo objects, using collection management to improve efficiency

4) Note: If you want to save the state of multiple originator objects at different times, you can also, you only need HashMap

3. Notes and details of the memo mode

1) Provide users with a mechanism for restoring the state, allowing users to easily return to a certain historical state

2) The encapsulation of information is realized, so that users do not need to care about the details of state preservation

3) If there are too many member variables of the class, it will inevitably take up relatively large resources, and each save will consume a certain amount of memory, this needs attention

4) Applicable application scenarios: 1. Regret medicine. 2. Save when playing games. 3. ctri + z in Windows. 4. Back in IE. 4. Transaction management of the database

5) In order to save memory, the memo mode can be used in conjunction with the prototype mode

Guess you like

Origin blog.csdn.net/qq_45072383/article/details/114242119