根据对象内部状态变化生成相应的快照,供后期恢复。下面是利用笔记软件的历史版本控制实现备忘录模式的例子。原始类publicclassNote{privateStringtitle;私有字符串内容;私有长创建时间;私人长更新时间;私有整数版本;publicNote(Stringtitle){this.title=title;this.createTime=System.currentTimeMillis();这个.version=0;}/***更新笔记*@paramtitle*@paramcontent*/publicvoidupdate(Stringtitle,Stringcontent){NoteMementomemento=newNoteMemento(this.title,this.content,this.version);NoteStack.push(纪念品);if(title!=null)this.title=title;如果(内容!=null)this.content=content;this.updateTime=System.currentTimeMillis();这个版本++;}/***返回最近更改记录*@return*/publicListhistory(){returnNoteStack.history();}/***恢复上一个版本的信息*/publicvoidrestore(){NoteMementomemento=NoteStack.lastVersion();这.title=memento.getTitle();this.content=memento.getContent();this.updateTime=System.currentTimeMillis();这个版本++;}publicStringgetTitle(){返回标题;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetContent(){返回内容;}publicvoidsetContent(Stringcontent){this.content=content;}publicLonggetCreateTime(){returncreateTime;}publicvoidsetCreateTime(LongcreateTime){this.createTime=createTime;}publicLonggetUpdateTime(){返回更新时间;}publicvoidsetUpdateTime(LongupdateTime){this.updateTime=updateTime;}publicIntegergetVersion(){返回版本;}publicvoidsetVersion(Integerversion){this.version=version;}@OverridepublicStringtoString(){return"Note{"+"title='"+title+'\''+",content='"+content+'\''+",createTime="+createTime+",updateTime="+updateTime+",version="+version+'}';}}备忘记录类(旧版本类)publicclassNoteMemento{privateStringtitle;私有字符串内容;私有整数版本;publicNoteMemento(Stringtitle,Stringcontent,Integerversion){this.title=title;this.content=内容;this.version=版本;}publicStringgetTitle(){返回标题;}publicvoidsetTitle(Stringtitle){this.title=title;}publicStringgetContent(){返回内容;}publicvoidsetContent(Stringcontent){this.content=content;}publicIntegergetVersion(){返回版本;}publicvoidsetVersion(Integerversion){this.version=版本;}@OverridepublicStringtoString(){return"NoteMemento{"+"title='"+title+'\''+",content='"+content+'\''+",version="+version+'}';}}version存储类publicclassNoteStack{privatestaticStacknoteMementos=newStack<>();publicstaticvoidpush(NoteMementomemento){noteMementos.push(memento);}/***返回最近5条历史记录*@return*/publicstaticListhistory(){returnnoteMementos.subList(noteMementos.size()>5?noteMementos.size()-5:0,noteMementos。尺寸());}/***返回上一个版本*@return*/publicstaticNoteMementolastVersion(){returnnoteMementos.lastElement();}}测试类publicclassMemorandumTest{@Testpublicvoidtest(){Notenote=newNote("母猪护理从入门到熟练");系统.out.println(注);note.update(null,"好大一头母猪,好不容易照顾!!!");note.update("母猪护理从入门到熟练",null);System.out.println(注);System.out.println(note.history());note.update(null,"还用管它吗?它好大啊!");note.update(null,"不用管了,我去养一头野猪!");note.update(null,"啊!野猪好大啊!");note.update(null,"我继续喂母猪!");System.out.println(注);System.out.println(note.history());注意.恢复();System.out.println(注);}}=====测试结果=====Note{title='母猪护理从入门到精通',content='null',createTime=1655912034992,updateTime=null,version=0}Note{title='母猪护理从入门到精通',content='好大的脑袋母猪好难养啊!!!!',createTime=1655912034992,updateTime=1655912034994,version=2}[NoteMemento{title='母猪护理从入门到精通',content='null',version=0},NoteMemento{title='母猪护理从入门到要proficient',content='好大一头母猪,真难养!!!!',version=1}]Note{title='母猪护理从入门到精通',content='我会继续护理母猪!',createTime=1655912034992,updateTime=1655912034996,version=6}[NoteMemento{title='母猪护理从入门到精通',content='好大一头母猪,真难养啊!!!!',version=1},NoteMemento{title='母猪保养从入门到精通',content='好大一头母猪,真难养啊!!!!',version=2},NoteMemento{title='从入门到精通的母猪护理',content='要不要护理?真的很大!',version=3},NoteMemento{title='母猪从入门到精通',content='我不管了,我要找一头公猪来照顾!',version=4},NoteMemento{title='播种从入门到精通',content='啊!野猪也很大!',version=5}]Note{title='母猪维护从入门到精通',content='啊!野猪也很大!',createTime=1655912034992,updateTime=1655912034996,version=7}