I. Overview
Also called Snapshot mode (Snapshot Pattern) or Token mode
Save the internal state of the object, and when (undo / rollback) needed to restore the previous state of the object.
It is intended: without violating encapsulation, capture and the internal state of an object, and save the state outside the object.
Mainly to solve: the so-called memorandum mode is, without destroying the package, to capture the internal state of an object, and save the state outside of the object so that the object can later be restored to a previously saved state.
Memo mode and a mode status: memo mode represents the state of the state model is used to represent a state class.
1.1 application scenarios
1, we need to save / restore state data related scenes. 2, there is provided a rollback operation.
(1) a class of objects need to save its state (corresponding role Originator)
(2) Design of a class is only used to store the state of the object (corresponding to Memento character)
(3) when necessary, the role of the Caretaker Originator Memento requires a return and save
time (4) undo or rollback operation, the Caretaker Memento saved by the recovery state of the object Originator
1.2, the advantages and disadvantages
Advantages: 1, to provide a mechanism to restore the state, allows users to more convenient to return to the status of a history. 2, the encapsulation information to achieve, so that the user need not be concerned about the details of the state save.
Cons: consumption of resources. If the member variable class too, is bound to take up a relatively large resource, and every time you save will consume a certain amount of memory.
1.3, class diagram roles and responsibilities
1, Originator (original poster): needs to be saved in order to restore the state of the object, and is responsible for creating a memorandum of Memento, the current time to record their own internal states, and can be used to restore internal memo states. Originator Memento can decide which store their own internal state as necessary.
2, Memento (memo): The object is created by the Originator, the state is responsible for internal storage Originator object, and the object can prevent access memo other than Originator. Memorandum has two interfaces: Caretaker can only see the memorandum of narrow interface, he can only pass the memo to other objects. Originator it can be seen memo wide interface, which allows access to return to the previous state
All data required state.
3, Caretaker (Manager): responsible for saving / restoring Originator objects at the right time of the state, responsible for the memo Memento, are unable to access the content or operation of Memento.
1.4 evolution
First, create a Person class, he is the Originator (formerly living)
/ * * Originator (native persons) * / public class the Person { / * * name, age, gender is a configuration diagram of the State, a state attribute * / // Name Private String name; // Age Private int Age; // Gender Private String Sex; public the Person () { Super (); } public the Person (String name, int Age, Sex String) { Super (); the this .name = name; the this .age = Age; the this.sex = sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex =Sex; } public void the display () { System.out.println ( "name:" + name + ", Sex:" Sex + + ", Age:" + Age); } // Create a backup public Memento createMemento () { return new new Memento (name, Age, Sex); } // restore a backup public void setMemento (Memento Memento) { the this .name = memento.getName (); the this .age = memento.getAge (); the this .sex = memento.getSex (); } }
Next, create a Memento (memo), he and the basic structure is the same as Originator
/ * * Memento (memo) * / public class Memento { // Name Private String name; // Age Private int Age; // Sex Private String Sex; public Memento () { Super (); } public Memento (String name, int Age, Sex String) { Super (); the this .name = name; the this .age = Age; the this .sex = Sex; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
Create a manager Caretaker (manager) role
/* * Caretaker(管理者) */ public class Caretaker { //持有一个对Memento的引用 private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }
test
@Test public void the Test () { the Person the Person = new new the Person ( "Bob", 18, "male" ); // print person.display (); // create a manager Caretaker Caretaker = new new Caretaker (); // create a backup // Memento Memento = person.createMemento (); caretaker.setMemento (person.createMemento ()); person.setName ( "red" ); person.setAge ( 17 ); person.setSex ( "F" ) ; // print person.display (); // backup and restore person.setMemento (caretaker.getMemento ()); // Print person.display (); }
Export
name: Hsiao Ming, sex: man, age: 18 name: small Kurenai, sex: Woman, Age: 17 name: Hsiao Ming, sex: Man, Age: 18
Second, the extension
2.1、spring
org.springframework.binding.message.StateManageableMessageContext
Transaction management database.