当前位置: 首页 > news >正文

t想学网站建设山西太原建站怎么做

t想学网站建设,山西太原建站怎么做,专业北京网站建设公司排名,上海网站排名推广文章目录 Java 设计模式之备忘录模式概述UML代码实现 Java 设计模式之备忘录模式 概述 备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。方便对该对象恢复到原先保存的状态。 UML Originnato…

文章目录

  • Java 设计模式之备忘录模式
    • 概述
    • UML
    • 代码实现

Java 设计模式之备忘录模式

概述

  • 备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。方便对该对象恢复到原先保存的状态。

UML

在这里插入图片描述

  • Originnator:发起者类,指需要被保存状态的对象,负责创建备忘录并恢复状态。
  • Memento:备忘录类,负责存储发起者的内部状态。
  • Caretaker:管理者类,负责保存和管理备忘录,不直接操作备忘录的内容。

代码实现

定义发起者类:

public class TextEditor {private String content;public String getContent() {return content;}public void setContent(String content) {this.content = content;}// 创建备忘录public TextEditorMemento createMemento() {return new TextEditorMemento(content);}// 恢复备忘录public void restoreMemento(TextEditorMemento memento) {this.content = memento.getContent();}@Overridepublic String toString() {return "TextEditor{" +"content='" + content + '\'' +'}';}
}

定义备忘录类:

public class TextEditorMemento {private final String content;public TextEditorMemento(String content) {this.content = content;}public String getContent() {return content;}
}

定义管理类:

public class TextEditorHistory {private final Stack<TextEditorMemento> history = new Stack<>();// 保存状态public void save(TextEditor editor) {history.push(editor.createMemento());}// 撤销操作public void undo(TextEditor editor) {if (!history.isEmpty()) {editor.restoreMemento(history.pop());} else {System.out.println("没有历史数据,不能撤销");}}
}

使用:

public class Client {public static void main(String[] args) {TextEditorHistory history = new TextEditorHistory();TextEditor editor = new TextEditor();// 编辑内容并保存状态editor.setContent("one");history.save(editor);System.out.println(editor);// 编辑内容并保存状态editor.setContent("two");history.save(editor);System.out.println(editor);// 只编辑内容editor.setContent("three");// 第1次撤销操作history.undo(editor);System.out.println("第1次撤销操作后:" + editor);// 第2次撤销操作history.undo(editor);System.out.println("第2次撤销操作后:" + editor);// 第3次撤销操作history.undo(editor);System.out.println("第3次撤销操作后:" + editor);}
}

输出:

TextEditor{content='one'}
TextEditor{content='two'}
第1次撤销操作后:TextEditor{content='two'}
第2次撤销操作后:TextEditor{content='one'}
没有历史数据,不能撤销
第3次撤销操作后:TextEditor{content='one'}
http://www.dtcms.com/a/480246.html

相关文章:

  • 上海建设网站便宜的网站建设 费用高
  • 高端酒店网站模板免费下载佛山北京网站建设公司哪家好
  • 微信对接网站可以做301跳转吗连城县住房和城乡建设局 网站
  • 汉沽谁做网站找大学生做家教的网站
  • 便利的赣州网站建设凯里哪里有做网站的
  • 虚拟主机部署网站室内设计学校广州
  • wordpress建站需要学什么意思企业网站的推广建议
  • 诸城哪有做公司网站和的套用模板网站
  • 手机商城建站系统网站建设江干建设局网站
  • 网站建设公司 青岛医院 网站建设
  • 新网站建设服务公司给网站加织梦后台
  • 网页怎么做网站地图php 做资讯网站
  • 郑州微信公众号网站建设做笑话网站
  • 网站开发留言板代码公司宣传片ppt模板
  • 网站建设的相关论文设计软件排行榜
  • 南宁营销型网站建设公司哪家好南阳网站建设大旗电商
  • 国外网站域名wordpress 添加 联系我们
  • 什么叫seo网站推广药品推荐网站模板
  • Jeffreys先验:贝叶斯统计中的不变性无信息先验
  • 网站突然不收录了google推广有效果吗
  • 山西教育学会网站建设国外wordpress主机
  • 南京制作网站培训学校网站不备案可以吗
  • 建设项目环保备案登记网站网站做移动端
  • 如何建开发手机网站网站开发组织架构
  • 网站首页修改做宠物服务的相关网站
  • 上海这边敲墙拆旧做啥网站的比较多企联网站建设
  • 文山知名网站建设公司网站应包括的基本功能和高级功能
  • 做优惠券的网站搭建广告点击量多少钱一个点击
  • 专业网站建设的公司五种网络广告形式
  • 网站图标做多大好中华建筑网