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

郑州手机网站推广外包平面设计图制作

郑州手机网站推广外包,平面设计图制作,建站方案书备案,wordpress添加域名设计模式--行为型--备忘录模式 备忘录模式定义结构案例实现白箱备忘录模式黑箱备忘录模式 优缺点使用场景 备忘录模式 定义 又叫快照模式#xff0c;在不破坏封装性的前提下#xff0c;捕获一个对象的对象的内部状态#xff0c;并在该对象之外保存这个状态#xff0c;以便… 设计模式--行为型--备忘录模式 备忘录模式定义结构案例实现白箱备忘录模式黑箱备忘录模式 优缺点使用场景 备忘录模式 定义 又叫快照模式在不破坏封装性的前提下捕获一个对象的对象的内部状态并在该对象之外保存这个状态以便以后当需要时能将该对象恢复到原先保存的状态。 结构 发起人角色Originator记录当前时刻的内部状态信息提供创建备忘录和恢复备忘录数据的功能实现其他业务功能它可以访问备忘录里的所有信息。备忘录角色Memento负责存储发起人的内部状态在需要的时候提供这些内部状态给发起人管理者角色Caretaker对备忘录进行管理提供保存与获取备忘录的功能但其不能对备忘录的内容进行访问与修改 备忘录有两个等效接口窄接口管理者对象和其他发起人对象之外的任何对象看到的是备忘录的窄接口这个窄接口只允许他把备忘录对象传递给其他的对象。宽接口与管理者看到的窄接口相反发起人对象可以看到一个宽接口这个宽接口允许它读取所有的数据以便根据这些数据恢复这个发起人对象内部状态。 案例实现 游戏保存游戏角色有生命力攻击力防御力等数据模拟玩家打boss前的保存机制失败后可以返回到进入boss关之前的状态 实现上述案例有两种方式 白箱备忘录黑箱备忘录 白箱备忘录模式 备忘录角色对任何对象都提供一个接口即宽接口备忘录角色内部所存储的状态就对所有对象公开。类图如下 /*** 游戏角色类 发起人角色*/ public class GameRole {private int vit; // 生命private int atk; // 攻击力private int def; // 防御力// 初始化内部状态public void initState() {this.vit 100;this.atk 100;this.def 100;}// 战斗public void fight() {this.vit 0;this.atk 100;this.def 100;}// 保存角色状态public RoleStateMemento saveState() {return new RoleStateMemento(vit, atk, def);}// 恢复角色到初始状态public void recoverState(RoleStateMemento roleStatusMemento) {// 将备忘录中的数据赋值给当前对象this.vit roleStatusMemento.getVit();this.atk roleStatusMemento.getAtk();this.def roleStatusMemento.getDef();}// 展示状态功能public void stateDisplay() {System.out.println(角色生命 vit);System.out.println(角色攻击 atk);System.out.println(角色防御 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 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 {// 声明RoleStateMemento类型的变量private RoleStateMemento roleStatusMemento;public RoleStateMemento getRoleStatusMemento() {return roleStatusMemento;}public void setRoleStatusMemento(RoleStateMemento roleStatusMemento) {this.roleStatusMemento roleStatusMemento;} }public class Test01 {public static void main(String[] args) {// 创建游戏角色对象GameRole gameRole new GameRole();System.out.println(---boss关卡前---);gameRole.initState();gameRole.stateDisplay();// 将游戏角色内部状态进行备份// 创建管理者对象RoleStateCaretaker roleStateCaretaker new RoleStateCaretaker();roleStateCaretaker.setRoleStatusMemento(gameRole.saveState());System.out.println(---挑战boss---);gameRole.fight();gameRole.stateDisplay();System.out.println(---生命为0boss关挑战失败回到boss关卡前---);gameRole.recoverState(roleStateCaretaker.getRoleStatusMemento());gameRole.stateDisplay();} }白箱备忘录模式是破坏封装性的但是通过程序员的自律同样可以在一定程度上实现模式的大部分用意 黑箱备忘录模式 备忘录角色对发起人提供一个宽接口而为其他对象提供一个窄接口。在JAVA语言中实现双重接口的办法就是将备忘录类设计成发起人类的内部成员类。将RoleStateMemento设为GameRole的内部类从而将RoleStateMemento对象封装在GameRole里面在外面提供一个标识接口Memento给RoleStateCaretaker及其他对象使用。这样GameRole类看到的是RoleStateMemento所有的接口而RoleStateCaretaker及其他对象看到的仅仅是标识接口Memento所暴露出来的接口从而维护了封装性。 /*** 备忘录接口 对外提供窄接口*/ public interface Memento { }/*** 游戏角色类 发起人角色*/ public class GameRole {private int vit; // 生命private int atk; // 攻击力private int def; // 防御力// 初始化内部状态public void initState() {this.vit 100;this.atk 100;this.def 100;}// 战斗public void fight() {this.vit 0;this.atk 100;this.def 100;}// 保存角色状态public Memento saveState() {return new RoleStateMemento(vit, atk, def);}// 恢复角色到初始状态public void recoverState(Memento memento) {RoleStateMemento roleStatusMemento (RoleStateMemento) memento;// 将备忘录中的数据赋值给当前对象this.vit roleStatusMemento.getVit();this.atk roleStatusMemento.getAtk();this.def roleStatusMemento.getDef();}// 展示状态功能public void stateDisplay() {System.out.println(角色生命 vit);System.out.println(角色攻击 atk);System.out.println(角色防御 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;}// 私有的成员内部类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 class RoleStateCaretaker {// 声明RoleStateMemento类型的变量private Memento memento;public Memento getMemento() {return memento;}public void setMemento(Memento memento) {this.memento memento;} }public class Test01 {public static void main(String[] args) {// 创建游戏角色对象GameRole gameRole new GameRole();System.out.println(---boss关卡前---);gameRole.initState();gameRole.stateDisplay();// 将游戏角色内部状态进行备份// 创建管理者对象RoleStateCaretaker roleStateCaretaker new RoleStateCaretaker();roleStateCaretaker.setMemento(gameRole.saveState());System.out.println(---挑战boss---);gameRole.fight();gameRole.stateDisplay();System.out.println(---生命为0boss关挑战失败回到boss关卡前---);gameRole.recoverState(roleStateCaretaker.getMemento());gameRole.stateDisplay();} }优缺点 优点 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史状态。实现了内部状态的封装。除了创建它的发起人之外其他对象都不能够访问这些状态信息简化了发起人类。发起人不需要管理和保存其内部状态的各个备份所有状态信息都保存在备忘录中并由管理者进行管理这符合单一职责原则。 缺点 资源消耗大如果要保存的内部状态信息过多或者特别频繁将会占用较大的内存资源。 使用场景 需要保存与恢复数据的场景例如游戏的存档功能。需要提供一个可回滚操作的场景。
http://wiki.neutronadmin.com/news/440935/

