Definition of memo pattern

Definition: To capture the internal state of an object and store the state outside of the object without breaking encapsulation. The object can then be restored to its original saved state

In layman’s terms, this is to record the current state of the class and restore it when needed

The class diagram is as follows:

 

The roles are as follows:

  1. Originator Originator: records internal status at the current time, defines states that belong to the backup scope, and creates and restores memo data
  2. Memento Role: Stores the internal state of the initiator object and provides the internal state required by the initiator when needed
  3. Cartetaker The role of Memo administrator: Manages, saves, and provides memos.

Sponsor role code:

 

Memo role code:

 

Memo Administrator role code:

 

Scenario:

 

The application of the memo pattern

Usage scenarios of the memo mode:

  1. Status-related scenarios in which data needs to be saved and restored
  2. Provide a rollback operation
  3. In the duplicate scenario to be monitored. For example, to monitor the properties of an object, but monitoring should not be called as the main business of the system, even if the monitoring is not correct, it will not affect much, so the general practice is to back up an object in the main thread
  4. The transaction management of database connections is the memo pattern

Notes for the memo mode:

  1. The life of a memo. Memos are created to be used in “recent” code, actively manage their life cycle, created to be used, deleted references immediately if they don’t apply, and wait for their garbage collection
  2. Note Performance: Do not use this mode in scenarios that frequently create backups. The reasons are as follows: First, you cannot control the number of objects that create memos. Second, the establishment of large objects is to consume resources, system performance needs to be considered.

Extensions to the memo pattern

1. Clone memos

Clone a copy of the current object and restore it when needed. This eliminates the need for the memo object and the need for the memo management role.

 

Procedures have been streamlined.

2. Multi-state memo mode

When there are many states of a class, it is not a good idea to write the states one by one, which requires a lot of code and is prone to errors.

Using Clone mode can be solved, the following uses data technology to solve, to achieve a JavaBean object of all the state backup and restore

All attributes of a JavaBean object are saved into a HashMap through a utility class.

Sponsor role code:

 

Utility class code:

 

Of course, many tools are already available, such as Commons, Apache’s toolset

Memo code:

 

The code for the memo administrator role remains unchanged.

So, no matter how many states there are, that’s fine

3. Backup memos

Sometimes multiple backups are required. Let’s start with a term, checkpoint, which is a stamp made during a backup. A system-level backup is usually a timestamp. It’s usually a meaningful string.

We just need to modify the general code of the memo administrator, the code is as follows:

 

In this case, be aware of the memory overflow problem. It is dangerous to assume that a backup is loaded into memory as soon as it is generated without any destruction.

4. Encapsulate it better

Sometimes, we need to make sure that the backup can’t be tampered with, that no one else has access to the memo, but only the originator.

We set the memo as the internal class of the originator, so that the methods of the memo are private, so that only the originator can call the method of the memo, then the memo manager how to get the memo? Just have the memo implement an external empty interface.

The sponsor code is as follows:

 

The built-in classes are all private and not accessible to anyone except the originator, and the association with other classes is implemented through a public interface

Empty interface for memo:

 

Memo manager:

 

Here, we use a new design method: double interface design, our class can realize multiple interfaces, in the system design, if the security of the object is considered, we can provide two interfaces, one is the normal business interface, to achieve the necessary business logic, called the wide interface; The other is an empty interface with no methods, which is intended to provide access to modules outside the subsystem, such as container objects. This is called a narrow interface, which is relatively safe because it does not provide any methods to manipulate data


Do not use temporary tables in the database as cache backup data. Although it is a simple solution, but it increases the frequency of database operations, put the pressure on the database, the best solution is to use the memo mode.