一、背景
备忘录这个词汇大家应该都不陌生,我们经常使用备忘录来记录一些比较重要的或者容易遗忘的信息,与之相关的最常见的应用有许多,比如游戏存档,我们玩游戏的时候肯定有存档功能,旨在下一次登录游戏时可以从上次退出的地方继续游戏,还有在 IE 中的后退键、数据库事务管理中的回滚操作。
二、定义
备忘录模式(Memento Pattern)又叫做快照模式(Snapshot Pattern)或Token模式。在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态,以便以后当需要时能将该对象恢复到原先保存的状态
2.1. 模式的结构
备忘录模式的主要角色如下。
- 发起人(Originator)角色:记录当前时刻的内部状态信息,提供创建备忘录和恢复备忘录数据的功能,实现其他业务功能,它可以访问备忘录里的所有信息。
- 备忘录(Memento)角色:负责存储发起人的内部状态,在需要的时候提供这些内部状态给发起人。
- 管理者(Caretaker)角色:对备忘录进行管理,提供保存与获取备忘录的功能,但其不能对备忘录的内容进行访问与修改。
结构图如下:
2.2 代码
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
/// <summary>
/// 备忘录
/// </summary>
public class Memento
{
private String state;
public Memento(String state)
{
this.state = state;
}
public void setState(String state)
{
this.state = state;
}
public String getState()
{
return state;
}
}
/// <summary>
/// 发起人
/// </summary>
public class Originator
{
private String state;
public void setState(String state)
{
this.state = state;
}
public String getState()
{
return state;
}
public Memento createMemento()
{
return new Memento(state);
}
public void restoreMemento(Memento m)
{
this.setState(m.getState());
}
}
/// <summary>
/// 管理者
/// </summary>
public class Caretaker
{
private Memento memento;
public void setMemento(Memento m)
{
memento = m;
}
public Memento getMemento()
{
return memento;
}
}
class Program
{
static void Main(string[] args)
{
Originator or = new Originator();
Caretaker cr = new Caretaker();
or.setState("S0");
Console.WriteLine("初始状态:" + or.getState());
cr.setMemento(or.createMemento()); //保存状态
or.setState("S1");
Console.WriteLine("新的状态:" + or.getState());
or.restoreMemento(cr.getMemento()); //恢复状态
Console.WriteLine("恢复状态:" + or.getState());
Console.ReadKey();
}
}
}
运行结果
三、实践
下面以一个游戏存档的功能为例,看一下如何用备忘录模式实现。UML图如下:
代码
using System;
using System.Collections.Generic;
namespace ConsoleApp1
{
/// <summary>
/// 游戏角色
/// </summary>
public class GameRole
{
private int vit; //生命力
private int atk; //攻击力
private int 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;
}
//状态显示
public void stateDisplay()
{
Console.WriteLine("角色当前状态:");
Console.WriteLine("体力:" + this.vit);
Console.WriteLine("攻击力:" + this.atk);
Console.WriteLine("防御力: " + this.def);
Console.WriteLine("-----------------");
}
//获得初始状态
public void getInitState()
{
this.vit = 100;
this.atk = 100;
this.def = 100;
}
//战斗后
public void fight()
{
this.vit = 0;
this.atk = 0;
this.def = 0;
}
//保存角色状态
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();
}
}
/// <summary>
/// 角色状态存储箱
/// </summary>
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;
}
}
/// <summary>
/// 备忘录管理者
/// </summary>
public class RoleStateCaretaker
{
private RoleStateMemento memento;
public RoleStateMemento getMemento()
{
return memento;
}
public void setMemento(RoleStateMemento memento)
{
this.memento = memento;
}
}
class Program
{
static void Main(string[] args)
{
//打boss前
GameRole gameRole = new GameRole();
gameRole.getInitState();
gameRole.stateDisplay();
//保存进度
RoleStateCaretaker caretaker = new RoleStateCaretaker();
caretaker.setMemento(gameRole.saveState());
//打boss失败
gameRole.fight();
gameRole.stateDisplay();
//恢复状态
gameRole.recoveryState(caretaker.getMemento());
gameRole.stateDisplay();
Console.ReadKey();
}
}
}
运行结果
四、总结
备忘录模式是一种对象行为型模式,其主要优点如下。
- 提供了一种可以恢复状态的机制。当用户需要时能够比较方便地将数据恢复到某个历史的状态。
- 实现了内部状态的封装。除了创建它的发起人之外,其他对象都不能够访问这些状态信息。
- 简化了发起人类。发起人不需要管理和保存其内部状态的各个备份,所有状态信息都保存在备忘录中,并由管理者进行管理,这符合单一职责原则。
其主要缺点是:资源消耗大。如果要保存的内部状态信息过多或者特别频繁,将会占用比较大的内存资源。
参考:
http://c.biancheng.net/view/1400.html