一、什么是备忘录模式
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对象的状态。
//示例1:
public class Person {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Person(){}
public Person(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class MainClass {
public static void main(String[] args) {
Person per = new Person("张三","男",30);
//保存内部状态
Person backup = new Person();
backup.setName(per.getName());
backup.setAge(per.getAge());
backup.setSex(per.getSex());
System.out.println(per);
//修改
per.setAge(20);
System.out.println(per);
//回滚 还原
per.setName(backup.getName());
per.setAge(backup.getAge());
per.setSex(backup.getSex());
System.out.println(per);
}
}
//Person [name=张三, sex=男, age=30]
//Person [name=张三, sex=男, age=20]
//Person [name=张三, sex=男, age=30]
//示例2:
public class Memento {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Memento(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class Person {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Person(){}
public Person(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [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.age = memento.getAge();
this.sex = memento.getSex();
}
}
public class MainClass {
public static void main(String[] args) {
Person per = new Person("张三","男",24);
Memento memento = per.createMemento();
System.out.println(per);
per.setName("李四");
per.setSex("女");
per.setAge(25);
System.out.println(per);
per.setMemento(memento);
System.out.println(per);
}
}
//Person [name=张三, sex=男, age=24]
//Person [name=李四, sex=女, age=25]
//Person [name=张三, sex=男, age=24]
//示例3:
public class Memento {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Memento(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class Memento {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Memento(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Memento [name=" + name + ", sex=" + sex + ", age=" + age + "]";
}
}
public class Person {
// 姓名
private String name;
// 性别
private String sex;
// 年龄
private int age;
public Person(){}
public Person(String name, String sex, int age) {
super();
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;
}
@Override
public String toString() {
return "Person [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.age = memento.getAge();
this.sex = memento.getSex();
}
}
public class MainClass {
public static void main(String[] args) {
Person per = new Person("张三","男",24);
// Memento memento = per.createMemento();
Caretaker caretaker = new Caretaker();
caretaker.setMemento(per.createMemento());
System.out.println(per);
per.setName("李四");
per.setSex("女");
per.setAge(25);
System.out.println(per);
per.setMemento(caretaker.getMemento());
System.out.println(per);
}
}
//Person [name=张三, sex=男, age=24]
//Person [name=李四, sex=女, age=25]
//Person [name=张三, sex=男, age=24]