目录
前言
设计模式,又称为软件设计模式,是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。学习设计模式可以提高程序员的思维能力、编程能力和设计能力,使程序设计更加标准化、代码编制更加工程化,使软件开发效率大大提高,从而缩短软件的开发周期。
一、🌍备忘录(Memento)模式是什么?🌍
在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态。该模式又叫快照模式。
二、🌍备忘录(Memento)模式的优缺点🌍
优点:
1)提供了一种可以恢复状态的机制。用户当需要时能够比较方便地将数据恢复到某个历史的状态;
2)实现了内部状态的封装。除了创建它的发起人之外其他对象都不能够访问这些状态信息;
3)简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
缺点:
1)资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。
三、🌍备忘录(Memento)模式的结构🌍
备忘录模式的主要角色有:
⑴ 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其它业务功能,它可以访问备忘录里的所有信息。
⑵ 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
⑶ 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
四、🌍实例演示🌍
1.🌕实例概述🌕
发起人角色:
AddExercise类,一个加法练习题小程序;
备忘录角色:
AdditionMemento类,加法练习题备忘录角色,存储发起人状态;
管理者角色:
Drafters类,草稿箱,用于存放备忘录角色的一系列操作。
2.🌕代码演示🌕
话不多说,代码附上!
package Memento;
import java.util.Stack;
public class MementoDemo{
public static void main(String[] args) {
//管理者
Drafters drafters = new Drafters();
//发起人的角色
AddExercise addExercise = new AddExercise(1);
//备忘录的角色,封装了内部状态
AdditionMemento additionMemento = addExercise.saveToMemento();
drafters.addMemento(additionMemento);
System.out.println("暂存成功");
System.out.println("完整信息" + addExercise);
System.out.println("==========首次保存题目进度===========");
addExercise.setIndex(25);
System.out.println("==========首次修改文章完成===========");
System.out.println("完整信息" + addExercise);
additionMemento = addExercise.saveToMemento();
drafters.addMemento(additionMemento);
System.out.println("==========保存到草稿箱===========");
System.out.println("==========第2次修改文章===========");
addExercise.setIndex(56);
System.out.println("完整信息" + addExercise);
System.out.println("==========第2次修改文章完成===========");
System.out.println("==========第1次撤销===========");
additionMemento = drafters.getMemento();
addExercise.undoFromMemento(additionMemento);
System.out.println("完整的信息" + addExercise);
System.out.println("==========第1次撤销完成===========");
System.out.println("==========第2次撤销===========");
additionMemento = drafters.getMemento();
addExercise.undoFromMemento(additionMemento);
System.out.println("完整的信息" + addExercise);
System.out.println("==========第2次撤销完成===========");
}
}
class AddExercise {
private int index;
public AddExercise(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public AdditionMemento saveToMemento() {
AdditionMemento additionMemento = new AdditionMemento(this.index);
return additionMemento;
}
public void undoFromMemento(AdditionMemento additionMemento) {
this.index = additionMemento.getIndex();
}
@Override
public String toString() {
return "{当前已完成" +
index + '题' +
'}';
}
}
//然后创建备忘录角色 AdditionMemento类。
class AdditionMemento {
private int index;
public AdditionMemento(int index) {
this.index = index;
}
public int getIndex() {
return index;
}
@Override
public String toString() {
return "{当前已完成" +
index + '题' +
'}';
}
}
//管理者
class Drafters {
//草稿箱中定义的 Stack 类是 Vector 的一个子类,它实现了一个标准的后进先出的栈。
private final Stack<AdditionMemento> STACK = new Stack<AdditionMemento>();
public AdditionMemento getMemento() {
AdditionMemento additionMemento = STACK.pop();
return additionMemento;
}
public void addMemento(AdditionMemento additionMemento) {
STACK.push(additionMemento);
}
}
3.🌕实验结果🌕
暂存成功
完整信息{当前已完成1题}
==========首次保存题目进度===========
==========首次修改文章完成===========
完整信息{当前已完成25题}
==========保存到草稿箱===========
==========第2次修改文章===========
完整信息{当前已完成56题}
==========第2次修改文章完成===========
==========第1次撤销===========
完整的信息{当前已完成25题}
==========第1次撤销完成===========
==========第2次撤销===========
完整的信息{当前已完成1题}
==========第2次撤销完成===========
总结
以上就是今天要讲的内容,本文仅仅简单介绍了行为模式下备忘录模式的定义,优缺点以及其的模式结构,并通过实例演示,进一步反应了在一定需求条件下,备忘录模式的优点。