21备忘录模式Memento

一、什么是备忘录模式

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 {
 2     //姓名
 3     private String name;
 4     //性别
 5     private String sex;
 6     //年龄
 7     private int age;
 8     
 9     public Person() {
10         
11     }
12     
13     public Person(String name, String sex, int age) {
14         this.name = name;
15         this.sex = sex;
16         this.age = age;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getSex() {
28         return sex;
29     }
30 
31     public void setSex(String sex) {
32         this.sex = sex;
33     }
34 
35     public int getAge() {
36         return age;
37     }
38 
39     public void setAge(int age) {
40         this.age = age;
41     }
42     
43     public void display() {
44         System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
45     }
46 }
 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Person per = new Person("lifengxing","男",30);
 4         
 5         //保存内部状态(备份)
 6         Person backup = new Person();
 7         backup.setName(per.getName());
 8         backup.setAge(per.getAge());
 9         backup.setSex(per.getSex());
10         
11         per.display();
12         
13         //修改
14         per.setAge(20);
15         per.display();
16         
17         //回滚 还原
18         per.setName(backup.getName());
19         per.setSex(backup.getSex());
20         per.setAge(backup.getAge());
21         
22         per.display();
23         
24     }
25 }

=======================================================

使用备忘录模式

 1 public class Person {
 2     //姓名
 3     private String name;
 4     //性别
 5     private String sex;
 6     //年龄
 7     private int age;
 8     
 9     public Person() {
10         
11     }
12     
13     public Person(String name, String sex, int age) {
14         this.name = name;
15         this.sex = sex;
16         this.age = age;
17     }
18 
19     public String getName() {
20         return name;
21     }
22 
23     public void setName(String name) {
24         this.name = name;
25     }
26 
27     public String getSex() {
28         return sex;
29     }
30 
31     public void setSex(String sex) {
32         this.sex = sex;
33     }
34 
35     public int getAge() {
36         return age;
37     }
38 
39     public void setAge(int age) {
40         this.age = age;
41     }
42     
43     public void display() {
44         System.out.println("name:" + name + ",sex:" + sex + ",age:" + age);
45     }
46     
47     //创建一个备份
48     public Memento createMemento() {
49         return new Memento(name,sex,age);
50     }
51     
52     //恢复备份,还原
53     public void setMemento(Memento memento) {
54         this.name = memento.getName();
55         this.sex = memento.getSex();
56         this.age =  memento.getAge();
57     }
58 }

备份

 1 //备份
 2 public class Memento {
 3     // 姓名
 4     private String name;
 5     // 性别
 6     private String sex;
 7     // 年龄
 8     private int age;
 9     
10     public Memento(String name, String sex, int age) {
11         this.name = name;
12         this.sex = sex;
13         this.age = age;
14     }
15 
16     public String getName() {
17         return name;
18     }
19 
20     public void setName(String name) {
21         this.name = name;
22     }
23 
24     public String getSex() {
25         return sex;
26     }
27 
28     public void setSex(String sex) {
29         this.sex = sex;
30     }
31 
32     public int getAge() {
33         return age;
34     }
35 
36     public void setAge(int age) {
37         this.age = age;
38     }
39 }

看守人

 1 //看守人
 2 public class Caretaker {
 3     private Memento memento;
 4 
 5     public Memento getMemento() {
 6         return memento;
 7     }
 8 
 9     public void setMemento(Memento memento) {
10         this.memento = memento;
11     }
12 }

测试

 1 public class MainClass {
 2     public static void main(String[] args) {
 3         Person per = new Person("lifengxing","男",24);
 4         
 5 //        Memento memento = per.createMemento();
 6         Caretaker caretaker = new Caretaker();
 7         caretaker.setMemento(per.createMemento());
 8         
 9         per.display();
10         
11         per.setName("beifeng");
12         per.setSex("女");
13         per.setAge(1);
14         
15         per.display();
16         
17         per.setMemento(caretaker.getMemento());
18         per.display();
19         
20     }
21 }
原文地址:https://www.cnblogs.com/justdoitba/p/9034885.html