备忘录模式经常可以遇到,譬如下面这些场景:
Ctrl + z 撤销,撤销后可以按 Ctrl + y 重做git reset --hard 版本号 即可回到指定的版本,让代码时空穿梭回到过去某个历史时刻在不破坏封装的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,这样可以在以后将对象恢复到原先保存的状态。它是一种对象行为型模式,其别名为Token。
备忘录模式的核心是备忘录类以及用于管理备忘录的负责人类的设计。
说明:如果希望保存多个originator对象的不同时间的状态,也可以,只需要 HashMap <String, 集合>
举个例子说明,下棋软件要提供“悔棋”功能,用户走错棋或操作失误后可恢复到前一个步骤。悔棋可能回到上一步,也有可能回到上上次的状态…因此需要记录多次的状态


在设计备忘录类时需要考虑其封装性,除了Originator类,不允许其他类来调用备忘录类Memento的构造函数与相关方法,如果不考虑封装性,允许其他类调用setState()等方法,将导致在备忘录中保存的历史状态发生改变,通过撤销操作所恢复的状态就不再是真实的历史状态,备忘录模式也就失去了本身的意义。
originator : 对象(需要保存状态的对象)
public class Originator {
private String state;//状态
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
//编写一个方法,可以保存一个状态对象Memento
public Memento saveStateMemento(){
return new Memento(state);
}
public void getStateFromMemento (Memento memento){
state = memento.getState();
}
}Memento : 备忘录对象,负责保存好记录,即Originator内部状态
public class Memento {
private String state;
public Memento(String state) {
this.state = state;
}
public String getState() {
return state;
}
}Caretaker: 守护者对象,负责保存多个备忘录对象, 使用集合管理,提高效率
public class Caretaker {
//List集合中会有很多备忘录对象
private List<Memento> mementoList = new ArrayList<>();
public void add(Memento memento) {
mementoList.add(memento);
}
//获取到第index个Origintor的备忘录对象
public Memento get(int index){
return mementoList.get(index);
}
}棋子类 Chessman,原发器角色
//原发器,需要保存对象的状态
@Data
@AllArgsConstructor
public class Chessman
{
private String label;//当前棋子的名字: 车,炮,马
private Integer x,y;//当前棋子的坐标
//保存当前对象的状态--备份数据
public ChessmanMemento save()
{
return new ChessmanMemento(label,x,y);
}
//恢复当前对象的状态
public void restore(ChessmanMemento chessmanMemento)
{
this.label=chessmanMemento.getLabel();
this.x=chessmanMemento.getX();
this.y=chessmanMemento.getY();
}
//展示当前对象的状态
public void show()
{
System.out.println(
String.format("棋子: %s ,位置: [%d,%d]",label,x,y)
);
}
}备忘录角色 ChessmanMemento
//负责备份的棋子状态
@Data
@AllArgsConstructor
public class ChessmanMemento
{
private String label;
private Integer x,y;
}负责人角色 MementoCaretaker
//负责保存多个备份对象
public class MementoCaretaker
{
//记录当前所处的备份状态
Integer index=-1;//一开始没有备份数据
//通过一个List集合保存多个备份对象
List<ChessmanMemento> chessmanMementoLinkedList= Lists.newLinkedList();
//悔棋操作--恢复到上一个备忘录状态
public ChessmanMemento getMemento()
{
if(index<=0)
{
throw new IndexOutOfBoundsException("已经无棋可悔了");
}
this.index--;//当前所处的备份状态减去一
//将当前状态之后的状态全部清空
//保留前index个元素,并将流收集到List中
chessmanMementoLinkedList = chessmanMementoLinkedList.stream()
.limit(this.index+1).collect(Collectors.toList());
return chessmanMementoLinkedList.get(index);
}
//下棋---增加新的备份对象
public void addMemento(ChessmanMemento chessmanMemento)
{
index++;
chessmanMementoLinkedList.add(chessmanMemento);
}
}棋子客户端,维护了一个 MementoCaretaker 对象
//客户端
public class Client
{
//维护一个守护者对象
MementoCaretaker mementoCaretaker=new MementoCaretaker();
//下棋
public void play(Chessman chessman)
{
//通过调用备份返回,返回一个备份对象,添加进备份集合中去
mementoCaretaker.addMemento(chessman.save());
}
//悔棋
public void undo(Chessman chessman)
{
//得到上一次记录的备份状态对象
ChessmanMemento memento = mementoCaretaker.getMemento();
//调用恢复功能
chessman.restore(memento);
}
}测试
public class Test
{
public static void main(String[] args) {
//创建棋子对象
Chessman chessman=new Chessman("车",1,1);
//创建一个客户端
Client client=new Client();
client.play(chessman);
chessman.show();
chessman=new Chessman("马",2,0);
client.play(chessman);
chessman.show();
//悔棋
client.undo(chessman);
chessman.show();
client.undo(chessman);
chessman.show();
}
}