备忘录模式:主要目的是保存一个对象的某个状态,以便在适当的时候恢复对象。
模式角色:
- Originator(发起人):负责创建一个备忘录Memento。
- Memento(备忘录):负责存储Originator对象的内部状态。
- CareTaker(管理者):负责保存好备忘录Memento
originator.java-(发起者类,用于创建一个新的备忘录,并保存状态)
public class Originator {
//获取状态
private String state;
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public void show(){
System.out.println("State="+state);
}
//创建一个备忘录
public Memento createMemento(){
return new Memento(state);
}
//将创建的备忘录重新用于恢复状态
public void setMemento(Memento memento){
this.state=memento.getState();
}
}
Memento.java-(备忘录类用于保存状态)
public class Memento {
//存放的状态
private String state;
public Memento(String state){
this.state=state;
}
//从外部获取备忘录中保存的状态
public String getState(){
return state;
}
}
CareTaker.java-(专门用于存放备忘录的类)
public class CareTaker {
//用于保存备忘录
private Memento memento;
public Memento getMemento() {
return memento;
}
//将备忘录保存在CareTaker中
public void saveMemento(Memento memento) {
this.memento = memento;
}
}
Test.java
public class Test {
public static void main(String[] args) {
Originator o = new Originator();
o.setState("ON");
o.show();
CareTaker c = new CareTaker();
Memento memento = o.createMemento();
c.saveMemento(memento);
o.setState("OFF");
o.show();
//将备忘录信息恢复
o.setMemento(c.getMemento());
o.show();
}
}
游戏进度备忘
Role.java-角色状态
public class Role {
private int vit;
private int atk;
private int def;
//获得初始状态
public void getInitState(){
this.vit=100;
this.atk=100;
this.def=100;
}
//战斗
public void fight(){
this.vit=0;
this.atk=0;
this.def=0;
}
//displayState
public void stateDisplay(){
System.out.println("角色当前状态:");
System.out.println("体力:"+this.vit);
System.out.println("攻击力:"+this.atk);
System.out.println("防御力:"+this.def);
}
//保存角色状态
public RoleStateMemento saveState(){
return new RoleStateMemento(vit,atk,def);
}
//恢复角色状态
public void recoveryState(RoleStateMemento memento){
this.vit=memento.getVit();
this.atk=memento.getAtk();
this.def=memento.getDef();
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
RoleStateMemento.java-备忘录用于保存当前状态
public class RoleStateMemento {
private int vit;
private int atk;
private int def;
public RoleStateMemento(int vit,int atk,int def){
this.vit=vit;
this.atk=atk;
this.def=def;
}
public int getVit() {
return vit;
}
public void setVit(int vit) {
this.vit = vit;
}
public int getAtk() {
return atk;
}
public void setAtk(int atk) {
this.atk = atk;
}
public int getDef() {
return def;
}
public void setDef(int def) {
this.def = def;
}
}
RoleStateCaretaker.java-(存放备忘录)
public class RoleStateCaretaker {
private RoleStateMemento memento;
public RoleStateMemento getMemento() {
return memento;
}
public void setMemento(RoleStateMemento memento) {
this.memento = memento;
}
}
Test.java
public class Test {
public static void main(String[] args) {
Role role = new Role();
role.getInitState();
role.stateDisplay();
//保存游戏进度
RoleStateCaretaker roleStateCaretaker = new RoleStateCaretaker();
roleStateCaretaker.setMemento(role.saveState());
//大战Boss
role.fight();
role.stateDisplay();
//恢复之前的状态
role.recoveryState(roleStateCaretaker.getMemento());
role.stateDisplay();
}
}