2025年3月31日 星期一 乙巳(蛇)年 正月初一 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > 设计模式

【设计模式】24.行为型模式-备忘录(Memento)

时间:02-01来源:作者:点击数:73

一、描述

  定义:在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复至原来保存的状态。

  理解:当需要保存某个对象的操作数据或者该对象状态时,使用另外一个类记录原始对象的数据以及状态,以便回退到以前的某种数据。

  角色:

  1.Originator(原发器):定义原始类的数据以及状态信息,包含创建备忘录的方法。

  2.Memonto(备忘录):定义与原始类相同的业务数据结构。

  3.CareTaker(负责人):负责存储备忘录。

  类图:

二、优点

  1.给用户提供了一种可以恢复状态的机制,可以使用户能够比较方便地回到某个历史的状态。

  2.实现了信息的封装,使得用户不需要关心状态的保存细节。

三、缺点

  1.消耗资源。如果类的成员变量过多,势必会占用比较大的资源,而且每一次保存都会消耗一定的内存。

四、使用场景

  1.需要保存或者恢复数据的相关状态场景。

  2.提供一个可回滚的操作。

五、示例

  玩儿过游戏的都知道,英雄在被杀死之后,状态会变为等待复活。当复活之后,状态、等级之类的数据会恢复到上次死亡之前。以“王者荣耀中的英雄为例”:玩妲己,当前等级为12,被敌方英雄打死之后,状态为死亡,各种装备都不能使用;等待一段时间之后满血复活,恢复为复活状态。

  1.英雄(原发器)

  • /**
  • * Hero
  • * 英雄
  • *
  • * @author zhouxy
  • * @date 2022/8/1
  • **/
  • @Data
  • public class Hero {
  • /** 状态 */
  • private String state;
  • /** 生命值 */
  • private int life;
  • /** 伤害 */
  • private int damage;
  • /** 防御力 */
  • private int defense;
  • /**
  • * 创建备忘录对象,并且将当前数据信息赋值给备忘录
  • *
  • * @return
  • */
  • public HeroMemento createHeroMemento() {
  • HeroMemento memento = new HeroMemento();
  • memento.setState(this.state);
  • memento.setLife(this.life);
  • memento.setDamage(this.damage);
  • memento.setDefense(this.defense);
  • return memento;
  • }
  • /**
  • * 还原当前对象信息
  • */
  • public void recover(HeroMemento heroMemento) {
  • this.state = heroMemento.getState();
  • this.life = heroMemento.getLife();
  • this.damage = heroMemento.getDamage();
  • this.defense = heroMemento.getDefense();
  • }
  • public void display(){
  • System.out.println("状态:" + this.state);
  • System.out.println("生命:" + this.life);
  • System.out.println("伤害:" + this.damage);
  • System.out.println("防御力:" + this.defense);
  • }
  • }

  2.英雄备忘录(备忘录)

  • /**
  • * HeroMemonto
  • *
  • * @author zhouxy
  • * @date 2022/8/1
  • **/
  • @Data
  • public class HeroMemento {
  • /** 状态 */
  • private String state;
  • /** 生命值 */
  • private int life;
  • /** 伤害 */
  • private int damage;
  • /** 防御力 */
  • private int defense;
  • }

  3.负责人

  • /**
  • * CareTaker
  • *
  • * @author zhouxy
  • * @date 2022/8/1
  • **/
  • @Data
  • public class CareTaker {
  • public HeroMemento heroMemento;
  • }

  4.测试

  • public class Client {
  • public static void main(String[] args) throws InterruptedException {
  • Hero hero = new Hero();
  • hero.setState("战斗中");
  • hero.setLife(100);
  • hero.setDamage(100);
  • hero.setDefense(100);
  • //保存备忘录信息
  • CareTaker careTaker = new CareTaker();
  • careTaker.setHeroMemento(hero.createHeroMemento());
  • hero.display();
  • System.out.println("============战亡============");
  • hero.setState("待复活");
  • hero.setLife(0);
  • hero.setDamage(0);
  • hero.setDefense(0);
  • hero.display();
  • System.out.println("============等待5秒之后复活============");
  • for (int i = 1; i <= 5; i++) {
  • System.out.println(i + "秒");
  • Thread.sleep(1000);
  • }
  • //取出备忘录数据,还原英雄数据
  • System.out.println("============已复活============");
  • hero.recover(careTaker.getHeroMemento());
  • hero.display();
  • }
  • }

  实现效果:

  • 状态:战斗中
  • 生命:100
  • 伤害:100
  • 防御力:100
  • ============战亡============
  • 状态:待复活
  • 生命:0
  • 伤害:0
  • 防御力:0
  • ============等待5秒之后复活============
  • 1秒
  • 2秒
  • 3秒
  • 4秒
  • 5秒
  • ============已复活============
  • 状态:战斗中
  • 生命:100
  • 伤害:100
  • 防御力:100
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门