设计模式之备忘录模式

备忘录模式(Memento Pattern)官方的定义是这样的:
在不破坏封闭的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。它是面向对象的23种设计模式中的一种,属于行为模式的范围。
直白点说就是:
我们可以在不暴露更多字段的前提下,直接将某个对象保存到其他地方,可以是磁盘,也可以是内存。
有些地方也将之称为快照模式,就是给当前对象做一个快照。

一般我们将备忘录模式定义为3个组成角色:
1、Originator 发起人,有些人将他翻译为原发器,我是非常不认同的。
什么叫原发器?本人查了辞海等在线网站,压根就没有这个词,我非常不人认同最初的译者,凭感觉随意造词。
Originator 查询了谷歌翻译(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )
a person who creates or initiates something.
大概可以理解为 创始人或者是最初发起某个事情的人,所以这里可以理解为驱动、发起生成备忘录的角色。
2、Memento 备忘录,用来存储要保存对象的内部状态,也就是快照。
3、Caretaker 负责人 负责保存和管理备忘录的。

下面我们写一个例子,
背景:模仿超级马里奥,可以吃金币,移动

主类:

 1 package com.example.demo.learn.pattern.behavior.memento;
 2 
 3 import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
 4 
 5 import java.util.Map;
 6 
 7 /**
 8  * @discription
 9  */
10 public class RecordSystem {
11 
12     private static Map<String, String> recordMap = Maps.newHashMap();
13 
14     public static void saveRecord(MapInfo mapInfo, String index) {
15         recordMap.put(index, mapInfo.saveRecord());
16     }
17 
18     public static MapInfo getRecord(String time) {
19         if (recordMap.containsKey(time)) {
20             return MapInfo.loadRecord(recordMap.get(time));
21         }
22         return null;
23     }
24 }

游戏内容类:

 1 package com.example.demo.learn.pattern.behavior.memento;
 2 
 3 import com.alibaba.fastjson.JSON;
 4 import lombok.Data;
 5 
 6 /**
 7  * @discription
 8  */
 9 @Data
10 public class MapInfo {
11     private Location marioLocation = new Location(0, 100, 1);
12     private int golden = 0;
13 
14     public void touchGolden() {
15         golden++;
16     }
17 
18     public void marioMove(int xv, int yv) {
19         int newX = marioLocation.getX() + xv;
20         marioLocation.setX(newX);
21 
22         int newY = marioLocation.getY() + yv;
23         marioLocation.setY(newY);
24     }
25 
26     //马里奥调头
27     public void marioTuneBack() {
28         int forward = marioLocation.getForward() * -1;
29         marioLocation.setX(forward);
30     }
31 
32     public String saveRecord() {
33         return JSON.toJSONString(marioLocation) + ";" + golden;
34     }
35 
36     public static MapInfo loadRecord(String recordContent) {
37         String[] contents = recordContent.split(";");
38         MapInfo mapInfo = new MapInfo();
39         Location marioLocation = JSON.parseObject(contents[0], Location.class);
40         mapInfo.setMarioLocation(marioLocation);
41         mapInfo.setGolden(Integer.valueOf(contents[1]));
42         return mapInfo;
43     }
44 
45     public static MapInfo initMap() {
46         MapInfo mapInfo = new MapInfo();
47         return mapInfo;
48     }
49 }

 

package com.example.demo.learn.pattern.behavior.memento;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * @discription
 */

@AllArgsConstructor
@Data
public class Location {
    //坐标
    private int x;
    //坐标
    private int y;

    //方向 -1向左,1向右
    private int forward;
}

游戏记录存储

 1 package com.example.demo.learn.pattern.behavior.memento;
 2 
 3 import com.alibaba.nacos.shaded.com.google.common.collect.Maps;
 4 
 5 import java.util.Map;
 6 
 7 /**
 8  * @discription
 9  */
10 public class RecordSystem {
11 
12     private static Map<String, String> recordMap = Maps.newHashMap();
13 
14     public static void saveRecord(MapInfo mapInfo, String index) {
15         recordMap.put(index, mapInfo.saveRecord());
16     }
17 
18     public static MapInfo getRecord(String time) {
19         if (recordMap.containsKey(time)) {
20             return MapInfo.loadRecord(recordMap.get(time));
21         }
22         return null;
23     }
24 }

