设计模式 - 16)备忘录模式

打游戏的时候,存档;
场景:保存某个时刻的状态,一般用于针对的保存部分属性,如果是所有,直接调用 Clone 即可。

graph LR 角色Originator-->存档Memento 管理存档CareTaker-->存档Memento
/// <summary>
/// 角色类
/// </summary>
class Originator
{
    int _attackNum;
    public int AttackNum
    {
        get { return _attackNum; }
        set { _attackNum = value; }
    }

    ...

    public void Show()
    {
        Console.WriteLine(string.Format("攻击力:{0},防御力:{1},生命力:{2}", AttackNum, DefenseNum, LifeNum));
    }

    public void InitStance()
    {
        this._attackNum = 100;
        this._defenseNum = 100;
        this._lifeNum = 100;
    }

    public void hart()
    {
        Console.WriteLine("角色受到伤害");
        this._attackNum -= 10;
        this._defenseNum -= 10;
        this._lifeNum -= 10;
    }
        
    /// <summary>
    /// 保存当前存档
    /// </summary>
    /// <returns></returns>
    public Memento SaveState()
    {
        return new Memento(_attackNum, _defenseNum, _lifeNum);
    }
    
    /// <summary>
    /// 加载某个存档
    /// </summary>
    /// <param name="memento"></param>
    public void RecoverState(Memento memento)
    {
        this._attackNum = memento.AttackNum;
        this._defenseNum = memento.DefenseNum;
        this._lifeNum = memento.LifeNum;
    }
}

/// <summary>
/// 存档类
/// </summary>
class Memento
{
    int _attackNum;
    public int AttackNum
    {
        get { return _attackNum; }
        set { _attackNum = value; }
    }
    ...
    
    public Memento(int attack, int defense, int life)
    {
        this._attackNum = attack;
        this._defenseNum = defense;
        this._lifeNum = life;
    }
}

/// <summary>
/// 管理存档类
/// </summary>
class CareTaker
{
    Memento _memento;
    public Memento Memento
    {
        get { return _memento; }
        set { _memento = value; }
    }
}

// 业务代码:
// 备忘管理
CareTaker ct = new CareTaker();

// 角色
Originator og = new Originator();
// 角色初始化
og.InitStance();
og.Show();

// 角色受到伤害
og.hart();
og.Show();
ct.Memento = og.SaveState();

// 角色继续暴击
og.hart();
og.Show();
og.hart();
og.Show();

// 扛不动了,重新加载一下存档
og.RecoverState(ct.Memento);
            
og.Show();

后面如果需要管理的存档多了:

interface IMemento { }

class Memento : IMemento {...}

class CareTaker
{
    Dictionary<string, IMemento> MementoDic = new Dictionary<string, IMemento>();

    public Memento GetMemento(string key)
    {
        Memento result = null;
        if (MementoDic.ContainsKey(key))
        {
            result = (Memento)MementoDic[key];
        }
        return result;
    }

    public void AddMemento(string key, IMemento memento)
    {
        if (MementoDic.ContainsKey(key))
        {
            MementoDic[key] = memento;
        }
        else
        {
            MementoDic.Add(key, memento);
        }
    }
}

// 业务代码:
CareTaker ct = new CareTaker();

// 角色
Originator og = new Originator();

string mementoName = "og" + DateTime.Now.ToString();
// 存档
ct.AddMemento(mementoName, og.SaveState();

// 加载存档
og.RecoverState(ct.GetMemento(mementoName));
posted @ 2020-12-27 22:07  鑫茂  阅读(88)  评论(0编辑  收藏  举报