【设计模式】——备忘录模式

本文详细介绍了备忘录设计模式的原理与应用,通过代码示例展示了如何在不破坏封装性的前提下,保存和恢复对象的内部状态。适用于需要撤销功能的命令模式,或在复杂对象状态变化时进行有效备份的场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

备忘录(Memento):在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态。这样以后就可将该对象恢复到原先保存的状态。

  Originator(发起人):负责创建一个备忘录Memento,用以记录当前时刻它的内部状态,并可使用备忘录恢复内部状态。Originator可根据需要决定Memento存储Originator的那些状态。Memento(备忘录):负责存储Originator对象的内部状态,并可防止Originator以外的对象访问备忘录Memento。备忘录有两个接口,Caretaker只能看到备忘录的窄接口,它只能将备忘录传递给其他对象。Originator能够看到一个宽接口,允许它访问返回到先前状态所需的所有数据。Caretaker(管理者):负责保存好备忘录Memento,不能对备忘录的内容进行操作或检查。这当中要保存的细节给封装在了Memento中,那一天要更改保存的细节也不用影响客户端了。

  代码基本模板

#include <iostream>

using namespace std;
//备忘录(Memento)类
class Memento
{
private:
    string m_state;
public:
    Memento(string state):m_state(state)
    {
    }
    string getState()
    {
        return this->m_state;
    }
};
//发起人(Originator)类
class Originator
{
private:
    string m_state;
public:
    void setState(string state)
    {
        this->m_state=state;
    }
    string getState()
    {
        return this->m_state;
    }
    //创建备忘录,将当前需要保存的信息导入并实例化一个Memento对象
    Memento *CreateMemento()
    {
        return (new Memento(this->m_state));
    }
    //恢复备忘录,将Memento导入并将相关数据恢复
    void SetMemento(Memento *memento)
    {
        this->m_state=memento->getState();
    }
    void Show()
    {
        cout << "State=" << this->m_state << endl;
    }
};
//管理者(Caretaker)类
class Caretaker
{
private:
    Memento *m_memento;
public:
    //设置备忘录
    void setMemento(Memento *memento)
    {
        this->m_memento=memento;
    }
    //得到备忘录
    Memento *getMemento()
    {
        return this->m_memento;
    }
};
int main()
{
    Originator *O=new Originator();
    O->setState("On");
    O->Show();
    Caretaker *c=new Caretaker();
    c->setMemento(O->CreateMemento());
    O->setState("Off");
    O->Show();
    O->SetMemento(c->getMemento());
    O->Show();
    return 0;
}

备忘录所用场合:Memento模式比较适用于功能比较复杂的,但需要维护或记录属性历史的类,或者需要保存的属性只是众多属性中的一小部分时,Originator可以根据保存的Memento信息还原到前一状态。如果在某个系统中使用命令模式时,需要实现命令的撤销功能,那么命令模式可以使用备忘录模式来保存可撤销操作的状态。有时一些对象的内部信息必须保存在对象以外的地方,但是必须要有对象自己读取,这时,使用备忘录可以把复杂的对象内部信息对其他的对象屏蔽起来,从而可以恰当地保持封装的边界。其实最大的作用是在当角色的状态改变的时候,有可能这个状态无效,这时候就可以使用暂时存储起来的备忘录将状态复原。

 

游戏进度备忘

#include <iostream>

using namespace std;
class RoleStateMemento
{
 private:
    int m_vit;
    int m_atk;
    int m_def;
 public:
    RoleStateMemento(int vit,int atk,int def)
    {
        this->m_vit=vit;
        this->m_atk=atk;
        this->m_def=def;
    }
    int getVit()
    {
        return this->m_vit;
    }
    int getAtk()
    {
        return this->m_atk;
    }
    int getDef()
    {
        return this->m_def;
    }
};
class GameRoles
{
private:
    int m_vit;
    int m_atk;
    int m_def;
public:
    RoleStateMemento *SaveState()
    {
        return (new RoleStateMemento(this->m_vit,this->m_atk,this->m_def));
    }
    void RecoveryState(RoleStateMemento *memento)
    {
        this->m_vit=memento->getVit();
        this->m_atk=memento->getAtk();
        this->m_def=memento->getDef();
    }
    void GetInitState()
    {
        this->m_vit=100;
        this->m_atk=100;
        this->m_def=100;
    }
    void Fight()
    {
        this->m_vit=0;
        this->m_atk=0;
        this->m_def=0;
    }
    void StateDisplay()
    {
        cout << "角色当前状态:" << endl;
        cout << "体力:" << this->m_vit << endl;
        cout << "攻击力:" << this->m_atk << endl;
        cout << "防御力:" << this->m_def << endl;
    }
};
class RoleStateCaretaker
{
private:
    RoleStateMemento *m_memento;
public:
    void SetMemento(RoleStateMemento *memento)
    {
        this->m_memento=memento;
    }
    RoleStateMemento *GetMemento()
    {
        return this->m_memento;
    }
};
int main()
{
    //大战Boss前
    GameRoles *lixiaoyao=new GameRoles();
    lixiaoyao->GetInitState();
    lixiaoyao->StateDisplay();
    //保存进度
    RoleStateCaretaker *stateAdmin=new RoleStateCaretaker();
    stateAdmin->SetMemento(lixiaoyao->SaveState());
    //大战Boss时,损耗严重
    lixiaoyao->Fight();
    lixiaoyao->StateDisplay();
    //恢复之前状态
    lixiaoyao->RecoveryState(stateAdmin->GetMemento());
    lixiaoyao->StateDisplay();
    return 0;
}

转载于:https://www.cnblogs.com/awy-blog/p/3828889.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值