[转载http://www.java63.com/design_pattern/memento_pattern.html]
一、什么是备忘录模式
Memento模式也叫备忘录模式,是行为模式之一,它的作用是保存对象的内部状态,并在需要的时候(undo/rollback)恢复对象以前的状态。
二、备忘录模式的应用场景
如果一个对象需要保存状态并可通过undo或rollback等操作恢复到以前的状态时,可以使用Memento模式。
1)一个类需要保存它的对象的状态(相当于Originator角色)
2)设计一个类,该类只是用来保存上述对象的状态(相当于Memento角色)
3)需要的时候,Caretaker角色要求Originator返回一个Memento并加以保存
4)undo或rollback操作时,通过Caretaker保存的Memento恢复Originator对象的状态
三、备忘录模式的结构
四、备忘录模式的角色和职责
Originator(原生者)
需要被保存状态以便恢复的那个对象。
Memento(备忘录)
该对象由Originator创建,主要用来保存Originator的内部状态。
Caretaker(管理者)
负责在适当的时间保存/恢复Originator对象的状态。
Person.java
public class Person {
private String name;
private String sex;
private int age;
public Person() {
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display() {
System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
}
}
MainClass.java
public class MainClass {
public static void main(String[] args) {
Person per = new Person("lifengxing","male",30);
Person backup = new Person();
backup.setName(per.getName());
backup.setAge(per.getAge());
backup.setSex(per.getSex());
per.display();
per.setAge(20);
per.display();
per.setName(backup.getName());
per.setSex(backup.getSex());
per.setAge(backup.getAge());
per.display();
}
}
使用了备忘录模式的例子: Person.java
public class Person {
private String name;
private String sex;
private int age;
public Person() {
}
public Person(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void display() {
System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
}
public Memento createMemento() {
return new Memento(name,sex,age);
}
public void setMemento(Memento memento) {
this.name = memento.getName();
this.sex = memento.getSex();
this.age = memento.getAge();
}
}
Caretaker.java
public class Caretaker{
private Memento mem ;
public Caretaker(){
}
public void setMemento(Memento mem){
this.mem = mem;
}
public Memento getMemento(){
return this.mem;
}
}
Memento.java
public class Memento {
private String name;
private String sex;
private int age;
public Memento(String name, String sex, int age) {
this.name = name;
this.sex = sex;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
MainClass.java
public class MainClass {
public static void main(String[] args) {
Person per = new Person("lifengxing","male",24);
// Memento memento = per.createMemento();
Caretaker caretaker = new Caretaker();
caretaker.setMemento(per.createMemento());
per.display();
per.setName("beifeng");
per.setSex("Female");
per.setAge(1);
per.display();
per.setMemento(caretaker.getMemento());
per.display();
}
}