网站服务类型/推广普通话图片
记录状态,便于回滚
(记录快照,瞬时状态、存盘)
(和命令模式的区别在于,命令模式需要一步一步的回滚;而备忘录模式可以跳过中间的过程,一直回滚到快照的版本)
(对象序列化)
代码结构
源码
package com.myspringboot.shejimoshi.beiwanglu;import java.io.*;
import java.util.HashMap;public class Main {public static void main(String[] args) {HashMap<String, String> map = new HashMap<>();map.put("key", "value");System.out.println(map.get("key"));save(map);HashMap<String, String> lodeMap = lode();System.out.println(lodeMap.get("key"));}private static void save(HashMap<String, String> map) {try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("D:\\javatest\\map.map")))) {oos.writeObject(map);oos.flush();} catch (Exception e) {e.printStackTrace();}}private static HashMap<String, String> lode() {HashMap<String, String> map = null;try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("D:\\javatest\\map.map")))) {map = (HashMap<String, String>) ois.readObject();} catch (Exception e) {e.printStackTrace();}return map;}
}