乐趣区

关于java:备忘录模式

依据对象的外部状态变动生成相应快照,以便当前复原。

上面举一个应用笔记软件的历史版本控制的例子来实现备忘录模式。

原始类

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}
退出移动版