设计模式-备忘录模式

介绍

  人如其名,该模式是保存对象的某个状态,以便在适当的时候恢复对象(有点像"后悔药")。

  适用场景:
  撤销操作:如在Wps中编写文档,使用CTRL + Z恢复到上一步的内容
  状态恢复:如在游戏中的存档、恢复存档。


角色

  发起人(Originator)角色:可以创建一个备忘录,保存当前状态;并能根据备忘录恢复状态
  备忘录(Memento)角色:用于存储发起人角色某一刻的状态,并且除发起人、备忘录管理角色外不可访问(三者放在一个包中,此类设为default)
  备忘录管理(Caretaker)角色:负责存储、提供备忘录,不能修改备忘录里的内容

示例代码

  发起人角色:

	import lombok.Data;
	
	/**
	 * 发起人角色
	 */
	@Data
	public class Article {
	
	    private String title;
	    private String content;
	    private String image;
	
	    public Article(String tittle, String content, String image) {
	        this.title = tittle;
	        this.content = content;
	        this.image = image;
	    }
	
	    /**
	     * 创建备忘录
	     * @return
	     */
	    public ArticleMemento saveToMemento() {
	        ArticleMemento articleMemento = new ArticleMemento(title, 
	        content, image);
	        return articleMemento;
	    }
	
	    /**
	     * 从备忘录恢复
	     * @param articleMemento
	     */
	    public void undoFromMemento(ArticleMemento articleMemento) {
	        this.title = articleMemento.getTitle();
	        this.content = articleMemento.getContent();
	        this.image = articleMemento.getImage();
	    }
	
	    @Override
	    public String toString() {
	        return "Article{" +
	                "title='" + title + "'" +
	                ", content='" + content + "'" +
	                ", image='" + image + "'" +
	                "}";
	    }
	
	}

  备忘录角色

	/**
	 * 备忘录角色
	 * 属性无set方法,防止备忘录管理角色修改
	 */
	public class ArticleMemento {
	
	    private String title;
	    private String content;
	    private String image;
	
	    public ArticleMemento(String title, String content, String image){
	        this.title = title;
	        this.content = content;
	        this.image = image;
	    }
	
	    public String getTitle() {
	        return title;
	    }
	
	    public String getContent() {
	        return content;
	    }
	
	    public String getImage() {
	        return image;
	    }
	
	}

  备忘录管理角色:

	/**
	 * 备忘录管理角色
	 */
	public class ArticleMementoManager {
	
	    /**
	     * 存储备忘录;用栈存储,先进后出
	     */
	    private final Stack<ArticleMemento> mArticleMementoStack = 
	    new Stack<>();
	
	    /**
	     * 获取栈顶的备忘录
	     * @return
	     */
	    public ArticleMemento getArticleMemento() {
	        return mArticleMementoStack.pop();
	    }
	
	    /**
	     * 备忘录入栈顶
	     * @param articleMemento
	     */
	    public void setArticleMemento(ArticleMemento articleMemento) {
	        mArticleMementoStack.push(articleMemento);
	    }
	
	}

  测试类:

	public class Test {
	
	    public static void main(String[] args) {
	        ArticleMementoManager articleMementoManager = 
	        new ArticleMementoManager();
	
	        Article article = new Article("标题a", "内容b", "图片链接c");
	        ArticleMemento articleMemento = article.saveToMemento();
	        articleMementoManager.setArticleMemento(articleMemento);
	        // 打印发起人角色
	        System.out.println("文档信息 : " + article.toString());
	
	        // 修改文档内容
	        article.setTitle("标题d");
	        article.setContent("内容e");
	        article.setImage("图片链接f");
	        articleMemento = article.saveToMemento();
	        articleMementoManager.setArticleMemento(articleMemento);
	        // 打印发起人角色
	        System.out.println("文档信息 : " + article.toString());
	
	        // 此时 ArticleMementoManager 中存储了 2 个存档
	        // 存档 1 : Article{title='标题a', content='内容b', image=
	        // '图片链接c'}
	        // 存档 2 : Article{title='标题d', content='内容e', image=
	        // '图片链接f'}
	
	        articleMementoManager.getArticleMemento();
	        article.undoFromMemento(articleMementoManager
	        .getArticleMemento());
	        // 打印发起人角色
	        System.out.println("文档信息 : " + article.toString());
	    }
	
	}

  运行结果如下:
在这里插入图片描述

优缺点

  优点:为用户提供一种可恢复机制
  缺点:额外占用资源,像磁盘、内存等。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值