备忘录模式原理类图:
package memento;
public class Originator {
/*originator:需要保存状态的对象*/
private String state;//状态信息
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Originator() {
}
public Originator(String state) {
this.state = state;
}
//编写一个方法,可以保存一个状态对象Memento
public Memento saveMemento() {
return new Memento(state);
}
//通过备忘录对象,恢复状态
public void getStateFromMemento(Memento memento) {
state=memento.getState();
}
}
package memento;
public class Memento {
//Memento:备忘录对象,负责保存好记录,即Originator内部状态
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public Memento(String state) {
this.state = state;
}
}
package memento;
import java.util.ArrayList;
import java.util.List;
/*Caretaker:守护者对象,负责保存多个备忘录对象,
* 使用集合管理,提高效率
* 如果需要保存多个Originator对象的不同时间状态,可以用HashMap<String,集合>*/
public class CareTaker {
//在list集合中,会有很多Memento对象
private List<Memento> mementoList=new ArrayList<Memento>();
public void add(Memento memento) {
mementoList.add(memento);
}
//获取到第index个Originator的备忘录对象,即保存状态
public Memento get(int index) {
return mementoList.get(index);
}
}
package memento;
public class Client {
public static void main(String[] args) {
Originator originator=new Originator();
CareTaker careTaker=new CareTaker();
originator.setState("state1");
//保存了当前状态
careTaker.add(originator.saveMemento());
originator.setState("state2");
careTaker.add(originator.saveMemento());
originator.setState("state3");
careTaker.add(originator.saveMemento());
System.out.println("当前状态为:"+originator.getState());
//希望得到状态1,并将originator恢复到状态1
originator.getStateFromMemento(careTaker.get(0));
System.out.println("恢复到状态1,当前状态为:"+originator.getState());
}
}
象棋悔棋的备忘录实现:
package memento_chess;
//象棋类
public class Chess {
private String name;
private int x;
private int y;
public ChessMemento createMemento() {
return new ChessMemento(name,x,y);
}
public Chess(String name, int x, int y) {
super();
this.name = name;
this.x = x;
this.y = y;
}
public Chess() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public void setMemento(ChessMemento chessMemento) {
if(chessMemento==null)return;
this.name=chessMemento.getName();
this.x=chessMemento.getX();
this.y=chessMemento.getY();
}
@Override
public String toString() {
return "Chessman [name=" + name + ", x=" + x + ", y=" + y + "]";
}
}
package memento_chess;
//象棋备忘录,用于悔棋,保存了象棋的信息
public class ChessMemento {
private String name;
private int x;
private int y;
public ChessMemento(String name, int x, int y) {
this.name = name;
this.x = x;
this.y = y;
}
public ChessMemento() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
}
package memento_chess;
import java.util.ArrayList;
import java.util.List;
//管理备忘录
public class MementoCareTaker {
private List<ChessMemento> mementoList=new ArrayList<ChessMemento>();
public void add(ChessMemento memento) {
mementoList.add(memento);
}
//获取到第index-2(index是从0开始,悔棋一步index-2)个Chess的备忘录对象,即悔棋
public ChessMemento huiQi() {
int index=mementoList.size();
ChessMemento c=mementoList.get(index-2);
mementoList.remove(index-2);
return c;
}
}
package memento_chess;
public class Client {
public static void main(String[] args) {
MementoCareTaker careTaker=new MementoCareTaker();
Chess chess=new Chess();
chess.setName("炮2");
chess.setX(5);
chess.setY(2);
System.out.println("象棋的初始状态为:"+chess);
careTaker.add(chess.createMemento());
chess.setName("马2");
chess.setX(8);
chess.setY(2);
System.out.println("象棋的当前状态为:"+chess);
careTaker.add(chess.createMemento());
chess.setName("兵2");
chess.setX(2);
chess.setY(4);
System.out.println("象棋的当前状态为:"+chess);
careTaker.add(chess.createMemento());
System.out.println("悔棋");
chess.setMemento(careTaker.huiQi());
System.out.println(chess);
}
}