21.2 memorandum model Overview
The memorandum mode provides a State recovery implementation mechanism, allowing you to easily return to a specific historical step. When a new State is invalid or has problems, you can use the currently stored memos to restore the status. Currently, many software provides undo operations, in which the memorandum mode is used.
The memorandum mode is defined as follows:
|
Memento pattern: capture the internal state of an object without damaging the encapsulation, and save the state outside the object, in this way, the object can be restored to the previously saved State later. It is an object Behavior Pattern and Its alias is token. |
At the core of the memorandum model is the memorandum category and the person in charge of human design for the Management memorandum. Its structure is as follows:
The memorandum mode structure includes the following roles:
● Originator ):It is a common class. You can create a memo and store its current internal status. You can also use the memo to restore its internal status, generally, the class that needs to save the internal status is designed as the primary machine.
● Memento (Memorandum ):Stores the internal status of the primary node and determines the internal status of the primary node. 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, apart from the primary and responsible humans, the memorandum object cannot be directly used by other classes. The design of the primary is different in different programming languages.
● Caretaker (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.
It is not difficult to understand the memorandum model, but the key lies inHow to Design memos and take charge of humans. Because the memorandum stores the intermediate state of the primary object, it is necessary to prevent objects other than the primary object from accessing the memorandum, especially those that are not allowed to modify the memorandum.
The following uses a simple example code to illustrate how to use the Java language to implement the memorandum mode:
When using the memorandum mode, there should first be a primitive class originator. In real business, the primitive class is a specific business class, which contains some attributes used to store member data, the typical code is as follows:
Package DP. memento; public class originator {private string State; public originator () {}// create a memorandum object public memento creatememento () {return new memento (this );} // restore the public void restorememento (memento m) {state = m. state;} public void setstate (string state) {This. state = State;} Public String getstate () {return this. state ;}}
For memento, memento generally provides attributes corresponding to the primary device (which can be all or partially) for storing the status of the primary device, the typical memorandum design code is as follows:
Package DP. memento; // memo class, default visibility, class memento {private string State; public memento (originator O) {state = O. getstate ();} public void setstate (string state) {This. state = State;} Public String getstate () {return this. state ;}}
When designing the memorandum category, consider itsEncapsulation,Except for the originator class, other classes are not allowed to call the constructor and related methods of the memento class.If other classes are allowed to call methods such as setstate () without considering encapsulation, The History state stored in the memo will change, the restored State through the Undo operation is no longer a real historical state, and the memorandum mode loses its meaning.
When using the Java language to implement the memorandum mode, memento classes and originator classes are generally defined in the same package to implement encapsulation, in Java, you can use the default access identifier to define the memento class, which ensures that the class is visible in the package. Only the originator class can access memento, while other classes can access memento. The originator state value is saved in memento. If the state value in originator needs to be revoked after it is changed, you can call its restoremento () method to restore it.
For the human caretaker, it is used to save the memo object and provides the getmemento () method to return a memo object to the client. The primary machine can return to a historical state by using this memo object. The typical human implementation code is as follows:
package dp.memento;public class Caretaker {private Memento memento;public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento=memento;}}
In the caretaker class, the state change method in memento should not be called directly. It is only used to store the memorandum object. Store the memo objects generated by the primary Machine backup. When the user needs to restore the primary machine, the memo objects stored in the object will be taken out.
|
|
Thoughts
Can I create a memorandum object in the prototype mode? How should the system be designed? |
|
【Author】 Liu WeiHttp://blog.csdn.net/lovelion]