美文网首页
设计模式-备忘录模式

设计模式-备忘录模式

作者: 东方欲晓_莫道君行早 | 来源:发表于2024-06-13 14:14 被阅读0次
    备忘录模式

    备忘录模式(Memento Pattern),又成为快照模式(Snapshot Pattern),或者令牌模式(Token Pattern),是指在不破坏封装得前提下,捕获一个对象得内部状态,并在对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存得状态。属于行为型模式。
    特征:后悔药

    适用场景
    • 需要保存历史快照得场景
    • 希望在对象之外保存状态,且除了自己,其他类对象无法访问状态保存得具体内容
    优点
    • 简化发起人实体类(Originator)职责,隔离状态得存储与获取,实现了信息得封装,客户端无需关心状态得保存细节
    • 提供状态回滚功能
    缺点
    • 消耗资源:如果需要保存得状态过多时,每次保存都需要消耗很多内存。有时也会消耗磁盘
    角色:
    • 发起人角色Originator:负责创建一个备忘录,记录自身需要保存得状态;具备状态回滚功能;
    • 备忘录角色Memento:用于存储Originator得内部状态,且可以防止Originator以外得对象进行访问
    • 备忘录管理员CareTaker:负责存储与提供备忘录Memento,无法对备忘录内容进行操作与访问
    示例代码:对一篇文章得修改与撤销
    发起人角色:
    package com.caozz.demo2.memento.demo;
    
    public class Editor {
    
        private String title;
        private String content;
        private String imgs;
    
        public Editor(String title, String content, String imgs) {
            this.title = title;
            this.content = content;
            this.imgs = imgs;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getContent() {
            return content;
        }
    
        public String getImgs() {
            return imgs;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    
        public void setImgs(String imgs) {
            this.imgs = imgs;
        }
    
        public ArticleMemento saveToMemento(){
            ArticleMemento articleMemento = new ArticleMemento(this.title,this.content,this.imgs);
            return articleMemento;
        }
    
        public void undoFromMemento(ArticleMemento articleMemento){
            this.title = articleMemento.getTitle();
            this.content = articleMemento.getContent();
            this.imgs = articleMemento.getImgs();
        }
    
        @Override
        public String toString() {
            return "Editor{" +
                    "title='" + title + '\'' +
                    ", content='" + content + '\'' +
                    ", imgs='" + imgs + '\'' +
                    '}';
        }
    }
    
    
    备忘录角色
    package com.caozz.demo2.memento.demo;
    
    public class ArticleMemento {
        private String title;
        private String content;
        private String imgs;
    
        public ArticleMemento(String title, String content, String imgs) {
            this.title = title;
            this.content = content;
            this.imgs = imgs;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getContent() {
            return content;
        }
    
        public String getImgs() {
            return imgs;
        }
    
        @Override
        public String toString() {
            return "ArticleMemento{" +
                    "title='" + title + '\'' +
                    ", content='" + content + '\'' +
                    ", imgs='" + imgs + '\'' +
                    '}';
        }
    }
    
    
    备忘录管理者角色
    package com.caozz.demo2.memento.demo;
    
    import java.util.Stack;
    
    public class DraftsBox {
        private final Stack<ArticleMemento> STACK = new Stack<ArticleMemento>();
    
        public ArticleMemento getMemento(){
            ArticleMemento articleMemento = STACK.pop();
            return articleMemento;
        }
    
        public void addMemento(ArticleMemento articleMemento){
            STACK.push(articleMemento);
        }
    
    }
    
    
    测试
    package com.caozz.demo2.memento.demo;
    
    public class Test {
        public static void main(String[] args) {
            DraftsBox draftsBox = new DraftsBox();
    
            Editor editor = new Editor("初始title",
                    "初始文本",
                    "111111.png");
    
            ArticleMemento articleMemento = editor.saveToMemento();
            draftsBox.addMemento(articleMemento);
    
            System.out.println("标题:" + editor.getTitle() + "\n" +
                                "内容:" + editor.getContent() + "\n" +
                                "插图:" + editor.getImgs() + "\n暂存成功");
    
            System.out.println("完整的信息" + editor);
    
    
            System.out.println("==========首次修改文章===========");
            editor.setTitle("首次修改title");
            editor.setContent("首次修改文本");
    
            System.out.println("==========首次修改文章完成===========");
    
            System.out.println("完整的信息" + editor);
    
            articleMemento = editor.saveToMemento();
    
            draftsBox.addMemento(articleMemento);
    
            System.out.println("==========保存到草稿箱===========");
    
    
            System.out.println("==========第2次修改文章===========");
            editor.setTitle("二次修改title");
            editor.setContent("二次修改文本");
            System.out.println("完整的信息" + editor);
            System.out.println("==========第2次修改文章完成===========");
    
            System.out.println("==========第1次撤销===========");
            articleMemento = draftsBox.getMemento();
            editor.undoFromMemento(articleMemento);
            System.out.println("完整的信息" + editor);
            System.out.println("==========第1次撤销完成===========");
    
    
            System.out.println("==========第2次撤销===========");
            articleMemento = draftsBox.getMemento();
            editor.undoFromMemento(articleMemento);
            System.out.println("完整的信息" + editor);
            System.out.println("==========第2次撤销完成===========");
    
        }
    }
    
    
    测试结果
    标题:初始title
    内容:初始文本
    插图:111111.png
    暂存成功
    完整的信息Editor{title='初始title', content='初始文本', imgs='111111.png'}
    ==========首次修改文章===========
    ==========首次修改文章完成===========
    完整的信息Editor{title='首次修改title', content='首次修改文本', imgs='111111.png'}
    ==========保存到草稿箱===========
    ==========第2次修改文章===========
    完整的信息Editor{title='二次修改title', content='二次修改文本', imgs='111111.png'}
    ==========第2次修改文章完成===========
    ==========第1次撤销===========
    完整的信息Editor{title='首次修改title', content='首次修改文本', imgs='111111.png'}
    ==========第1次撤销完成===========
    ==========第2次撤销===========
    完整的信息Editor{title='初始title', content='初始文本', imgs='111111.png'}
    ==========第2次撤销完成===========
    

    相关文章

      网友评论

          本文标题:设计模式-备忘录模式

          本文链接:https://www.haomeiwen.com/subject/jzecvltx.html