博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
21备忘录模式Memento
阅读量:6993 次
发布时间:2019-06-27

本文共 4900 字,大约阅读时间需要 16 分钟。

一、什么是备忘录模式

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

你可能感兴趣的文章
MySQL execute dynamic sql script.
查看>>
Collection框架
查看>>
网络流(二)最大流的增广路算法
查看>>
HTTP请求、响应报文格式
查看>>
zendstudio中出现中文乱码的解决方法
查看>>
服务器端与客户端TCP连接入门(一)
查看>>
lombok使用方法
查看>>
多线程基础
查看>>
1028: C语言程序设计教程(第三版)课后习题8.2
查看>>
批量更新软连接脚本
查看>>
Linux 文件和目录的属性
查看>>
Log4j配置使用
查看>>
初步认识Hadoop
查看>>
jQuery对象扩展方法(Extend)深度解析
查看>>
9道前端技能编程题
查看>>
NOIP 2000年提高组复赛 单词接龙
查看>>
mysql-索引与优化
查看>>
sql server 2008安装需要一直重启。但重启后又没有达到效果。
查看>>
Psp个人软件开发工具
查看>>
uva 1395(kruskal变形)
查看>>