Lecture 21st: Memento memorandum Mode

Source: Internet
Author: User

2006.8.29 Li Jianzhong

Object status backtracking

The object state changes without reason. How to trace/restore the state of an object at a certain point

If we want to restore the object state, we may first think of saving the object, but this will damage the object encapsulation. Because the object has operations in the state, if we keep the original object for saving the object and make a deep copy, other objects can also access the object state through the interface of this object, this is not what we want. Instead of supporting interfaces for object state access, the Memento mode is generated.

 

Motivation)

During the software build process, the state of some objects may be required to be traced back to the state at a certain point before the object. If you use some public interfaces to make other objects get the object state, the detailed implementation of the object will be exposed.

How can the object state be well preserved and restored? But at the same time, it does not destroy the encapsulation of the object itself.

 

Intent)

Capture the internal state of an object without damaging encapsulation,And save the status outside the object.(If you do not have this key point, you can solve the problem by using deep copy ). In this way, the object can be restored to the previously saved state.

-- Design Pattern GoF

 

For example, Memento Application

The memorandum mode is suitable for objects with frequent changes and recovery needs.

This design method can solve some problems, but it uses an rSaved object to operate the Rectangle object in addition to the State of the Rectangle object. In terms of the object's responsibility, rSaved can be changed with the rSaved field, but the rSave object is only used to save the object's status, and we should not allow changes to the rSaved content. Even if we do not change the content of rSaved, but the concept of pure OO requires that as long as the object provides an operation, its meaning indicates that this operation is correct, this is also a single responsibility principle.

Improved code

The Rectangle class does not require the Clone operation. We add a RectangleMomento class.

In an external program, the rSaved instance of the RectangleMomento type cannot be implemented because it is an internal method. Therefore, this method saves the status internally and does not provide Operations externally.

In fact, this is very similar to cloning, except that the cloned object can have an interface for the object, but Memento does not. Memento only encapsulates the status and does not provide other operations.

 

Structure)

State is the state field to be stored. CreateMemento is responsible for storing the state, and SetMemento is responsible for restoring the state.

 

Key Points of Memento Mode

Memento stores the internal status of the Originator object and restores the status of the original object as needed. The Memento mode applies to "information that is managed by the primary but must be stored outside the primary ".

In Memento mode, you must prevent objects other than the primary object from accessing the memorandum object. The memorandum object has two interfaces: one is the wide interface used by the primary object and the other is the narrow interface used by other objects.

When implementing the Memento mode, you must consider the efficiency of copying object States. If the object overhead is large, you can use an incremental change (that is, only remember the changed state) to improve the Memento mode.

 

We can also implement memos using serialization. After serialization, We can temporarily save it to a database, file, in-process, out-of-process, and other places. The Session of ASP. Net actually has this shadow.

Class must be added with serializable markup. MemoryStream is a memory stream that serializes and deserializes objects. The memory stream is saved to the current process. During operations, the object is serialized; during storage, the object is deserialized. The advantage of the memory stream is that it is easy to operate, avoiding the need to write many fields for storage.

 

Differences between Memento mode and Command mode

Although both support the Undo operationCommand is the encapsulation of behaviors, and Memento is the retention of object states., Which is different in purpose. They also support different levels of Undo operations,Command is an action on the behavior sequence, and Memento is an action on the behavior state..

2010.10.23

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.