[Design Patterns Learn to Apply] Memorandum Pattern

[Gewuzhizhi|Explain the profound things in simple language|Practice what you have learned]

1. Definition

The so-called memo mode is to capture the internal state of an object without breaking the closure, and save this state outside the object, so that the object can be restored to its original state in the future.

2. Grid

For the memo mode, there are several points to note:

  • Do not break the closure
  • The internal state of the object
  • Outside of the object

These three points are extracted from the definition of the memorandum pattern, but if you want to understand it more easily, you should reverse their order. as follows:

  1. Outside of the object
  2. The internal state of the object
  3. Do not break the closure

Let's analyze these points in turn.

2.1. Outside the object

This point actually requires that the data model class be decoupled from the data storage operation, and the object does not persist the data itself, so as to avoid changing the implementation of the class when the specific storage logic changes.
This point is actually a requirement of other design patterns and principles.

2.2. The internal state of the object

As we all know, in the class definition of object-oriented concept, there are private variables and public variables, and the "internal state of the object" emphasized in the memo pattern definition should refer to similar private variables. The internal state of an object is only related to the object's own behavior, so it is not open to instances of other classes.
But if you want to save the complete object in a certain state, the value of the private variable also needs to be saved.
This point is the premise of the design pattern.

2.3. Do not break the closure

This point is the essence of the design pattern. With the foreshadowing of the first two, this point is easy to understand.

Problem: Save the internal private variables of the object outside of an object.
Generally, when encountering such a problem, there are two commonly used methods:
first, violate the point one, let the object save the data itself, and add a public method to external calls;
second, violate the point two, change the class The private variables are changed to public variables, allowing external objects to directly access and save.
Seeing this, some people can't help but ask, the above two methods have violated the premise of this design pattern definition, how are they called solutions? Author Are you telling a joke?
In fact, there are countless accommodations and compromises in real programming work, and the above two methods are the portrayal of reality~ (there should be a
silly emoji here) Maybe Gof in order to prevent everyone from using these two tricks, so add The third point is emphasized-"Do not destroy the closure."
This requires us to face this seemingly paradox question: how to save the private variables of the object outside the object?

3. Know

With the analysis of the first three points, the status quo is very clear.
The internal variables of the object need to be stored outside the object, and the two cannot communicate directly, so an intermediate product must be used as an intermediary. That is, the object puts internal variables in the intermediate product, and then hands it to the outside of the object, and the object saves the intermediate product.
When data is restored, the object first reads the intermediate product, and then the intermediate product is handed over to the object, and the object restores the data in the intermediate product by itself.

In this way, the three main points in the pattern definition are fully satisfied, the closure is not broken, and the internal variables of the object are stored outside the object.
The so-called intermediate product here is the name of the model-memo.

The three roles in the memo mode are Originator, Memento, and Caretaker. These role classifications fully reflect the single responsibility principle of the design model. The initiator is responsible for business functions and provides data to the business system; the memo is only responsible for providing organized data for the storage function; the manager is responsible for the specific storage function.
In order to satisfy the closure of information, that is, part of the data is open to the initiator, but not to the manager. Normally, two interfaces are required, which are commonly referred to as a wide interface and a narrow interface. The wide interface provides complete data access so that the initiator can recover the data accordingly. The manager only needs to save the memo itself, so only a minimal narrow interface is needed.

4. Applicable

According to the definition of the memo mode, we can easily find the first scenario where the mode is applicable-1. When the internal variables of the object need to be saved outside the object.
However, the significance of design patterns is to draw inferences from one another, so their applicable extensions mostly exceed their definitions.
Even if the current application scenario is not to save the internal variables of the object, but to save the public variables of the object, from the perspective of Dimit's law, it should not be directly saved by the manager, but by the initiator. But the promoters kept it by themselves, which violated the single responsibility principle. If the manager directly saves the initiator, this function is somewhat redundant when part of the variables of the initiator needs to be saved. In this way, it is back to the memo mode-2. When some variables of the object need to be saved.

Guess you like

Origin blog.csdn.net/jhq1990/article/details/51244310