首页
统计
关于
Search
1
Sealos3.0离线部署K8s集群
1,082 阅读
2
类的加载
741 阅读
3
Spring Cloud OAuth2.0
726 阅读
4
SpringBoot自动装配原理
691 阅读
5
集合不安全问题
584 阅读
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
登录
Search
标签搜索
Java
CSS
mysql
RabbitMQ
JavaScript
Redis
JVM
Mybatis-Plus
Camunda
多线程
CSS3
Python
Spring Cloud
注解和反射
Activiti
工作流
SpringBoot
Mybatis
Spring
html5
蘇阿細
累计撰写
388
篇文章
累计收到
4
条评论
首页
栏目
笔记
Java
多线程
注解和反射
JVM
JUC
设计模式
Mybatis
Spring
SpringMVC
SpringBoot
MyBatis-Plus
Elastic Search
微服务
Dubbo
Zookeeper
SpringCloud
Nacos
Sentinel
数据库
MySQL
Oracle
PostgreSQL
Redis
MongoDB
工作流
Activiti7
Camunda
消息队列
RabbitMQ
前端
HTML5
CSS
CSS3
JavaScript
jQuery
Vue2
Vue3
Linux
容器
Docker
Kubernetes
Python
页面
统计
关于
搜索到
1
篇与
的结果
2022-09-25
行为型模式-备忘录模式
(1)概述备忘录模式又叫快照模式,在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便需要时能将该对象恢复到之前保存的状态(2)结构发起人角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录的功能,它可以访问备忘录里的所有信息备忘录角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人管理者角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录没有读写权限备忘录有两个等效接口:窄接口:管理者对象(和其他除发起人对象之外的任何对象),看到的是窄接口,该接口只允许把备忘录对象传递给其他对象宽接口:发起人对象可以看到宽接口,该接口允许读取所有的数据,以便恢复对象之前的内部状态(3)案例以游戏存档为例:“白箱”备忘录备忘录角色对任何对象都提供宽接口(破坏了封装性)发起人角色public class GameRole { /** * 生命值 */ private int vit; /** * 攻击值 */ private int atk; /** * 防御值 */ private int def; public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } /** * 初始化内部状态 */ public void initState() { this.vit = 100; this.atk = 100; this.def = 100; } /** * 战斗 */ public void fight() { this.vit = 0; this.atk = 0; this.def = 0; } /** * 保存游戏角色状态 * * @return */ public RoleStateMemento saveState() { return new RoleStateMemento(vit, atk, def); } /** * 恢复角色状态 * * @param roleStateMemento */ public void recoverState(RoleStateMemento roleStateMemento) { this.vit = roleStateMemento.getVit(); this.atk = roleStateMemento.getAtk(); this.def = roleStateMemento.getDef(); } /** * 展示角色状态 */ public void displayState() { System.out.println("生命值:" + vit); System.out.println("攻击值:" + vit); System.out.println("防御值:" + vit); } }备忘录角色public class RoleStateMemento { /** * 生命值 */ private int vit; /** * 攻击值 */ private int atk; /** * 防御值 */ private int def; public RoleStateMemento() { } public RoleStateMemento(int vit, int atk, int def) { this.vit = vit; this.atk = atk; this.def = def; } public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } }备忘录管理者public class RoleStateCaretaker { private RoleStateMemento roleStateMemento; public RoleStateMemento getRoleStateMemento() { return roleStateMemento; } public void setRoleStateMemento(RoleStateMemento roleStateMemento) { this.roleStateMemento = roleStateMemento; } }Clientpublic class Client { public static void main(String[] args) { System.out.println("====战斗前===="); GameRole gameRole = new GameRole(); gameRole.initState(); gameRole.displayState(); //备份角色状态 RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker(); roleStateCaretaker.setRoleStateMemento(gameRole.saveState()); System.out.println("====战斗后===="); gameRole.fight(); gameRole.displayState(); System.out.println("====恢复状态===="); gameRole.recoverState(roleStateCaretaker.getRoleStateMemento()); gameRole.displayState(); } }“黑箱”备忘录备忘录角色对发起人提供宽接口,对其他对象提供窄接口(即将备忘录类设置为发起人的内部类)发起人角色public class GameRole { /** * 生命值 */ private int vit; /** * 攻击值 */ private int atk; /** * 防御值 */ private int def; public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } /** * 初始化内部状态 */ public void initState() { this.vit = 100; this.atk = 100; this.def = 100; } /** * 战斗 */ public void fight() { this.vit = 0; this.atk = 0; this.def = 0; } /** * 保存游戏角色状态 * * @return */ public Memento saveState() { return new RoleStateMemento(vit, atk, def); } /** * 恢复角色状态 * * @param memento */ public void recoverState(Memento memento) { RoleStateMemento roleStateMemento = (RoleStateMemento) memento; this.vit = roleStateMemento.getVit(); this.atk = roleStateMemento.getAtk(); this.def = roleStateMemento.getDef(); } /** * 展示角色状态 */ public void displayState() { System.out.println("生命值:" + vit); System.out.println("攻击值:" + vit); System.out.println("防御值:" + vit); } private class RoleStateMemento implements Memento { /** * 生命值 */ private int vit; /** * 攻击值 */ private int atk; /** * 防御值 */ private int def; public RoleStateMemento() { } public RoleStateMemento(int vit, int atk, int def) { this.vit = vit; this.atk = atk; this.def = def; } public int getVit() { return vit; } public void setVit(int vit) { this.vit = vit; } public int getAtk() { return atk; } public void setAtk(int atk) { this.atk = atk; } public int getDef() { return def; } public void setDef(int def) { this.def = def; } } }备忘录接口(对外提供的窄接口)public interface Memento { }备忘录管理者角色public class RoleStateCaretaker { private Memento memento; public Memento getMemento() { return memento; } public void setMemento(Memento memento) { this.memento = memento; } }Clientpublic class Client { public static void main(String[] args) { System.out.println("====战斗前===="); GameRole gameRole = new GameRole(); gameRole.initState(); gameRole.displayState(); //备份角色状态 RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker(); roleStateCaretaker.setMemento(gameRole.saveState()); System.out.println("====战斗后===="); gameRole.fight(); gameRole.displayState(); System.out.println("====恢复状态===="); gameRole.recoverState(roleStateCaretaker.getMemento()); gameRole.displayState(); } }(4)优缺点提供了一种可恢复状态的机制,当用户需要事,可方便的恢复到某个历史状态实现了内部状态的封装,除发起人之外的角色对这些状态信息都没有读写权限简化了发起人类,内部状态由管理者进行管理,发起人无需关心,复合单一职责原则当需要存储的状态信息及频率过多过大时,需要消耗更多的资源(5)使用场景需要保存与恢复数据时需要提供可回滚操作时,如 ctrl + z 、数据库回滚等
2022年09月25日
49 阅读
0 评论
0 点赞