1.1 Overview
without compromising encapsulation, capture the internal state of an object and save the state outside that object so that it can be restored to its previously saved state. This is the definition of the memo pattern.
The state of an object depends on the value of its variable, and the object may have a different state at various times during the program's run. In some applications, a program may need to use a reasonable way to preserve the state of an object at a certain point in time so that the object can revert to its original saved state when needed. In memo mode, the object that needs to save the state is the "original", saying that the object responsible for saving the original state is "Memo", and that the person responsible for managing the memo is "responsible". Memo mode requires that the original person can access the details in the memo, that is, the data in the memo can be accessed to restore the status of the original, and the owner can only save and get the memo, but the data in the access memo is limited. The memo mode allows the original sender to expose their state to the memo, but other objects may be restricted in their ability to obtain the data in the memo, which ensures that the original sender exposes the internal data while guaranteeing the encapsulation of the data. In addition, a well-designed memo may only need to save some of the original sender's variables when preserving the original state, which means that the memo can restore the original state based on the data in this memo by saving the most essential data in the original state.
For example, for a game software, the game may need to go through a number of levels to finally succeed, then the game should provide the ability to save "game levels", so that the game player after a successful completion of a certain level of the game, save the current game state, when playing to the next level failed, You can choose to start the game from the last saved state, starting with the last successful level, rather than starting with the first 1 .
Structure of the 1.2 pattern
The memo pattern consists of the following three roles:
(1) The original sender (originator): An object that needs to save its state at some point. The original is responsible for creating a memo, such as using the creatememento () method to create a memo, and the original sender uses the memo to record their status. When the original person needs to revert to a state, it recovers to that moment by acquiring the corresponding data in the memo, such as the original sender calling the restorefrommemento (Memento mem) method, and using the parameters Mem Specifies the memo recovery status.
(2) Memo (Memento): An object that is responsible for storing the state of the original, the class that created the memo, and the class that created the original, in the same package, which provides access to the data in a friendly way, Makes it possible to access the data in a memo only if the original is using an instance of the class in a package.
(3) person in charge (caretaker): Responsible for managing the objects in the save memo. Manager if you do not have the same package as the original, you cannot modify or read the contents of the memo. If you need to save the memo to disk, the owner can use the object flow to write the memo to the file.
The class diagram for the memo pattern structure is as follows:
Diagram one: Class diagram of the memo pattern
1.3
Memo
advantages of the model
(1) Memo mode The use of memos can save the original person's internal state, so that only very "intimate" objects can access the data in the memo.
(2) The memorandum mode emphasizes the principle of single responsibility of class design, and separates the characterization and preservation of the state.
1.4 Fit
Use Memo
pattern of the scene
(1) An object must be saved in all or part of its state at a certain point in order to restore the previous state of the object if needed.
(2) An object does not want to allow other objects to get their internal state by means of a method that provides public permissions, such as GetXXX () .
Design Patterns Learning Notes (22: Memo mode)