Preliminary Research on Design Mode-Memorandum Mode

Source: Internet
Author: User

MEMENTO, also known as Token, captures the internal state of an object without compromising encapsulation and saves the State outside the object, to restore the object to the previously saved state as needed. The memorandum mode is suitable for the revocation and redo functions in the software. It is very common in word processing software, image editing software, and database management software. In games, you can also use the memo mode to save the game status, such as the status before the game fails and the status when the game is paused, this makes the game more human-friendly, in case the player continues or recovers from the pause state before the death again.

I. Application scenarios

1. Save all or part of the state of an object at a certain time point to restore it to this state when necessary.

2. prevent other objects from directly operating on the internal state of objects through interfaces, avoid exposing the Implementation Details of objects and undermining object encapsulation.

Ii. UML diagram


Iii. Java implementation

Package study. patterns. memento;/*** the memo mode is suitable for Redo and Undo ). * A memorandum is a special object. Only the master has the control power over it, and the owner is only responsible for management. * Other classes cannot access the memorandum, therefore, we need to encapsulate the memorandum. * To encapsulate the memorandum object, you need to control the call of the Memorandum. For the primary device, * It can call all information of the memorandum, allow the primary server to access all data required to return to the previous state. * for the owner, only the memorandum is saved and transmitted to other objects. * for other objects, you only need to retrieve the memorandum object from the owner and restore the state of the original object, without caring about the saving details of the memorandum. * Ideally, only the primary machine that generates the memo can access the internal status of the memo * @ author qbg */public class MementoPattern {public static void main (String [] args) {Caretaker taker = new Caretaker (); Originator or = new Originator (); or. setState ("status 1"); display (or); // save status 1taker. setMemento (or. createMemento (); or. setState ("state 2"); display (or); System. out. println ("recovering from the memo .... "); or. restoredFromMemento (taker. getMemento (); display (or);} public static voi D display (Originator or) {System. out. println ("Current status:" + or. getState () ;}}/*** Er: A common class. You can create a memo and store its current internal status, * You can also use memos to restore the internal state. Generally, classes that need to save the internal state are designed as the primary machine. */class Originator {private String state;/*** create a memorandum based on the current master */public Memento createMemento () {return new Memento (this );} /*** restore status from the memo */public void restoredFromMemento (Memento m) {this. state = m. getState () ;}public String getState () {return state;} public vo Id setState (String state) {this. state = state ;}}/*** Memorandum: stores the internal status of the primary machine and determines which internal states are saved based on the primary machine. * The memorandum design can generally refer to the design of the primary generator and determine the attributes in the memorandum class according to actual needs. * Note that, except for the primary and responsible humans, the memorandum object cannot be used directly by other classes. * do not allow other classes to modify the state in the memorandum. * Encapsulation is implemented by defining the Memorandum and the primary device at the same package level. */Class Memento {private String state; public Memento (Originator or) {this. state = or. getState () ;}public String getState () {return state;} public void setState (String state) {this. state = state ;}}/*** owner: the owner, also known as the manager, is responsible for saving the memorandum, but cannot perform operations or checks on the content of the memorandum. * One or more memorandum objects can be stored in the person in charge of humans. It is only responsible for storing objects, but cannot modify objects, and does not need to know the Implementation Details of objects. */Class Caretaker {private Memento memento; public Memento getMemento () {return memento;} public void setMemento (Memento memento) {this. memento = memento ;}}
Running result:

Current status: Status 1 Current status: status 2 recovering from the Memorandum... Current status: Status 1
Iv. Advantages and Disadvantages

Advantages:

1. Maintain the encapsulation boundary. Memos can avoid exposing information that is only managed by the primary but must be stored outside the primary (either in a certain or some temporary state of the primary ). By using memos, the Originator internal information is shielded from other objects to maintain the encapsulation boundary.

2. simplified the primary machine. The memorandum mode simplifies the design and implementation of the primary object by encapsulating different internal state versions of the primary object into the corresponding Memorandum and handing over the storage of these memorandum objects to the owner.

3. Provides a State recovery implementation mechanism, allowing you to easily return to a specific historical step. When a new status is invalid or an error occurs, you can use a temporarily stored memo to restore the status.

Disadvantages:

1. In some cases, the use of the memorandum may be costly. If the primary machine must copy and store a large amount of information when generating a memorandum, or the customer frequently creates a memorandum and restores the state of the primary machine, this may cause a very high overhead, in these cases, the memorandum mode is not suitable.
2. potential costs of maintaining the memorandum. Because the system does not know the number of maintenance memos, a large number of memorandum objects may be stored. This can be solved by limiting the number of system storage memorandum objects. In addition, the system should always prevent other objects from modifying the memorandum status and increase the complexity of the system design.

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.