备忘录模式(java版)
来源:互联网 发布:人工智能产业链全景图 编辑:程序博客网 时间:2024/06/11 08:54
1.引入
备忘录模式的核心就是再不破坏其封装性的条件下,捕获一个对象的内部状态,然后使用备忘录保存这个状态,然后就可以随便修改了,如果想要恢复其原内部状态,那么就直接用备忘录恢复原内部状态。
2.举例
在网页设计中,我们拿到别人前端设计的网页,如果我们直接就修改,那么恢复是不可能的。所以我们可以先把别人的前端网页先另保存一份在磁盘上,然后拿着原别人的前端网页开始随意修改了,如果我们想要恢复到原网页,那么直接使用磁盘的备份就可以了。而这就是备忘录模式,其实就是三步,第一步初始化,第二步备份,第三步恢复。
3.实现
--3.1.原网页的样式类:
public class Original { //背景颜色private String backgroundColor;//字体大小 private String fontSize;//字体样式 private String fontStyle;public Original(String backgroundColor,String fontSize,String fontStyle){this.backgroundColor=backgroundColor;this.fontSize=fontSize;this.fontStyle=fontStyle;}public String getBackgroundColor() {return backgroundColor;}public void setBackgroundColor(String backgroundColor) {this.backgroundColor = backgroundColor;}public String getFontSize() {return fontSize;}public void setFontSize(String fontSize) {this.fontSize = fontSize;}public String getFontStyle() {return fontStyle;}public void setFontStyle(String fontStyle) {this.fontStyle = fontStyle;}public Memento createMemento(){return new Memento(backgroundColor,fontSize,fontStyle);}public void restoreMemento(Memento memento){this.backgroundColor=memento.getBackgroundColor();this.fontSize=memento.getFontSize();this.fontStyle=memento.getFontStyle();}}
--3.2.备份网页样式类:
public class Memento {private String backgroundColor;private String fontSize;private String fontStyle;public Memento(String backgroundColor,String fontSize,String fontStyle){this.backgroundColor=backgroundColor;this.fontSize=fontSize;this.fontStyle=fontStyle;}public String getBackgroundColor() {return backgroundColor;}public void setBackgroundColor(String backgroundColor) {this.backgroundColor = backgroundColor;}public String getFontSize() {return fontSize;}public void setFontSize(String fontSize) {this.fontSize = fontSize;}public String getFontStyle() {return fontStyle;}public void setFontStyle(String fontStyle) {this.fontStyle = fontStyle;}}
--3.3.存储备份样式类(磁盘):
public class Storage {private Memento memento;public Storage(Memento memento){this.memento=memento;}public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento = memento;}}
--3.4.测试类:
public class Test {public static void main(String[] args) {Original original =new Original("红色","24px","宋体");Storage storage=new Storage(original.createMemento());System.out.println("初始化状态:");System.out.println(original.getBackgroundColor()+","+original.getFontSize()+","+original.getFontStyle());original.setBackgroundColor("黑色");original.setFontSize("18px");original.setFontStyle("微软雅黑");System.out.println("修改后状态:");System.out.println(original.getBackgroundColor()+","+original.getFontSize()+","+original.getFontStyle());original.restoreMemento(storage.getMemento());System.out.println(original.getBackgroundColor()+"-"+original.getFontSize()+"-"+original.getFontStyle());}}
结果:
初始化状态:
红色,24px,宋体
修改后状态:
黑色,18px,微软雅黑
红色-24px-宋体
------------------------------------------------------------------------------------------------------------------
总结:
备忘录模式就是先初始化一个对象的状态,然后将这个状态保存成备份,当下次修改之后想要回到原状态,就用备份恢复就完事了。
0 0
- 备忘录模式(java版)
- 备忘录模式(Java)
- 备忘录模式(memento) Java
- java设计模式-备忘录模式(Memento)
- Java设计模式----备忘录模式(Memento)
- Java设计模式(十九)----备忘录模式
- Java备忘录模式(Memento模式)
- 备忘录模式(Memento)-----基于JAVA语言
- Java备忘录模式
- java备忘录模式
- java Memento(备忘录模式)
- java 备忘录模式
- java 备忘录模式
- java 备忘录模式
- Java备忘录模式(Memento)
- java备忘录模式
- java备忘录模式实例
- java备忘录模式
- VB程序学习代码记录20160828
- leetcode_c++:Divide and Conquer:The Skyline Problem(218)
- poj3167 Cow Patterns
- 生成学习算法之高斯判别分析模型
- Mybatis的<if>和<choose>的区别 以及“=”判断
- 备忘录模式(java版)
- 会计学
- javaFx的变量,javaFx的数据类型,javaFx的运算符
- ios学习开发0828
- HUD 1541/BIT(数状数组)
- R语言从基础入门到提高(二)Vectors(向量)
- spark val b = a.flatMap(x => 1 to x)详解
- 用户画像的技术选型与架构实现
- 283. Move Zeroes *