相关文章:

  • 好的公司网站建设公司网站设计制作开发方案
  • 手机微网站建设中国建筑网官网校招
  • 关于加强学校网站建设的通知东莞公司网站建设教程
  • 电影网站建设模板可以分销的平台
  • 雪亮工程建设网站界面新手怎么引流推广
  • 做网站要学什么专业上海手机网站
  • 邳州网站设计价格沙河企业做网站
  • 俄语网站建设公司长沙臻钬建站活动方案
  • 专业做互联网招聘的网站有哪些常熟建设网站
  • 唐山网站制作app网站开发前后端分离要多少钱
  • 商城类网站建设+数据库wordpress 支付接口
  • 网站 手机版 电脑版 怎么做的做网站需要准备哪些材料
  • 视频网站怎么做统计工商网上注册营业执照
  • 把网站制作成app旅游网站制作过程
  • 三明网站开发wordpress手机不方便
  • 自己电脑建设网站公众号免费推广平台
  • 直接IP做网站怎么让公司网站随便就搜的到
  • 软装设计网站有哪些网站建设公司哪有
  • 西安企业建站机构那里有销售渠道有哪几种
  • 重庆网站开发工资58同城青岛网站建设
  • 免费做图素材网站创建wordpress数据库
  • 瑞安做网站的公司新手怎么做seo优化
  • 设计企业网站多少钱百度优化排名
  • 大连在哪个网站做网上核名天猫商城的商品来源
  • 如何做移动端网站沈阳在线制作网站
  • 企业先做网站还是先做淘宝外包做网站哪家好
  • 邯郸做网站流程平台系统维护要几天
  • 网站建设公司做的网站鄂尔多斯网架公司
  • 网站平台管理培训网站建设课程
  • 制作一个工厂小程序收费谷歌seo知识