根據對象的內部狀態變化生成相應快照,以便以後恢復。
下面舉一個使用筆記軟件的歷史版本控制的例子來實現備忘錄模式。
原始類
public class Note {
private String title;
private String content;
private Long createTime;
private Long updateTime;
private Integer version;
public Note(String title) {
this.title = title;
this.createTime = System.currentTimeMillis();
this.version = 0;
}
/**
* 更新筆記
* @param title
* @param content
*/
public void update(String title,String content){
NoteMemento memento = new NoteMemento(this.title,this.content,this.version);
NoteStack.push(memento);
if (title != null)this.title = title;
if (content != null)this.content = content;
this.updateTime = System.currentTimeMillis();
this.version++;
}
/**
* 返回最近更改記錄
* @return
*/
public List<NoteMemento> history(){
return NoteStack.history();
}
/**
* 恢復上一個版本的信息
*/
public void restore(){
NoteMemento memento = NoteStack.lastVersion();
this.title = memento.getTitle();
this.content = memento.getContent();
this.updateTime = System.currentTimeMillis();
this.version++;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Long getCreateTime() {
return createTime;
}
public void setCreateTime(Long createTime) {
this.createTime = createTime;
}
public Long getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Override
public String toString() {
return "Note{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
", version=" + version +
'}';
}
}
備忘錄類(舊版本類)
public class NoteMemento {
private String title;
private String content;
private Integer version;
public NoteMemento(String title, String content, Integer version) {
this.title = title;
this.content = content;
this.version = version;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
@Override
public String toString() {
return "NoteMemento{" +
"title='" + title + '\'' +
", content='" + content + '\'' +
", version=" + version +
'}';
}
}
版本存儲類
public class NoteStack {
private static Stack<NoteMemento> noteMementos = new Stack<>();
public static void push(NoteMemento memento){
noteMementos.push(memento);
}
/**
* 返回最近的5條歷史記錄
* @return
*/
public static List<NoteMemento> history(){
return noteMementos.subList(noteMementos.size()>5?noteMementos.size()-5:0,noteMementos.size());
}
/**
* 返回上一個版本
* @return
*/
public static NoteMemento lastVersion(){
return noteMementos.lastElement();
}
}
測試類
public class MemorandumTest {
@Test
public void test(){
Note note = new Note("母豬護理從入門到精通");
System.out.println(note);
note.update(null,"好一大頭母豬,真難護理!!!!");
note.update("母豬養護從入門到精通",null);
System.out.println(note);
System.out.println(note.history());
note.update(null,"還要護理嗎它真的好大!");
note.update(null,"不護理了,我去找頭公豬護理!");
note.update(null,"啊!公豬也好大!");
note.update(null,"我還是繼續護理母豬吧!");
System.out.println(note);
System.out.println(note.history());
note.restore();
System.out.println(note);
}
}
=====測試結果=====
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='母豬護理從入門到精通', 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}