输出大概是这样:

"C:\Program Files\Java\jdk-11\bin\java.exe" "-javaagent:E:\Program Files\JetBrains\IntelliJ IDEA 2023.2\lib\idea_rt.jar=65256:E:\Program Files\JetBrains\IntelliJ IDEA 2023.2\bin" -Dfile.encoding=UTF-8 -classpath E:\code\common\learn-design-pattern\target\classes;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter\2.6.15\spring-boot-starter-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot\2.6.15\spring-boot-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-autoconfigure\2.6.15\spring-boot-autoconfigure-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-logging\2.6.15\spring-boot-starter-logging-2.6.15.jar;E:\Maven\learnDesignPattenRepo\ch\qos\logback\logback-classic\1.2.12\logback-classic-1.2.12.jar;E:\Maven\learnDesignPattenRepo\ch\qos\logback\logback-core\1.2.12\logback-core-1.2.12.jar;E:\Maven\learnDesignPattenRepo\org\slf4j\slf4j-api\1.7.36\slf4j-api-1.7.36.jar;E:\Maven\learnDesignPattenRepo\org\apache\logging\log4j\log4j-to-slf4j\2.17.2\log4j-to-slf4j-2.17.2.jar;E:\Maven\learnDesignPattenRepo\org\apache\logging\log4j\log4j-api\2.17.2\log4j-api-2.17.2.jar;E:\Maven\learnDesignPattenRepo\org\slf4j\jul-to-slf4j\1.7.36\jul-to-slf4j-1.7.36.jar;E:\Maven\learnDesignPattenRepo\jakarta\annotation\jakarta.annotation-api\1.3.5\jakarta.annotation-api-1.3.5.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-core\5.3.27\spring-core-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-jcl\5.3.27\spring-jcl-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\yaml\snakeyaml\1.29\snakeyaml-1.29.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-web\2.6.15\spring-boot-starter-web-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-json\2.6.15\spring-boot-starter-json-2.6.15.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.13.5\jackson-datatype-jdk8-2.13.5.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.13.5\jackson-datatype-jsr310-2.13.5.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\module\jackson-module-parameter-names\2.13.5\jackson-module-parameter-names-2.13.5.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-tomcat\2.6.15\spring-boot-starter-tomcat-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\apache\tomcat\embed\tomcat-embed-core\9.0.75\tomcat-embed-core-9.0.75.jar;E:\Maven\learnDesignPattenRepo\org\apache\tomcat\embed\tomcat-embed-el\9.0.75\tomcat-embed-el-9.0.75.jar;E:\Maven\learnDesignPattenRepo\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.75\tomcat-embed-websocket-9.0.75.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-web\5.3.27\spring-web-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-beans\5.3.27\spring-beans-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-webmvc\5.3.27\spring-webmvc-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-expression\5.3.27\spring-expression-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-test\2.6.15\spring-boot-test-2.6.15.jar;E:\Maven\learnDesignPattenRepo\junit\junit\4.13.2\junit-4.13.2.jar;E:\Maven\learnDesignPattenRepo\org\hamcrest\hamcrest-core\2.2\hamcrest-core-2.2.jar;E:\Maven\learnDesignPattenRepo\org\hamcrest\hamcrest\2.2\hamcrest-2.2.jar;E:\Maven\learnDesignPattenRepo\org\apache\commons\commons-lang3\3.13.0\commons-lang3-3.13.0.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\nacos\nacos-spring-context\1.1.1\nacos-spring-context-1.1.1.jar;E:\Maven\learnDesignPattenRepo\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-context\5.3.27\spring-context-5.3.27.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\spring\spring-context-support\1.0.11\spring-context-support-1.0.11.jar;E:\Maven\learnDesignPattenRepo\org\projectlombok\lombok\1.18.30\lombok-1.18.30.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\fastjson\1.2.83\fastjson-1.2.83.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-aop\2.6.15\spring-boot-starter-aop-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-aop\5.3.27\spring-aop-5.3.27.jar;E:\Maven\learnDesignPattenRepo\cglib\cglib\3.1\cglib-3.1.jar;E:\Maven\learnDesignPattenRepo\org\ow2\asm\asm\4.2\asm-4.2.jar;E:\Maven\learnDesignPattenRepo\org\springframework\boot\spring-boot-starter-amqp\2.6.15\spring-boot-starter-amqp-2.6.15.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-messaging\5.3.27\spring-messaging-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\springframework\amqp\spring-rabbit\2.4.12\spring-rabbit-2.4.12.jar;E:\Maven\learnDesignPattenRepo\org\springframework\amqp\spring-amqp\2.4.12\spring-amqp-2.4.12.jar;E:\Maven\learnDesignPattenRepo\org\springframework\retry\spring-retry\1.3.4\spring-retry-1.3.4.jar;E:\Maven\learnDesignPattenRepo\com\rabbitmq\amqp-client\5.13.1\amqp-client-5.13.1.jar;E:\Maven\learnDesignPattenRepo\org\springframework\spring-tx\5.3.27\spring-tx-5.3.27.jar;E:\Maven\learnDesignPattenRepo\org\aspectj\aspectjweaver\1.9.7\aspectjweaver-1.9.7.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\nacos\nacos-client\2.2.0\nacos-client-2.2.0.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\nacos\nacos-auth-plugin\2.2.0\nacos-auth-plugin-2.2.0.jar;E:\Maven\learnDesignPattenRepo\com\alibaba\nacos\nacos-encryption-plugin\2.2.0\nacos-encryption-plugin-2.2.0.jar;E:\Maven\learnDesignPattenRepo\commons-codec\commons-codec\1.15\commons-codec-1.15.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\core\jackson-core\2.13.5\jackson-core-2.13.5.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\core\jackson-databind\2.13.5\jackson-databind-2.13.5.jar;E:\Maven\learnDesignPattenRepo\com\fasterxml\jackson\core\jackson-annotations\2.13.5\jackson-annotations-2.13.5.jar;E:\Maven\learnDesignPattenRepo\org\apache\httpcomponents\httpasyncclient\4.1.5\httpasyncclient-4.1.5.jar;E:\Maven\learnDesignPattenRepo\org\apache\httpcomponents\httpcore\4.4.16\httpcore-4.4.16.jar;E:\Maven\learnDesignPattenRepo\org\apache\httpcomponents\httpcore-nio\4.4.16\httpcore-nio-4.4.16.jar;E:\Maven\learnDesignPattenRepo\org\apache\httpcomponents\httpclient\4.5.14\httpclient-4.5.14.jar;E:\Maven\learnDesignPattenRepo\io\prometheus\simpleclient\0.12.0\simpleclient-0.12.0.jar;E:\Maven\learnDesignPattenRepo\io\prometheus\simpleclient_tracer_otel\0.12.0\simpleclient_tracer_otel-0.12.0.jar;E:\Maven\learnDesignPattenRepo\io\prometheus\simpleclient_tracer_common\0.12.0\simpleclient_tracer_common-0.12.0.jar;E:\Maven\learnDesignPattenRepo\io\prometheus\simpleclient_tracer_otel_agent\0.12.0\simpleclient_tracer_otel_agent-0.12.0.jar com.example.demo.learn.pattern.behavior.memento.PatternMain
16:10:40.045 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain - 存档中。。。
16:10:40.173 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain - 1号存档已完成。。。
16:10:40.173 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain (防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )- 存档中。。。
16:10:40.173 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain - 2号存档已完成。。。
16:10:40.173 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain - 读档中。。。
16:10:40.192 [main] WARN com.example.demo.learn.pattern.behavior.memento.PatternMain - 1号读档已完成。。。

Process finished with exit code 0

在这个例子中 MapInfo 拥有了所有内部状态属性,但是并不直接暴露出来,我们通过它的save接口,来获取对象的所有状态。

这个例子中它负责发起数据的保存,充当了Originator的角色;
而RecordSystem类,负责管理和保存备忘录属于caretaker角色。
RecordSystem类中的 recordMap,负责充当备忘录的角色,也就是保存快照的对象;

 

posted @ 2024-09-02 18:50  王若伊_恩赐解脱  阅读(51)  评论(0编辑  收藏  举报