当前位置: 首页 > 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/574590.html

相关文章:

  • 基础数据结构之数组的双指针技巧之对撞指针(两端向中间):三数之和(LeetCode 15 中等题)
  • 网站鼠标悬停动态效果代码网站一个月
  • ASP.NET网站建设实战网络推广的方式有哪些?
  • 防止缓存穿透
  • 防火墙培训
  • 李宏毅机器学习笔记40
  • 网站开发甲方需求文档华为手机网络营销方式
  • 许昌旅游网站建设现状室内设计师联盟网站
  • 做公众号排版的网站常见的网络营销形式有哪些
  • AXI总线专题-AXI转SPI(接口)
  • 甘肃兴华建设集团网站怎么搭建一个视频网站
  • 珠海手机网站建设公司wordpress 数据库 备份
  • wordpress可以卸载360网站seo手机优化软件
  • 南山区住房和建设局网站莱芜都市网二手车租车
  • 中国建设银行网站查询密码是什么意思网站子页面怎么做的
  • 企业手机网站建设方案怎么自己建设一个网站
  • 集群网络技术2:流量控制与拥塞管理PFC ECN/DCQCN
  • 制作类网站哪里做网络推广
  • Spring Boot中使用Quartz实现动态定时任务
  • 2.4 python装饰器在 Web 框架和测试中的实战应用
  • 从容器化到自动化:Spring Boot 项目 Docker 部署与 GitLab CI/CD 集成 Harbor 全流程
  • 基于springboot的web的音乐网站开发与设计
  • AIIData数据中台商业版+开源版双模式
  • 音画同步革命:IndexTTS2深度解析——B站开源的情感化+时长可控TTS新标杆
  • 如果做淘宝网站wordpress 七牛视频播放
  • 成都模版网站制作网站建设项目结构分析
  • Transformer原理与过程详解
  • 迷你主机做网站c语言开发网站
  • 水利建设相关网站百度手机版下载
  • Nestjs框架: 微服务注册中心架构设计与Consul实战