java设计模式之备忘_java设计模式之备忘录模式

备忘录模式的定义:

备忘录模式又叫作快照模式或者令牌模式,指在不破坏封装的前提下,捕获一个状态的内部状态,并在对象之前保存这个状态。这样以后就可

将该对象恢复到原先保存的状态,属于行为型设计模式。

备忘录模式的应用场景:

需要保存历史快照的场景。

希望在对象之外保存状态,且除了自己,其他类对象无法访问状态保存的具体内容。

备忘录模式的UML类图:

2b522376c5d8e186963933685656f0f7.png

由上图可以看到,备忘录模式主要包含3个角色。

发起人角色(Originator):负责创建一个备忘录,记录自身需要保存的状态,具备状态回滚功能。

备忘录角色(Memento):用于存储Originator的内部状态,且可以防止Originator以外的对象进行访问。

备忘录管理员(Caretaker):负责存储、提供管理Memento,无法对Memento的内容进行操作和访问。

使用备忘录模式实现草稿箱功能:

首先创建发起人角色编辑器类:

public classEditor {privateString title;String content;String imgs;publicEditor(String title,String content,String imgs) {this.title =title;this.content =content;this.imgs =imgs;

}String getTitle() {returntitle;

}String getContent() {content;

}String getImgs() {voidsetTitle(String title) {setContent(String content) {setImgs(String imgs) {ArticleMemento saveToMemento(){

ArticleMemento articleMemento= new ArticleMemento(this.title,this.content,1)">this.imgs);articleMemento;

}undoFromMemento(ArticleMemento articleMemento){articleMemento.getTitle();articleMemento.getContent();articleMemento.getImgs();

}

@OverrideString toString() {return "Editor{" +

"title='" + title + '\'' +

",content='" + content + '\'' +

",imgs='" + imgs + '\'' +

'}';

}

}

然后创建备忘录角色:

ArticleMemento {ArticleMemento(String title,1)"> imgs;

}

@Overridereturn "ArticleMemento{" +

"title='" + title + '\'' +

",1)">;

}

}

接着创建备忘录管理角色草稿箱类,该类中的Stack类是Vector的一个子类,它实现了一个标准的后进先出的栈。

DraftsBox {private final Stack STACK = new Stack();ArticleMemento getMemento(){

ArticleMemento articleMemento=STACK.pop();addMemento(ArticleMemento articleMemento){

STACK.push(articleMemento);

}

}

最后编写客户端测试代码:

Test {static main(String[] args) {

DraftsBox draftsBox= newDraftsBox();

Editor editor= new Editor("我是这样手写Spring的,麻雀虽小五脏俱全","本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著,电子工业出版社出版。");

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("【Tom原创】我是这样手写Spring的,麻雀虽小五脏俱全");

editor.setContent("本文节选自《Spring5核心原理与30个类手写实战》一书,Tom著");

System.out.println("==========首次修改文章完成==========="editor);

articleMemento=editor.saveToMemento();

draftsBox.addMemento(articleMemento);

System.out.println("==========保存到草稿箱===========");

System.out.println("==========第2次修改文章===========");

editor.setTitle("手写Spring");

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次撤销==========="editor);

System.out.println("==========第2次撤销完成===========");

}

}

在测试类中,我们首先创建一个管理角色对象,然后创建一个创建人角色编辑器,并赋值,相当于编辑一部分内容的状态,然后将当前的内容

保存到备忘录角色对象,然后通过管理角色压入到栈中。然后继续编辑,继续保存到备忘录。当撤销时,又以此还原到原来的状态。

备忘录模式的优点:

简化发起人实体类的职责,隔离状态存储于获取,实现了信息的封装,客户端无须关心状态的保存细节。

提供状态回滚功能。

备忘录模式的缺点:

备忘录模式的缺点主要是消耗资源。如果需要保存的状态过多,则每一次保存都会消耗很多内存。

总结

以上是编程之家为你收集整理的java设计模式之备忘录模式全部内容,希望文章能够帮你解决java设计模式之备忘录模式所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。

如您喜欢交流学习经验,点击链接加入交流1群:1065694478(已满)交流2群:163560250

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值