简介:用来存储另外一个对象内部状态的快照的对象
目的:在不破坏封装的条件下,将一个对象的状态捉住,并外部化,存储起来,从而可以在将来合适的时候把这个对象还原到存储起来的状态
总结:记录某时刻某对象的状态,在需要的时候进行状态恢复
组成:备录对象,备录对象的状态, 备忘录管理器
1,备录对象
package com.design.p.memo; import java.util.ArrayList; import java.util.Collections; import java.util.List; /** * 被备录对象 */ public class Persion{ private String userName; private List<String> storyList = new ArrayList<>(); public Persion(String userName) { this.userName = userName; } public String getUserName() { return userName; } public List<String> getStoryList() { // return Collections.unmodifiableList(storyList); return storyList; } public void setStoryList(List<String> storyList) { this.storyList = storyList; } public void addStory(String story){ storyList.add(story); } /** * 获取备忘 * @return */ public Memo getMemo() { Memo memo = new Memo(); memo.setStoryList(storyList); return memo; } /** * 重置备忘 * @param memo */ public void setMemo(Memo memo) { storyList = memo.getStoryList(); } /** * 备忘打印 */ public String toString(){ StringBuilder str = new StringBuilder(); str.append(userName + " 读过的书: "); for(String st : storyList){ str.append(st).append(" "); } return str.toString(); } }
2,备录对象的状态
package com.design.p.memo; import java.util.List; /** * 被备录对象-状态 */ public class Memo{ private List<String> storyList = null; public List<String> getStoryList() { return storyList; } public void setStoryList(List<String> storyList) { this.storyList = storyList; } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("Memo [storyList="); builder.append(storyList); builder.append("]"); return builder.toString(); } }
3,备忘录管理器
package com.design.p.memo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 备忘录管理器 */ public class MemoManager { /** * 备忘集合 */ public Map<String, Memo> memoMap = new HashMap<String, Memo>(); /** * 记录备忘 */ public void putMemoMap(Persion persion){ //为了已经保存的某时刻对象的状态, 该对象需要和原内存地址分离; 如果不分离会导致已保存的状态被篡改 Persion temp = new Persion(persion.getUserName()); temp.setMemo(persion.getMemo()); List<String> storyList = new ArrayList<>(); storyList.addAll(persion.getStoryList()); temp.setStoryList(storyList); // storyList是不能被修改的 // List<String> storyList = Collections.unmodifiableList(temp.getStoryList()); Memo memo = new Memo(); memo.setStoryList(temp.getStoryList()); memoMap.put(temp.getUserName(), memo); } /** * 恢复备忘 */ public void resetMemoForPersion(Persion persion){ if(memoMap.containsKey(persion.getUserName())){ Memo memo = memoMap.get(persion.getUserName()); persion.setMemo(memo); }else{ persion.setMemo(null); } } @Override public String toString() { StringBuilder builder = new StringBuilder(); builder.append("MemoManager [memoMap="); builder.append(memoMap); builder.append("]"); return builder.toString(); } }
4,Main
package com.design.p.memo; /** * 16-备忘录模式 * * 记录某时刻某对象的状态,在需要的时候进行状态恢复 */ public class MainTest { public static void main(String[] args) { MemoManager memoManager = new MemoManager(); Persion persionA = new Persion("用户A"); persionA.addStory("红楼梦"); persionA.addStory("平凡的世界"); persionA.addStory("三国演义"); System.out.println("persionA=" + persionA.toString()); //进行备忘 System.out.println("备忘之前---memomanager=" + memoManager.toString()); memoManager.putMemoMap(persionA); System.out.println("备忘完成---memomanager=" + memoManager.toString() +" "); persionA.addStory("悲惨的世界"); System.out.println(persionA.toString()); System.out.println("备忘信息---memomanager=" + memoManager.toString() +" "); //恢复备忘 memoManager.resetMemoForPersion(persionA); System.out.println("备忘恢复 ... ... "); System.out.println(persionA.toString()); } }
5,Result
persionA=用户A 读过的书: 红楼梦 平凡的世界 三国演义 备忘之前---memomanager=MemoManager [memoMap={}] 备忘完成---memomanager=MemoManager [memoMap={用户A=Memo [storyList=[红楼梦, 平凡的世界, 三国演义]]}] 用户A 读过的书: 红楼梦 平凡的世界 三国演义 悲惨的世界 备忘信息---memomanager=MemoManager [memoMap={用户A=Memo [storyList=[红楼梦, 平凡的世界, 三国演义]]}] 备忘恢复 ... ... 用户A 读过的书: 红楼梦 平凡的世界 三国演义